先看《spring 集成mybatis》和《Mybatis一级缓存》,在此基础上进行修改。
集成了spring之后,似乎没有办法直接使用一级缓存了,但是二级缓存还是可以的。
一、实体类
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = -8081225022334946487L;
private int id;
private String name;
private Date birthday;
private double salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", birthday=" + birthday
+ ", salary=" + salary + "]";
}
}
同样要实现Serializable接口。
二、userMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.UserMapper">
<cache/>
<resultMap type="User" id="userResult">
<result column="user_id" property="id"/>
<result column="user_name" property="name"/>
<result column="user_birthday" property="birthday"/>
<result column="user_salary" property="salary"/>
</resultMap>
<!-- 取得插入数据后的 id -->
<insert id="save" keyColumn="user_id" keyProperty="id" useGeneratedKeys="true">
insert into s_user(user_name,user_birthday,user_salary)
values(#{name},#{birthday},#{salary})
</insert>
<update id="update">
update s_user
set user_name = #{name},
user_birthday = #{birthday},
user_salary = #{salary}
where user_id = #{id}
</update>
<delete id="delete">
delete from s_user
where user_id = #{id}
</delete>
<select id="findById" resultMap="userResult">
select *
from s_user
where user_id = #{id}
</select>
<select id="findAll" resultMap="userResult">
select *
from s_user
</select>
</mapper>
同样加上<cache/>就可以了。
三、测试方法
@Test
public void findAllWithCache1() {
User user = userMapper.findById(1);
System.out.println(user);
user = userMapper.findById(1);
System.out.println(user);
}
执行日志:
2015-05-09 18:07:11,062 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
2015-05-09 18:07:11,078 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1425f38] was not registered for synchronization because synchronization is not active
2015-05-09 18:07:11,156 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2015-05-09 18:07:11,156 [main] DEBUG [org.springframework.jdbc.datasource.DriverManagerDataSource] - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/mybatis]
2015-05-09 18:07:11,171 [main] DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.mysql.jdbc.JDBC4Connection@a0afab] will not be managed by Spring
2015-05-09 18:07:11,171 [main] DEBUG [com.mapper.UserMapper.findById] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@a0afab]
2015-05-09 18:07:11,187 [main] DEBUG [com.mapper.UserMapper.findById] - ==> Preparing: select * from s_user where user_id = ?
2015-05-09 18:07:11,234 [main] DEBUG [com.mapper.UserMapper.findById] - ==> Parameters: 1(Integer)
2015-05-09 18:07:11,296 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1425f38]
2015-05-09 18:07:11,296 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
User [id=1, name=marry, birthday=Sat Apr 18 00:00:00 CST 2015, salary=2000.0]
2015-05-09 18:07:11,296 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
2015-05-09 18:07:11,296 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1905742] was not registered for synchronization because synchronization is not active
2015-05-09 18:07:11,312 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1905742]
User [id=1, name=marry, birthday=Sat Apr 18 00:00:00 CST 2015, salary=2000.0]
可以看到,spring默认在查询完就关闭了SqlSession,所以想要用一级缓存就很难了。
然后也看到第二次查询并没有去获取JDBC连接。