Skip to content

Commit 183c8ad

Browse files
committed
'spring4版本'
1 parent e606e5b commit 183c8ad

File tree

11 files changed

+140
-178
lines changed

11 files changed

+140
-178
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
<dependency>
174174
<groupId>org.springframework</groupId>
175175
<artifactId>spring-framework-bom</artifactId>
176-
<version>3.2.12.RELEASE</version>
176+
<version>4.1.2.RELEASE</version>
177177
<type>pom</type>
178178
<scope>import</scope>
179179
</dependency>

src/main/java/com/isea533/mybatis/controller/demo/DemoController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.isea533.mybatis.controller.demo;
22

33
import com.isea533.mybatis.model.Country;
4+
import com.isea533.mybatis.model.Country2;
45
import com.isea533.mybatis.service.DemoService;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.http.HttpEntity;
@@ -40,10 +41,10 @@ public ResponseEntity<Country> requestTest6() {
4041

4142
@ResponseBody
4243
@RequestMapping("test3")
43-
public List<Country> requestTest7(
44+
public List<Country2> requestTest7(
4445
@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
4546
@RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize
4647
) {
47-
return demoService.selectPage(pageNum, pageSize);
48+
return demoService.selectPage2(pageNum, pageSize);
4849
}
4950
}
Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.isea533.mybatis.mapper;
22

3+
import com.isea533.mybatis.mapperhelper.Mapper;
34
import com.isea533.mybatis.model.Country2;
45
import com.isea533.mybatis.model.Country2Example;
5-
import java.util.List;
66
import org.apache.ibatis.annotations.Param;
77

8-
public interface Country2Mapper {
8+
import java.util.List;
9+
10+
public interface Country2Mapper extends Mapper<Country2> {
911
/**
1012
* This method was generated by MyBatis Generator.
1113
* This method corresponds to the database table country
@@ -22,30 +24,6 @@ public interface Country2Mapper {
2224
*/
2325
int deleteByExample(Country2Example example);
2426

25-
/**
26-
* This method was generated by MyBatis Generator.
27-
* This method corresponds to the database table country
28-
*
29-
* @mbggenerated Mon Dec 08 11:25:39 CST 2014
30-
*/
31-
int deleteByPrimaryKey(Integer id);
32-
33-
/**
34-
* This method was generated by MyBatis Generator.
35-
* This method corresponds to the database table country
36-
*
37-
* @mbggenerated Mon Dec 08 11:25:39 CST 2014
38-
*/
39-
int insert(Country2 record);
40-
41-
/**
42-
* This method was generated by MyBatis Generator.
43-
* This method corresponds to the database table country
44-
*
45-
* @mbggenerated Mon Dec 08 11:25:39 CST 2014
46-
*/
47-
int insertSelective(Country2 record);
48-
4927
/**
5028
* This method was generated by MyBatis Generator.
5129
* This method corresponds to the database table country
@@ -54,14 +32,6 @@ public interface Country2Mapper {
5432
*/
5533
List<Country2> selectByExample(Country2Example example);
5634

57-
/**
58-
* This method was generated by MyBatis Generator.
59-
* This method corresponds to the database table country
60-
*
61-
* @mbggenerated Mon Dec 08 11:25:39 CST 2014
62-
*/
63-
Country2 selectByPrimaryKey(Integer id);
64-
6535
/**
6636
* This method was generated by MyBatis Generator.
6737
* This method corresponds to the database table country
@@ -77,20 +47,4 @@ public interface Country2Mapper {
7747
* @mbggenerated Mon Dec 08 11:25:39 CST 2014
7848
*/
7949
int updateByExample(@Param("record") Country2 record, @Param("example") Country2Example example);
80-
81-
/**
82-
* This method was generated by MyBatis Generator.
83-
* This method corresponds to the database table country
84-
*
85-
* @mbggenerated Mon Dec 08 11:25:39 CST 2014
86-
*/
87-
int updateByPrimaryKeySelective(Country2 record);
88-
89-
/**
90-
* This method was generated by MyBatis Generator.
91-
* This method corresponds to the database table country
92-
*
93-
* @mbggenerated Mon Dec 08 11:25:39 CST 2014
94-
*/
95-
int updateByPrimaryKey(Country2 record);
9650
}

src/main/java/com/isea533/mybatis/mapper/Country2Mapper.xml

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -106,26 +106,6 @@
106106
order by ${orderByClause}
107107
</if>
108108
</select>
109-
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
110-
<!--
111-
WARNING - @mbggenerated
112-
This element is automatically generated by MyBatis Generator, do not modify.
113-
This element was generated on Mon Dec 08 11:25:39 CST 2014.
114-
-->
115-
select
116-
<include refid="Base_Column_List" />
117-
from country
118-
where id = #{id,jdbcType=INTEGER}
119-
</select>
120-
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
121-
<!--
122-
WARNING - @mbggenerated
123-
This element is automatically generated by MyBatis Generator, do not modify.
124-
This element was generated on Mon Dec 08 11:25:39 CST 2014.
125-
-->
126-
delete from country
127-
where id = #{id,jdbcType=INTEGER}
128-
</delete>
129109
<delete id="deleteByExample" parameterType="com.isea533.mybatis.model.Country2Example" >
130110
<!--
131111
WARNING - @mbggenerated
@@ -137,47 +117,6 @@
137117
<include refid="Example_Where_Clause" />
138118
</if>
139119
</delete>
140-
<insert id="insert" parameterType="com.isea533.mybatis.model.Country2" >
141-
<!--
142-
WARNING - @mbggenerated
143-
This element is automatically generated by MyBatis Generator, do not modify.
144-
This element was generated on Mon Dec 08 11:25:39 CST 2014.
145-
-->
146-
insert into country (id, countryname, countrycode
147-
)
148-
values (#{id,jdbcType=INTEGER}, #{countryname,jdbcType=VARCHAR}, #{countrycode,jdbcType=VARCHAR}
149-
)
150-
</insert>
151-
<insert id="insertSelective" parameterType="com.isea533.mybatis.model.Country2" >
152-
<!--
153-
WARNING - @mbggenerated
154-
This element is automatically generated by MyBatis Generator, do not modify.
155-
This element was generated on Mon Dec 08 11:25:39 CST 2014.
156-
-->
157-
insert into country
158-
<trim prefix="(" suffix=")" suffixOverrides="," >
159-
<if test="id != null" >
160-
id,
161-
</if>
162-
<if test="countryname != null" >
163-
countryname,
164-
</if>
165-
<if test="countrycode != null" >
166-
countrycode,
167-
</if>
168-
</trim>
169-
<trim prefix="values (" suffix=")" suffixOverrides="," >
170-
<if test="id != null" >
171-
#{id,jdbcType=INTEGER},
172-
</if>
173-
<if test="countryname != null" >
174-
#{countryname,jdbcType=VARCHAR},
175-
</if>
176-
<if test="countrycode != null" >
177-
#{countrycode,jdbcType=VARCHAR},
178-
</if>
179-
</trim>
180-
</insert>
181120
<select id="countByExample" parameterType="com.isea533.mybatis.model.Country2Example" resultType="java.lang.Integer" >
182121
<!--
183122
WARNING - @mbggenerated
@@ -225,32 +164,4 @@
225164
<include refid="Update_By_Example_Where_Clause" />
226165
</if>
227166
</update>
228-
<update id="updateByPrimaryKeySelective" parameterType="com.isea533.mybatis.model.Country2" >
229-
<!--
230-
WARNING - @mbggenerated
231-
This element is automatically generated by MyBatis Generator, do not modify.
232-
This element was generated on Mon Dec 08 11:25:39 CST 2014.
233-
-->
234-
update country
235-
<set >
236-
<if test="countryname != null" >
237-
countryname = #{countryname,jdbcType=VARCHAR},
238-
</if>
239-
<if test="countrycode != null" >
240-
countrycode = #{countrycode,jdbcType=VARCHAR},
241-
</if>
242-
</set>
243-
where id = #{id,jdbcType=INTEGER}
244-
</update>
245-
<update id="updateByPrimaryKey" parameterType="com.isea533.mybatis.model.Country2" >
246-
<!--
247-
WARNING - @mbggenerated
248-
This element is automatically generated by MyBatis Generator, do not modify.
249-
This element was generated on Mon Dec 08 11:25:39 CST 2014.
250-
-->
251-
update country
252-
set countryname = #{countryname,jdbcType=VARCHAR},
253-
countrycode = #{countrycode,jdbcType=VARCHAR}
254-
where id = #{id,jdbcType=INTEGER}
255-
</update>
256167
</mapper>

src/main/java/com/isea533/mybatis/mapperhelper/MapperHelper.java

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ public class MapperHelper {
6363
*/
6464
private Set<Collection<MappedStatement>> collectionSet = new HashSet<Collection<MappedStatement>>();
6565

66+
/**
67+
* 是否使用的Spring
68+
*/
69+
private boolean spring = false;
70+
71+
/**
72+
* 是否为Spring4.x以上版本
73+
*/
74+
private boolean spring4 = false;
75+
76+
/**
77+
* Spring版本号
78+
*/
79+
private String springVersion;
80+
6681
/**
6782
* 默认构造方法
6883
*/
@@ -88,21 +103,73 @@ public MapperHelper(Properties properties) {
88103
*
89104
* @param sqlSessions
90105
*/
91-
public void setSqlSessions(SqlSession[] sqlSessions){
92-
if (sqlSessions != null && sqlSessions.length>0) {
106+
public void setSqlSessions(SqlSession[] sqlSessions) {
107+
if (sqlSessions != null && sqlSessions.length > 0) {
93108
this.sqlSessions.addAll(Arrays.asList(sqlSessions));
94109
}
95110
}
96111

97112
/**
98113
* Spring初始化方法,使用Spring时需要配置init-method="initMapper"
99114
*/
100-
public void initMapper(){
115+
public void initMapper() {
116+
//只有Spring会执行这个方法,所以Spring配置的时候,从这儿可以尝试获取Spring的版本
117+
//先判断Spring版本,对下面的操作有影响
118+
//Spring4以上支持泛型注入,因此可以扫描通用Mapper
119+
initSpringVersion();
101120
for (SqlSession sqlSession : sqlSessions) {
102121
processConfiguration(sqlSession.getConfiguration());
103122
}
104123
}
105124

125+
/**
126+
* 检测Spring版本号,Spring4.x以上支持泛型注入
127+
*/
128+
private void initSpringVersion() {
129+
try {
130+
//反射获取SpringVersion
131+
Class<?> springVersionClass = Class.forName("org.springframework.core.SpringVersion");
132+
springVersion = (String) springVersionClass.getDeclaredMethod("getVersion", null).invoke(null, null);
133+
spring = true;
134+
if (springVersion.indexOf(".") > 0) {
135+
int MajorVersion = Integer.parseInt(springVersion.substring(0, springVersion.indexOf(".")));
136+
if (MajorVersion > 3) {
137+
spring4 = true;
138+
}
139+
}
140+
} catch (Exception e) {
141+
spring = false;
142+
spring4 = false;
143+
}
144+
}
145+
146+
/**
147+
* 是否为Spring4.x以上版本
148+
*
149+
* @return
150+
*/
151+
public boolean isSpring4() {
152+
return spring4;
153+
}
154+
155+
/**
156+
* 是否为Spring4.x以上版本
157+
*
158+
* @return
159+
*/
160+
public boolean isSpring() {
161+
return spring;
162+
}
163+
164+
/**
165+
* 获取Spring版本号
166+
*
167+
* @return
168+
*/
169+
public String getSpringVersion(){
170+
return springVersion;
171+
}
172+
106173
/**
107174
* 通过通用Mapper接口获取对应的MapperTemplate
108175
*
@@ -263,6 +330,7 @@ private class Config {
263330

264331
/**
265332
* 设置UUID
333+
*
266334
* @param UUID
267335
*/
268336
public void setUUID(String UUID) {
@@ -285,6 +353,7 @@ public void setIDENTITY(String IDENTITY) {
285353

286354
/**
287355
* 设置selectKey方法的ORDER,默认AFTER
356+
*
288357
* @param order
289358
*/
290359
public void setOrder(String order) {
@@ -296,7 +365,7 @@ public void setOrder(String order) {
296365
*
297366
* @param seqFormat
298367
*/
299-
public void setSeqFormat(String seqFormat){
368+
public void setSeqFormat(String seqFormat) {
300369
config.seqFormat = seqFormat;
301370
}
302371

@@ -305,7 +374,7 @@ public void setSeqFormat(String seqFormat){
305374
*
306375
* @param catalog
307376
*/
308-
public void setCatalog(String catalog){
377+
public void setCatalog(String catalog) {
309378
config.catalog = catalog;
310379
}
311380

@@ -314,7 +383,7 @@ public void setCatalog(String catalog){
314383
*
315384
* @param schema
316385
*/
317-
public void setSchema(String schema){
386+
public void setSchema(String schema) {
318387
config.schema = schema;
319388
}
320389

src/main/java/com/isea533/mybatis/mapperhelper/MapperInterceptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public Object intercept(Invocation invocation) throws Throwable {
5959
mapperHelper.setSqlSource(ms);
6060
}
6161
}
62-
return invocation.proceed();
62+
Object result = invocation.proceed();
63+
return result;
6364
}
6465

6566
@Override

0 commit comments

Comments
 (0)