Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7929fad

Browse files
committed
增加方法可以控制是否缓存MappedStatement:
当通过前台实现执行任意SQL时,建议关闭缓存,否则每一个SQL缓存一次,最终的缓存数量会很大。 如果是业务代码中拼的sql,建议缓存提高效率。
1 parent fc7ad3a commit 7929fad

File tree

1 file changed

+49
-21
lines changed

1 file changed

+49
-21
lines changed

src/main/java/com/github/abel533/sql/SqlMapper.java

+49-21
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,41 @@
2020
public class SqlMapper {
2121
private final MSUtils msUtils;
2222
private final SqlSession sqlSession;
23+
private boolean cached;
2324

25+
/**
26+
* 构造方法,默认缓存MappedStatement
27+
*
28+
* <br>当通过前台实现执行任意SQL时,建议关闭缓存,否则每一个SQL缓存一次,最终的缓存数量会很大
29+
* <br>如果是业务代码中拼的sql,建议缓存提高效率
30+
*
31+
* @param sqlSession
32+
*/
2433
public SqlMapper(SqlSession sqlSession) {
34+
this(sqlSession, true);
35+
}
36+
37+
/**
38+
* 构造方法,可以控制是否缓存MappedStatement
39+
*
40+
* @param sqlSession
41+
* @param cached 是否缓存MappedStatement
42+
*/
43+
public SqlMapper(SqlSession sqlSession, boolean cached) {
2544
this.sqlSession = sqlSession;
45+
this.cached = cached;
2646
this.msUtils = new MSUtils(sqlSession.getConfiguration());
2747
}
2848

49+
public void setCached(boolean cached) {
50+
this.cached = cached;
51+
}
52+
2953
/**
3054
* 获取List中最多只有一个的数据
3155
*
3256
* @param list List结果
33-
* @param <T> 泛型类型
57+
* @param <T> 泛型类型
3458
* @return
3559
*/
3660
private <T> T getOne(List<T> list) {
@@ -57,7 +81,7 @@ public Map<String, Object> selectOne(String sql) {
5781
/**
5882
* 查询返回一个结果,多个结果时抛出异常
5983
*
60-
* @param sql 执行的sql
84+
* @param sql 执行的sql
6185
* @param value 参数
6286
* @return
6387
*/
@@ -69,9 +93,9 @@ public Map<String, Object> selectOne(String sql, Object value) {
6993
/**
7094
* 查询返回一个结果,多个结果时抛出异常
7195
*
72-
* @param sql 执行的sql
96+
* @param sql 执行的sql
7397
* @param resultType 返回的结果类型
74-
* @param <T> 泛型类型
98+
* @param <T> 泛型类型
7599
* @return
76100
*/
77101
public <T> T selectOne(String sql, Class<T> resultType) {
@@ -82,10 +106,10 @@ public <T> T selectOne(String sql, Class<T> resultType) {
82106
/**
83107
* 查询返回一个结果,多个结果时抛出异常
84108
*
85-
* @param sql 执行的sql
86-
* @param value 参数
109+
* @param sql 执行的sql
110+
* @param value 参数
87111
* @param resultType 返回的结果类型
88-
* @param <T> 泛型类型
112+
* @param <T> 泛型类型
89113
* @return
90114
*/
91115
public <T> T selectOne(String sql, Object value, Class<T> resultType) {
@@ -107,7 +131,7 @@ public List<Map<String, Object>> selectList(String sql) {
107131
/**
108132
* 查询返回List<Map<String, Object>>
109133
*
110-
* @param sql 执行的sql
134+
* @param sql 执行的sql
111135
* @param value 参数
112136
* @return
113137
*/
@@ -120,9 +144,9 @@ public List<Map<String, Object>> selectList(String sql, Object value) {
120144
/**
121145
* 查询返回指定的结果类型
122146
*
123-
* @param sql 执行的sql
147+
* @param sql 执行的sql
124148
* @param resultType 返回的结果类型
125-
* @param <T> 泛型类型
149+
* @param <T> 泛型类型
126150
* @return
127151
*/
128152
public <T> List<T> selectList(String sql, Class<T> resultType) {
@@ -138,10 +162,10 @@ public <T> List<T> selectList(String sql, Class<T> resultType) {
138162
/**
139163
* 查询返回指定的结果类型
140164
*
141-
* @param sql 执行的sql
142-
* @param value 参数
165+
* @param sql 执行的sql
166+
* @param value 参数
143167
* @param resultType 返回的结果类型
144-
* @param <T> 泛型类型
168+
* @param <T> 泛型类型
145169
* @return
146170
*/
147171
public <T> List<T> selectList(String sql, Object value, Class<T> resultType) {
@@ -169,7 +193,7 @@ public int insert(String sql) {
169193
/**
170194
* 插入数据
171195
*
172-
* @param sql 执行的sql
196+
* @param sql 执行的sql
173197
* @param value 参数
174198
* @return
175199
*/
@@ -193,7 +217,7 @@ public int update(String sql) {
193217
/**
194218
* 更新数据
195219
*
196-
* @param sql 执行的sql
220+
* @param sql 执行的sql
197221
* @param value 参数
198222
* @return
199223
*/
@@ -216,8 +240,8 @@ public int delete(String sql) {
216240

217241
/**
218242
* 删除数据
219-
*
220-
* @param sql 执行的sql
243+
*
244+
* @param sql 执行的sql
221245
* @param value 参数
222246
* @return
223247
*/
@@ -263,7 +287,7 @@ private boolean hasMappedStatement(String msId) {
263287
* 创建一个查询的MS
264288
*
265289
* @param msId
266-
* @param sqlSource 执行的sqlSource
290+
* @param sqlSource 执行的sqlSource
267291
* @param resultType 返回的结果类型
268292
*/
269293
private void newSelectMappedStatement(String msId, SqlSource sqlSource, final Class<?> resultType) {
@@ -275,14 +299,16 @@ private void newSelectMappedStatement(String msId, SqlSource sqlSource, final Cl
275299
})
276300
.build();
277301
//缓存
278-
configuration.addMappedStatement(ms);
302+
if (cached) {
303+
configuration.addMappedStatement(ms);
304+
}
279305
}
280306

281307
/**
282308
* 创建一个简单的MS
283309
*
284310
* @param msId
285-
* @param sqlSource 执行的sqlSource
311+
* @param sqlSource 执行的sqlSource
286312
* @param sqlCommandType 执行的sqlCommandType
287313
*/
288314
private void newUpdateMappedStatement(String msId, SqlSource sqlSource, SqlCommandType sqlCommandType) {
@@ -294,7 +320,9 @@ private void newUpdateMappedStatement(String msId, SqlSource sqlSource, SqlComma
294320
})
295321
.build();
296322
//缓存
297-
configuration.addMappedStatement(ms);
323+
if (cached) {
324+
configuration.addMappedStatement(ms);
325+
}
298326
}
299327

300328
private String select(String sql) {

0 commit comments

Comments
 (0)