|
| 1 | +package com.github.abel533.sql; |
| 2 | + |
| 3 | +import org.apache.ibatis.builder.StaticSqlSource; |
| 4 | +import org.apache.ibatis.mapping.*; |
| 5 | +import org.apache.ibatis.scripting.LanguageDriver; |
| 6 | +import org.apache.ibatis.session.Configuration; |
| 7 | +import org.apache.ibatis.session.SqlSession; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +/** |
| 14 | + * MyBatis执行sql工具 |
| 15 | + * |
| 16 | + * @author liuzh |
| 17 | + * @since 2015-03-10 |
| 18 | + */ |
| 19 | +public class SqlMapper { |
| 20 | + private final MSUtils msUtils; |
| 21 | + private SqlSession sqlSession; |
| 22 | + |
| 23 | + public SqlMapper(SqlSession sqlSession) { |
| 24 | + this.sqlSession = sqlSession; |
| 25 | + this.msUtils = new MSUtils(sqlSession.getConfiguration()); |
| 26 | + } |
| 27 | + |
| 28 | + public List<Map<String, Object>> select(String sql) { |
| 29 | + String msId = msUtils.select(sql); |
| 30 | + return sqlSession.selectList(msId); |
| 31 | + } |
| 32 | + |
| 33 | + public List<Map<String, Object>> select(String sql, Object value) { |
| 34 | + Class<?> parameterType = value != null ? value.getClass() : null; |
| 35 | + String msId = msUtils.selectDynamic(sql, parameterType); |
| 36 | + return sqlSession.selectList(msId, value); |
| 37 | + } |
| 38 | + |
| 39 | + public <T> List<T> select(String sql, Class<T> resultType) { |
| 40 | + String msId; |
| 41 | + if (resultType == null) { |
| 42 | + msId = msUtils.select(sql); |
| 43 | + } else { |
| 44 | + msId = msUtils.select(sql, resultType); |
| 45 | + } |
| 46 | + return sqlSession.selectList(msId); |
| 47 | + } |
| 48 | + |
| 49 | + public <T> List<T> select(String sql, Object value, Class<T> resultType) { |
| 50 | + String msId; |
| 51 | + Class<?> parameterType = value != null ? value.getClass() : null; |
| 52 | + if (resultType == null) { |
| 53 | + msId = msUtils.selectDynamic(sql, parameterType); |
| 54 | + } else { |
| 55 | + msId = msUtils.selectDynamic(sql, parameterType, resultType); |
| 56 | + } |
| 57 | + return sqlSession.selectList(msId, value); |
| 58 | + } |
| 59 | + |
| 60 | + public int insert(String sql) { |
| 61 | + String msId = msUtils.insert(sql); |
| 62 | + return sqlSession.insert(msId); |
| 63 | + } |
| 64 | + |
| 65 | + public int insert(String sql, Object value) { |
| 66 | + Class<?> parameterType = value != null ? value.getClass() : null; |
| 67 | + String msId = msUtils.insertDynamic(sql, parameterType); |
| 68 | + return sqlSession.insert(msId, value); |
| 69 | + } |
| 70 | + |
| 71 | + public int update(String sql) { |
| 72 | + String msId = msUtils.update(sql); |
| 73 | + return sqlSession.insert(msId); |
| 74 | + } |
| 75 | + |
| 76 | + public int update(String sql, Object value) { |
| 77 | + Class<?> parameterType = value != null ? value.getClass() : null; |
| 78 | + String msId = msUtils.updateDynamic(sql, parameterType); |
| 79 | + return sqlSession.insert(msId, value); |
| 80 | + } |
| 81 | + |
| 82 | + public int delete(String sql) { |
| 83 | + String msId = msUtils.insert(sql); |
| 84 | + return sqlSession.delete(msId); |
| 85 | + } |
| 86 | + |
| 87 | + public int delete(String sql, Object value) { |
| 88 | + Class<?> parameterType = value != null ? value.getClass() : null; |
| 89 | + String msId = msUtils.deleteDynamic(sql, parameterType); |
| 90 | + return sqlSession.insert(msId, value); |
| 91 | + } |
| 92 | + |
| 93 | + private class MSUtils { |
| 94 | + private Configuration configuration; |
| 95 | + private LanguageDriver languageDriver; |
| 96 | + |
| 97 | + private MSUtils(Configuration configuration) { |
| 98 | + this.configuration = configuration; |
| 99 | + languageDriver = configuration.getDefaultScriptingLanuageInstance(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * 创建MSID |
| 104 | + * |
| 105 | + * @param sql |
| 106 | + * @param sqlCommandType |
| 107 | + * @return |
| 108 | + */ |
| 109 | + private String newMsId(String sql, SqlCommandType sqlCommandType) { |
| 110 | + StringBuilder msIdBuilder = new StringBuilder(sqlCommandType.toString()); |
| 111 | + msIdBuilder.append(".").append(sql.hashCode()); |
| 112 | + return msIdBuilder.toString(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * 是否已经存在该ID |
| 117 | + * |
| 118 | + * @param msId |
| 119 | + * @return |
| 120 | + */ |
| 121 | + private boolean hasMappedStatement(String msId) { |
| 122 | + return configuration.hasStatement(msId, false); |
| 123 | + } |
| 124 | + |
| 125 | + private String select(String sql) { |
| 126 | + String msId = newMsId(sql, SqlCommandType.INSERT); |
| 127 | + if (hasMappedStatement(msId)) { |
| 128 | + return msId; |
| 129 | + } |
| 130 | + StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql); |
| 131 | + newSelectMappedStatement(msId, sqlSource, Map.class, SqlCommandType.SELECT); |
| 132 | + return msId; |
| 133 | + } |
| 134 | + |
| 135 | + private String selectDynamic(String sql, Class<?> parameterType) { |
| 136 | + String msId = newMsId(sql, SqlCommandType.INSERT); |
| 137 | + if (hasMappedStatement(msId)) { |
| 138 | + return msId; |
| 139 | + } |
| 140 | + SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType); |
| 141 | + newSelectMappedStatement(msId, sqlSource, Map.class, SqlCommandType.SELECT); |
| 142 | + return msId; |
| 143 | + } |
| 144 | + |
| 145 | + private String select(String sql, Class<?> resultType) { |
| 146 | + String msId = newMsId(sql, SqlCommandType.INSERT); |
| 147 | + if (hasMappedStatement(msId)) { |
| 148 | + return msId; |
| 149 | + } |
| 150 | + StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql); |
| 151 | + newSelectMappedStatement(msId, sqlSource, resultType, SqlCommandType.SELECT); |
| 152 | + return msId; |
| 153 | + } |
| 154 | + |
| 155 | + private String selectDynamic(String sql, Class<?> parameterType, Class<?> resultType) { |
| 156 | + String msId = newMsId(sql, SqlCommandType.INSERT); |
| 157 | + if (hasMappedStatement(msId)) { |
| 158 | + return msId; |
| 159 | + } |
| 160 | + SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType); |
| 161 | + newSelectMappedStatement(msId, sqlSource, resultType, SqlCommandType.SELECT); |
| 162 | + return msId; |
| 163 | + } |
| 164 | + |
| 165 | + private void newSelectMappedStatement(String msId, SqlSource sqlSource, final Class<?> resultType, SqlCommandType sqlCommandType) { |
| 166 | + MappedStatement ms = new MappedStatement.Builder(configuration, msId, sqlSource, sqlCommandType) |
| 167 | + .resultMaps(new ArrayList<ResultMap>() { |
| 168 | + { |
| 169 | + add(new ResultMap.Builder(configuration, "defaultResultMap", resultType, new ArrayList<ResultMapping>(0)).build()); |
| 170 | + } |
| 171 | + }) |
| 172 | + .build(); |
| 173 | + //缓存 |
| 174 | + configuration.addMappedStatement(ms); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * 创建一个简单的MS |
| 179 | + * |
| 180 | + * @param msId |
| 181 | + * @param sqlSource |
| 182 | + * @param sqlCommandType |
| 183 | + */ |
| 184 | + private void newUpdateMappedStatement(String msId, SqlSource sqlSource, SqlCommandType sqlCommandType) { |
| 185 | + MappedStatement ms = new MappedStatement.Builder(configuration, msId, sqlSource, sqlCommandType) |
| 186 | + .resultMaps(new ArrayList<ResultMap>() { |
| 187 | + { |
| 188 | + add(new ResultMap.Builder(configuration, "defaultResultMap", int.class, new ArrayList<ResultMapping>(0)).build()); |
| 189 | + } |
| 190 | + }) |
| 191 | + .build(); |
| 192 | + //缓存 |
| 193 | + configuration.addMappedStatement(ms); |
| 194 | + } |
| 195 | + |
| 196 | + private String insert(String sql) { |
| 197 | + String msId = newMsId(sql, SqlCommandType.INSERT); |
| 198 | + if (hasMappedStatement(msId)) { |
| 199 | + return msId; |
| 200 | + } |
| 201 | + StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql); |
| 202 | + newUpdateMappedStatement(msId, sqlSource, SqlCommandType.INSERT); |
| 203 | + return msId; |
| 204 | + } |
| 205 | + |
| 206 | + private String insertDynamic(String sql, Class<?> parameterType) { |
| 207 | + String msId = newMsId(sql, SqlCommandType.INSERT); |
| 208 | + if (hasMappedStatement(msId)) { |
| 209 | + return msId; |
| 210 | + } |
| 211 | + SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType); |
| 212 | + newUpdateMappedStatement(msId, sqlSource, SqlCommandType.INSERT); |
| 213 | + return msId; |
| 214 | + } |
| 215 | + |
| 216 | + private String update(String sql) { |
| 217 | + String msId = newMsId(sql, SqlCommandType.UPDATE); |
| 218 | + if (hasMappedStatement(msId)) { |
| 219 | + return msId; |
| 220 | + } |
| 221 | + StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql); |
| 222 | + newUpdateMappedStatement(msId, sqlSource, SqlCommandType.UPDATE); |
| 223 | + return msId; |
| 224 | + } |
| 225 | + |
| 226 | + private String updateDynamic(String sql, Class<?> parameterType) { |
| 227 | + String msId = newMsId(sql, SqlCommandType.UPDATE); |
| 228 | + if (hasMappedStatement(msId)) { |
| 229 | + return msId; |
| 230 | + } |
| 231 | + SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType); |
| 232 | + newUpdateMappedStatement(msId, sqlSource, SqlCommandType.UPDATE); |
| 233 | + return msId; |
| 234 | + } |
| 235 | + |
| 236 | + private String delete(String sql) { |
| 237 | + String msId = newMsId(sql, SqlCommandType.DELETE); |
| 238 | + if (hasMappedStatement(msId)) { |
| 239 | + return msId; |
| 240 | + } |
| 241 | + StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql); |
| 242 | + newUpdateMappedStatement(msId, sqlSource, SqlCommandType.DELETE); |
| 243 | + return msId; |
| 244 | + } |
| 245 | + |
| 246 | + private String deleteDynamic(String sql, Class<?> parameterType) { |
| 247 | + String msId = newMsId(sql, SqlCommandType.DELETE); |
| 248 | + if (hasMappedStatement(msId)) { |
| 249 | + return msId; |
| 250 | + } |
| 251 | + SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType); |
| 252 | + newUpdateMappedStatement(msId, sqlSource, SqlCommandType.DELETE); |
| 253 | + return msId; |
| 254 | + } |
| 255 | + } |
| 256 | +} |
0 commit comments