Skip to content

Commit 34b9daf

Browse files
committed
fixed
1 parent 72e1dc0 commit 34b9daf

File tree

9 files changed

+146
-98
lines changed

9 files changed

+146
-98
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,15 @@ http://repo1.maven.org/maven2/javax/persistence/persistence-api/1.0/
108108

109109
##Maven坐标以及下载地址
110110

111-
###测试版本3.1.3-SNAPSHOT - 2015-07-14
111+
###测试版本3.1.3-SNAPSHOT- 2015-08-02
112112

113113
* 新增`MapperOnceInterceptor`拦截器,该拦截器执行后会自动卸载,以后不会再进入该方法,只需要将`MapperInterceptor`替换为`MapperOnceInterceptor`即可
114-
* 由于`MapperHelper`的配置方式容易出现错误,因此从3.1.3以后废弃这种配置方法
114+
* 大家可以尝试`MapperOnceInterceptor`拦截器,发现问题可以提issue
115+
* 由于Spring中的`MapperHelper`的配置方式容易出现错误,因此从3.1.3以后废弃这种配置方法
115116
* `Example`增加`andEqualTo(实体对象)`方法,可以将一个实体放进去,会自动根据属性和值拼出column=value的条件 <b>Bob - 0haizhu0@gmail.com 提供</b>
117+
* MyBatis在处理`<cache/>``@CacheNamespace`的时候不统一,只有一个能生效,这导致xml中配置二级缓存对通用Mapper注解形式的方法无效,该问题已解决
118+
* 二级缓存配置方法,如果接口有对应的xml,在xml中配置二级缓存。如果只有接口没有xml,用注解配置二级缓存即可
119+
* 需要注意的是,二级缓存在xml配置时,只对通用Mapper方法有效,自己用`@Select`等注解定义的这种仍然无效,这种情况只能在xml中定义
116120

117121
###最新版本3.1.2 - 2015-07-14
118122

src/main/java/tk/mybatis/mapper/mapperhelper/EntityHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static String getSelectColumns(Class<?> entityClass) {
128128
if (entityColumn.getColumn().substring(1, entityColumn.getColumn().length() - 1).equalsIgnoreCase(entityColumn.getProperty())) {
129129
selectBuilder.append(",");
130130
} else {
131-
selectBuilder.append(" ").append(entityColumn.getProperty()).append(",");
131+
selectBuilder.append(" AS ").append(entityColumn.getProperty()).append(",");
132132
}
133133
} else {
134134
selectBuilder.append(",");

src/main/java/tk/mybatis/mapper/mapperhelper/MapperTemplate.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package tk.mybatis.mapper.mapperhelper;
2626

27+
import org.apache.ibatis.cache.Cache;
2728
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
2829
import org.apache.ibatis.executor.keygen.KeyGenerator;
2930
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
@@ -127,6 +128,29 @@ protected void setSqlSource(MappedStatement ms, SqlSource sqlSource) {
127128
}
128129
}
129130

131+
/**
132+
* check ms cache
133+
*
134+
* @param ms
135+
* @throws Exception
136+
*/
137+
private void checkCache(MappedStatement ms) throws Exception {
138+
if(ms.getCache() == null){
139+
String nameSpace = ms.getId().substring(0, ms.getId().lastIndexOf("."));
140+
Cache cache = null;
141+
try {
142+
//不存在的时候会抛出异常
143+
cache = ms.getConfiguration().getCache(nameSpace);
144+
} catch (IllegalArgumentException e) {
145+
return;
146+
}
147+
if(cache != null){
148+
MetaObject metaObject = SystemMetaObject.forObject(ms);
149+
metaObject.setValue("cache", cache);
150+
}
151+
}
152+
}
153+
130154
/**
131155
* 重新设置SqlSource
132156
*
@@ -163,6 +187,8 @@ public void setSqlSource(MappedStatement ms) throws Exception {
163187
} else {
164188
throw new RuntimeException("自定义Mapper方法返回类型错误,可选的返回类型为void,SqlNode,String三种!");
165189
}
190+
//cache
191+
checkCache(ms);
166192
} catch (IllegalAccessException e) {
167193
throw new RuntimeException(e);
168194
} catch (InvocationTargetException e) {

src/test/java/tk/mybatis/mapper/mapper/CachedCountryMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
/**
3333
* Created by liuzh on 2014/11/19.
3434
*/
35-
@CacheNamespace
35+
//@CacheNamespace
3636
public interface CachedCountryMapper extends Mapper<Country>,HsqldbMapper<Country> {
37+
int selectCache(int id);
3738
}

