一、建一个普通java项目,
项目目录如下:
添加jar包如下:
junit-4.10.jar
log4j-1.2.16.jar
mybatis-3.1.1.jar
mysql-connector-java-5.1.7-bin.jar
二、实体类
public class User{
private int id;
private String name;
private int age;
public User() {
}
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
无参数构造函数千万别忘了哟。。。
三、db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=xxxx
四、conf.xml(略去了干扰内容)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<typeAliases>
<typeAlias type="com.test9.User" alias="_CUser"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/test9/userMapper.xml"/>
</mappers>
</configuration>
五、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.test9.userMapper">
<select id="getUser" parameterType="int" resultType="_CUser">
select * from c_user where id=#{id}
</select>
<update id="updateUser" parameterType="_CUser">
update c_user set name=#{name}, age=#{age} where id=#{id}
</update>
</mapper>
六、MybatisUtils
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtils {
public static SqlSessionFactory getSessionFactory(String resource)
throws IOException {
// 加载 mybatis 的配置文件(它也加载关联的映射文件)
Reader reader = Resources.getResourceAsReader(resource);
// 构建 sqlSession 的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
.build(reader);
return sessionFactory;
}
}
七、测试类
import java.io.IOException;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import com.util.MybatisUtils;
public class Test9 {
/*
* 一级缓存: 也就 Session 级的缓存(默认开启)
*/
@Test
public void testCache1() throws IOException {
SqlSessionFactory factory = MybatisUtils.getSessionFactory("conf.xml");
SqlSession session = factory.openSession();
String statement = "com.test9.userMapper.getUser";
User user = session.selectOne(statement, 1);
System.out.println(user);
/*
* 一级缓存默认就会被使用
*/
user = session.selectOne(statement, 1);
System.out.println(user);
/*
* 1. 必须是同一个 Session,如果 session 对象已经close()过了就不可能用了
*/
SqlSession session1 = factory.openSession();
user = session1.selectOne(statement, 1);
System.out.println(user);
/*
* 2. 查询条件是不一样的,没有缓存,重新查询
*/
user = session.selectOne(statement, 2);
System.out.println(user);
/*
* 3. 清理缓存,重新查询
*/
session.clearCache();
user = session.selectOne(statement, 2);
System.out.println(user);
/*
* 4. 执行过增删改的操作(这些操作都会清理缓存)
*/
session.update("com.test9.userMapper.updateUser", new User(2, "user",
23));
user = session.selectOne(statement, 2);
System.out.println(user);
}
}
执行日志:
2015-05-09 17:59:51,078 [main] DEBUG [com.test9.userMapper.getUser] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@1ef3a22]
2015-05-09 17:59:51,078 [main] DEBUG [com.test9.userMapper.getUser] - ==> Preparing: select * from c_user where id=?
2015-05-09 17:59:51,140 [main] DEBUG [com.test9.userMapper.getUser] - ==> Parameters: 1(Integer)
User [id=1, name=Tom, age=12]
User [id=1, name=Tom, age=12]
2015-05-09 17:59:51,187 [main] DEBUG [com.test9.userMapper.getUser] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@6f947b]
2015-05-09 17:59:51,187 [main] DEBUG [com.test9.userMapper.getUser] - ==> Preparing: select * from c_user where id=?
2015-05-09 17:59:51,187 [main] DEBUG [com.test9.userMapper.getUser] - ==> Parameters: 1(Integer)
User [id=1, name=Tom, age=12]
2015-05-09 17:59:51,187 [main] DEBUG [com.test9.userMapper.getUser] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@1ef3a22]
2015-05-09 17:59:51,187 [main] DEBUG [com.test9.userMapper.getUser] - ==> Preparing: select * from c_user where id=?
2015-05-09 17:59:51,187 [main] DEBUG [com.test9.userMapper.getUser] - ==> Parameters: 2(Integer)
User [id=2, name=Jack, age=11]
2015-05-09 17:59:51,187 [main] DEBUG [com.test9.userMapper.getUser] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@1ef3a22]
2015-05-09 17:59:51,187 [main] DEBUG [com.test9.userMapper.getUser] - ==> Preparing: select * from c_user where id=?
2015-05-09 17:59:51,187 [main] DEBUG [com.test9.userMapper.getUser] - ==> Parameters: 2(Integer)
User [id=2, name=Jack, age=11]
2015-05-09 17:59:51,203 [main] DEBUG [com.test9.userMapper.updateUser] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@1ef3a22]
2015-05-09 17:59:51,203 [main] DEBUG [com.test9.userMapper.updateUser] - ==> Preparing: update c_user set name=?, age=? where id=?
2015-05-09 17:59:51,203 [main] DEBUG [com.test9.userMapper.updateUser] - ==> Parameters: user(String), 23(Integer), 2(Integer)
2015-05-09 17:59:51,437 [main] DEBUG [com.test9.userMapper.getUser] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@1ef3a22]
2015-05-09 17:59:51,437 [main] DEBUG [com.test9.userMapper.getUser] - ==> Preparing: select * from c_user where id=?
2015-05-09 17:59:51,437 [main] DEBUG [com.test9.userMapper.getUser] - ==> Parameters: 2(Integer)
User [id=2, name=user, age=23]
可以看到哪些查询用到了一级缓存,哪些没有用到。