Skip to content

Commit d696c4e

Browse files
committed
修复不一致的地方,msId唯一
1 parent 9c58cdd commit d696c4e

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed

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

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -122,77 +122,84 @@ private boolean hasMappedStatement(String msId) {
122122
return configuration.hasStatement(msId, false);
123123
}
124124

125+
/**
126+
* 创建一个查询的MS
127+
*
128+
* @param msId
129+
* @param sqlSource
130+
* @param resultType
131+
*/
132+
private void newSelectMappedStatement(String msId, SqlSource sqlSource, final Class<?> resultType) {
133+
MappedStatement ms = new MappedStatement.Builder(configuration, msId, sqlSource, SqlCommandType.SELECT)
134+
.resultMaps(new ArrayList<ResultMap>() {
135+
{
136+
add(new ResultMap.Builder(configuration, "defaultResultMap", resultType, new ArrayList<ResultMapping>(0)).build());
137+
}
138+
})
139+
.build();
140+
//缓存
141+
configuration.addMappedStatement(ms);
142+
}
143+
144+
/**
145+
* 创建一个简单的MS
146+
*
147+
* @param msId
148+
* @param sqlSource
149+
* @param sqlCommandType
150+
*/
151+
private void newUpdateMappedStatement(String msId, SqlSource sqlSource, SqlCommandType sqlCommandType) {
152+
MappedStatement ms = new MappedStatement.Builder(configuration, msId, sqlSource, sqlCommandType)
153+
.resultMaps(new ArrayList<ResultMap>() {
154+
{
155+
add(new ResultMap.Builder(configuration, "defaultResultMap", int.class, new ArrayList<ResultMapping>(0)).build());
156+
}
157+
})
158+
.build();
159+
//缓存
160+
configuration.addMappedStatement(ms);
161+
}
162+
125163
private String select(String sql) {
126-
String msId = newMsId(sql, SqlCommandType.INSERT);
164+
String msId = newMsId(sql, SqlCommandType.SELECT);
127165
if (hasMappedStatement(msId)) {
128166
return msId;
129167
}
130168
StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);
131-
newSelectMappedStatement(msId, sqlSource, Map.class, SqlCommandType.SELECT);
169+
newSelectMappedStatement(msId, sqlSource, Map.class);
132170
return msId;
133171
}
134172

135173
private String selectDynamic(String sql, Class<?> parameterType) {
136-
String msId = newMsId(sql, SqlCommandType.INSERT);
174+
String msId = newMsId(sql + parameterType, SqlCommandType.SELECT);
137175
if (hasMappedStatement(msId)) {
138176
return msId;
139177
}
140178
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType);
141-
newSelectMappedStatement(msId, sqlSource, Map.class, SqlCommandType.SELECT);
179+
newSelectMappedStatement(msId, sqlSource, Map.class);
142180
return msId;
143181
}
144182

145183
private String select(String sql, Class<?> resultType) {
146-
String msId = newMsId(sql, SqlCommandType.INSERT);
184+
String msId = newMsId(resultType + sql, SqlCommandType.SELECT);
147185
if (hasMappedStatement(msId)) {
148186
return msId;
149187
}
150188
StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);
151-
newSelectMappedStatement(msId, sqlSource, resultType, SqlCommandType.SELECT);
189+
newSelectMappedStatement(msId, sqlSource, resultType);
152190
return msId;
153191
}
154192

155193
private String selectDynamic(String sql, Class<?> parameterType, Class<?> resultType) {
156-
String msId = newMsId(sql, SqlCommandType.INSERT);
194+
String msId = newMsId(resultType + sql + parameterType, SqlCommandType.SELECT);
157195
if (hasMappedStatement(msId)) {
158196
return msId;
159197
}
160198
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType);
161-
newSelectMappedStatement(msId, sqlSource, resultType, SqlCommandType.SELECT);
199+
newSelectMappedStatement(msId, sqlSource, resultType);
162200
return msId;
163201
}
164202

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-
196203
private String insert(String sql) {
197204
String msId = newMsId(sql, SqlCommandType.INSERT);
198205
if (hasMappedStatement(msId)) {
@@ -204,7 +211,7 @@ private String insert(String sql) {
204211
}
205212

206213
private String insertDynamic(String sql, Class<?> parameterType) {
207-
String msId = newMsId(sql, SqlCommandType.INSERT);
214+
String msId = newMsId(sql + parameterType, SqlCommandType.INSERT);
208215
if (hasMappedStatement(msId)) {
209216
return msId;
210217
}
@@ -224,7 +231,7 @@ private String update(String sql) {
224231
}
225232

226233
private String updateDynamic(String sql, Class<?> parameterType) {
227-
String msId = newMsId(sql, SqlCommandType.UPDATE);
234+
String msId = newMsId(sql + parameterType, SqlCommandType.UPDATE);
228235
if (hasMappedStatement(msId)) {
229236
return msId;
230237
}
@@ -244,7 +251,7 @@ private String delete(String sql) {
244251
}
245252

246253
private String deleteDynamic(String sql, Class<?> parameterType) {
247-
String msId = newMsId(sql, SqlCommandType.DELETE);
254+
String msId = newMsId(sql + parameterType, SqlCommandType.DELETE);
248255
if (hasMappedStatement(msId)) {
249256
return msId;
250257
}

0 commit comments

Comments
 (0)