Skip to content

Commit 6d3b830

Browse files
committed
增加一个SqlMapper,这样一来,通用mapper真的就不用xml就能执行所有方法了。
1 parent 55bb8f8 commit 6d3b830

File tree

5 files changed

+581
-0
lines changed

5 files changed

+581
-0
lines changed
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
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+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.github.abel533.sql.test;
2+
3+
import com.github.abel533.mapper.CountryMapper;
4+
import com.github.abel533.mapper.MybatisHelper;
5+
import com.github.abel533.model.Country;
6+
import com.github.abel533.sql.SqlMapper;
7+
import org.apache.ibatis.session.SqlSession;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
import java.util.List;
12+
13+
/**
14+
* Created by liuzh on 2015/3/10.
15+
*/
16+
public class DeleteTest {
17+
18+
@Test
19+
public void testSqlHelperDelete() {
20+
SqlSession sqlSession = MybatisHelper.getSqlSession();
21+
SqlMapper sqlMapper = new SqlMapper(sqlSession);
22+
try {
23+
CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
24+
//sqlMapper插入
25+
int result = sqlMapper.delete("delete from country where id = 35");
26+
Assert.assertEquals(1, result);
27+
//查询验证
28+
Country country = countryMapper.selectByPrimaryKey(35);
29+
Assert.assertNull(country);
30+
} finally {
31+
sqlSession.rollback();
32+
sqlSession.close();
33+
}
34+
}
35+
36+
@Test
37+
public void testSqlHelperDelete2() {
38+
SqlSession sqlSession = MybatisHelper.getSqlSession();
39+
SqlMapper sqlMapper = new SqlMapper(sqlSession);
40+
try {
41+
CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
42+
//sqlMapper插入
43+
int result = sqlMapper.delete("delete from country where id = #{id}", 35);
44+
Assert.assertEquals(1, result);
45+
//查询验证
46+
Country country = countryMapper.selectByPrimaryKey(35);
47+
Assert.assertNull(country);
48+
} finally {
49+
sqlSession.rollback();
50+
sqlSession.close();
51+
}
52+
}
53+
54+
@Test
55+
public void testSqlHelperDelete33() {
56+
SqlSession sqlSession = MybatisHelper.getSqlSession();
57+
SqlMapper sqlMapper = new SqlMapper(sqlSession);
58+
try {
59+
CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
60+
//sqlMapper插入
61+
Country tc = new Country();
62+
tc.setId(35);
63+
tc.setCountryname("天朝");
64+
tc.setCountrycode("TC");
65+
int result = sqlMapper.delete("delete from country where id = #{id}", tc);
66+
Assert.assertEquals(1, result);
67+
//查询验证
68+
Country country = countryMapper.selectByPrimaryKey(35);
69+
Assert.assertNull(country);
70+
} finally {
71+
sqlSession.rollback();
72+
sqlSession.close();
73+
}
74+
}
75+
76+
77+
@Test
78+
public void testSqlHelperDelete3() {
79+
SqlSession sqlSession = MybatisHelper.getSqlSession();
80+
SqlMapper sqlMapper = new SqlMapper(sqlSession);
81+
try {
82+
CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
83+
//sqlMapper插入
84+
Country tc = new Country();
85+
tc.setId(35);
86+
tc.setCountryname("天朝");
87+
//注意这里的countrycode和countryname故意写反的
88+
int result = sqlMapper.delete("delete from country where id in(select id from country where countryname like 'A%')", tc);
89+
Assert.assertEquals(12, result);
90+
tc = new Country();
91+
tc.setCountryname("天朝");
92+
List<Country> countryList = countryMapper.select(tc);
93+
Assert.assertEquals(0, countryList.size());
94+
} finally {
95+
sqlSession.rollback();
96+
sqlSession.close();
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)