src/test/java/tk/mybatis/mapper/test/country/TestCache.java

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public void testCache() {
4747
country.setCountrycode("CN");
4848
//第一次查询,会被缓存
4949
country = mapper.selectOne(country);
50-
Assert.assertEquals(true, country.getId() == 35);
51-
Assert.assertEquals("China", country.getCountryname());
5250
//只有close才会真正缓存,而不是用一级缓存
5351
sqlSession.close();
5452

@@ -59,44 +57,26 @@ public void testCache() {
5957
country.setCountrycode("CN");
6058
//第二次查询,会使用第一次的缓存
6159
country = mapper.selectOne(country);
62-
63-
Assert.assertEquals(true, country.getId() == 35);
64-
Assert.assertEquals("China", country.getCountryname());
6560
//只有close才会真正缓存,而不是用一级缓存
6661
sqlSession.close();
62+
} finally {
63+
sqlSession.close();
64+
}
65+
}
6766

68-
//======================================================================
69-
sqlSession = MybatisHelper.getSqlSession();
70-
mapper = sqlSession.getMapper(CachedCountryMapper.class);
71-
72-
country = new Country();
73-
country.setCountryname("天朝");
74-
country.setId(35);
75-
//更新操作会清空缓存
76-
int result = mapper.updateByPrimaryKeySelective(country);
77-
Assert.assertEquals(1, result);
78-
sqlSession.commit();
67+
@Test
68+
public void testCache2() {
69+
SqlSession sqlSession = MybatisHelper.getSqlSession();
70+
try {
71+
//第一次查询,会被缓存
72+
sqlSession.selectOne("selectCache", 35);
7973
//只有close才会真正缓存,而不是用一级缓存
8074
sqlSession.close();
8175

8276
//======================================================================
8377
sqlSession = MybatisHelper.getSqlSession();
84-
mapper = sqlSession.getMapper(CachedCountryMapper.class);
85-
country = new Country();
86-
country.setCountrycode("CN");
87-
//第三次查询,会重新查询,不使用缓存
88-
country = mapper.selectOne(country);
89-
90-
Assert.assertEquals(true, country.getId() == 35);
91-
Assert.assertEquals("天朝", country.getCountryname());
92-
93-
country = new Country();
94-
country.setCountryname("China");
95-
country.setId(35);
96-
//还原
97-
result = mapper.updateByPrimaryKeySelective(country);
98-
sqlSession.commit();
99-
Assert.assertEquals(1, result);
78+
sqlSession.selectOne("selectCache", 35);
79+
sqlSession.close();
10080
} finally {
10181
sqlSession.close();
10282
}

src/test/java/tk/mybatis/mapper/test/jdbc/TestJDBC.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.junit.Assert;
3232

3333
/**
34-
* sqlserver测试
34+
* sqlserver测试 - 该类注释所有测试方法是因为该测试是针对sqlserver的,而项目测试用的hsqldb,所以这些测试不能运行,需要换为sqlserver才可以
3535
*/
3636
public class TestJDBC {
3737

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ The MIT License (MIT)
4+
~
5+
~ Copyright (c) 2014 abel533@gmail.com
6+
~
7+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
8+
~ of this software and associated documentation files (the "Software"), to deal
9+
~ in the Software without restriction, including without limitation the rights
10+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
~ copies of the Software, and to permit persons to whom the Software is
12+
~ furnished to do so, subject to the following conditions:
13+
~
14+
~ The above copyright notice and this permission notice shall be included in
15+
~ all copies or substantial portions of the Software.
16+
~
17+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
~ THE SOFTWARE.
24+
-->
25+
26+
<!DOCTYPE mapper
27+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
28+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
29+
30+
<mapper namespace="tk.mybatis.mapper.mapper.CachedCountryMapper">
31+
<cache/>
32+
<select id="selectCache" resultType="int">
33+
select * from country where id = #{id}
34+
</select>
35+
</mapper>

src/test/resources/mybatis-config.xml

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,72 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<!DOCTYPE configuration
3-
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4-
"http://mybatis.org/dtd/mybatis-3-config.dtd">
3+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
55

66
<configuration>
7-
<settings>
8-
<setting name="cacheEnabled" value="true"/>
9-
<setting name="lazyLoadingEnabled" value="false"/>
10-
<setting name="aggressiveLazyLoading" value="true"/>
11-
<setting name="logImpl" value="LOG4J"/>
12-
</settings>
7+
<settings>
8+
<setting name="cacheEnabled" value="true"/>
9+
<setting name="lazyLoadingEnabled" value="false"/>
10+
<setting name="aggressiveLazyLoading" value="true"/>
11+
<setting name="logImpl" value="LOG4J"/>
12+
</settings>
1313

14-
<typeAliases>
15-
<package name="tk.mybatis.mapper.model"/>
16-
</typeAliases>
14+
<typeAliases>
15+
<package name="tk.mybatis.mapper.model"/>
16+
</typeAliases>
1717

18-
<plugins>
18+
<plugins>
1919
<plugin interceptor="tk.mybatis.mapper.mapperhelper.MapperOnceInterceptor">
20-
<!--================================================-->
21-
<!--可配置参数说明(一般无需修改)-->
22-
<!--================================================-->
23-
<!--UUID生成策略-->
24-
<!--配置UUID生成策略需要使用OGNL表达式-->
25-
<!--默认值32位长度:@java.util.UUID@randomUUID().toString().replace("-", "")-->
26-
<!--<property name="UUID" value="@java.util.UUID@randomUUID().toString()"/>-->
27-
<!--主键自增回写方法,默认值MYSQL,详细说明请看文档-->
28-
<property name="IDENTITY" value="HSQLDB"/>
29-
<!--序列的获取规则,使用{num}格式化参数,默认值为{0}.nextval,针对Oracle-->
30-
<!--可选参数一共3个,对应0,1,2,分别为SequenceName,ColumnName,PropertyName-->
31-
<property name="seqFormat" value="{0}.nextval"/>
32-
<!-- 设置全局的catalog,默认为空,如果设置了值,操作表时的sql会是catalog.tablename -->
33-
<property name="catalog" value=""/>
34-
<!-- 设置全局的schema,默认为空,如果设置了值,操作表时的sql会是schema.tablename -->
35-
<!-- 如果同时设置了catalog,优先使用catalog.tablename -->
36-
<property name="schema" value=""/>
37-
<!--主键自增回写方法执行顺序,默认AFTER,可选值为(BEFORE|AFTER)-->
38-
<property name="ORDER" value="AFTER"/>
39-
<!--通用Mapper接口,多个用逗号隔开-->
40-
<property name="mappers" value="tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.hsqldb.HsqldbMapper"/>
41-
</plugin>
42-
</plugins>
20+
<!--================================================-->
21+
<!--可配置参数说明(一般无需修改)-->
22+
<!--================================================-->
23+
<!--UUID生成策略-->
24+
<!--配置UUID生成策略需要使用OGNL表达式-->
25+
<!--默认值32位长度:@java.util.UUID@randomUUID().toString().replace("-", "")-->
26+
<!--<property name="UUID" value="@java.util.UUID@randomUUID().toString()"/>-->
27+
<!--主键自增回写方法,默认值MYSQL,详细说明请看文档-->
28+
<property name="IDENTITY" value="HSQLDB"/>
29+
<!--序列的获取规则,使用{num}格式化参数,默认值为{0}.nextval,针对Oracle-->
30+
<!--可选参数一共3个,对应0,1,2,分别为SequenceName,ColumnName,PropertyName-->
31+
<property name="seqFormat" value="{0}.nextval"/>
32+
<!-- 设置全局的catalog,默认为空,如果设置了值,操作表时的sql会是catalog.tablename -->
33+
<property name="catalog" value=""/>
34+
<!-- 设置全局的schema,默认为空,如果设置了值,操作表时的sql会是schema.tablename -->
35+
<!-- 如果同时设置了catalog,优先使用catalog.tablename -->
36+
<property name="schema" value=""/>
37+
<!--主键自增回写方法执行顺序,默认AFTER,可选值为(BEFORE|AFTER)-->
38+
<property name="ORDER" value="AFTER"/>
39+
<!--通用Mapper接口,多个用逗号隔开-->
40+
<property name="mappers" value="tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.hsqldb.HsqldbMapper"/>
41+
</plugin>
42+
</plugins>
4343

44-
<environments default="development">
45-
<environment id="development">
46-
<transactionManager type="JDBC">
47-
<property name="" value="" />
48-
</transactionManager>
49-
<dataSource type="UNPOOLED">
50-
<property name="driver" value="org.hsqldb.jdbcDriver" />
51-
<property name="url" value="jdbc:hsqldb:mem:basetest" />
52-
<property name="username" value="sa" />
53-
</dataSource>
54-
</environment>
55-
</environments>
44+
<environments default="development">
45+
<environment id="development">
46+
<transactionManager type="JDBC">
47+
<property name="" value=""/>
48+
</transactionManager>
49+
<dataSource type="UNPOOLED">
50+
<property name="driver" value="org.hsqldb.jdbcDriver"/>
51+
<property name="url" value="jdbc:hsqldb:mem:basetest"/>
52+
<property name="username" value="sa"/>
53+
</dataSource>
54+
</environment>
55+
</environments>
5656

57-
<mappers>
58-
<mapper class="tk.mybatis.mapper.mapper.CountryMapper" />
59-
<mapper class="tk.mybatis.mapper.mapper.Country2Mapper" />
60-
<mapper class="tk.mybatis.mapper.mapper.CountryTMapper" />
61-
<mapper class="tk.mybatis.mapper.mapper.CountryUMapper" />
62-
<mapper class="tk.mybatis.mapper.mapper.CountryJDBCMapper" />
63-
<mapper class="tk.mybatis.mapper.mapper.CountryIMapper" />
64-
<mapper class="tk.mybatis.mapper.mapper.UserInfoMapper" />
65-
<mapper class="tk.mybatis.mapper.mapper.UserInfoMapMapper" />
66-
<mapper class="tk.mybatis.mapper.mapper.UserLoginMapper" />
67-
<mapper class="tk.mybatis.mapper.mapper.UserLogin2Mapper" />
68-
<mapper class="tk.mybatis.mapper.mapper.CachedCountryMapper"/>
69-
</mappers>
57+
<mappers>
58+
<mapper resource="CachedCountryMapper.xml"/>
59+
<mapper class="tk.mybatis.mapper.mapper.CountryMapper"/>
60+
<mapper class="tk.mybatis.mapper.mapper.Country2Mapper"/>
61+
<mapper class="tk.mybatis.mapper.mapper.CountryTMapper"/>
62+
<mapper class="tk.mybatis.mapper.mapper.CountryUMapper"/>
63+
<mapper class="tk.mybatis.mapper.mapper.CountryJDBCMapper"/>
64+
<mapper class="tk.mybatis.mapper.mapper.CountryIMapper"/>
65+
<mapper class="tk.mybatis.mapper.mapper.UserInfoMapper"/>
66+
<mapper class="tk.mybatis.mapper.mapper.UserInfoMapMapper"/>
67+
<mapper class="tk.mybatis.mapper.mapper.UserLoginMapper"/>
68+
<mapper class="tk.mybatis.mapper.mapper.UserLogin2Mapper"/>
69+
<!--<mapper class="tk.mybatis.mapper.mapper.CachedCountryMapper"/>-->
70+
</mappers>
7071

7172
</configuration>

src/test/resources/mybatis-java.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
</environments>
3838

3939
<mappers>
40+
<mapper resource="CachedCountryMapper.xml"/>
4041
<mapper class="tk.mybatis.mapper.mapper.CountryMapper"/>
41-
<mapper class="tk.mybatis.mapper.mapper.CachedCountryMapper"/>
42+
<!--<mapper class="tk.mybatis.mapper.mapper.CachedCountryMapper"/>-->
4243
<mapper class="tk.mybatis.mapper.mapper.Country2Mapper"/>
4344
<mapper class="tk.mybatis.mapper.mapper.CountryTMapper"/>
4445
<mapper class="tk.mybatis.mapper.mapper.CountryUMapper"/>

0 commit comments

Comments
 (0)