IAccountDao.xml
<resultMap id="accountUserMap" type="account">
<id property="id" column="id">id>
<result property="uid" column="uid">result>
<result property="money" column="money">result>
resultMap>
<select id="findAll" resultMap="accountUserMap">
select * from account
select>
AccountTest
public void testFindAll(){
//5.执行查询
List<Account> accounts = accountDao.findAll();
//6.打印
for (Account account : accounts) {
System.out.println(account);
System.out.println(account.getUser());
}
}
IAccountDao.xml
<resultMap id="accountUserMap" type="account">
<id property="id" column="id">id>
<result property="uid" column="uid">result>
<result property="money" column="money">result>
<association property="user" column="uid" javaType="user" select="com.xpt.dao.IUserDao.findById">
association>
resultMap>
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false">setting>
settings>
AccountTest
的findAll
方法观察 @Test
public void testFindAll(){
//5.执行查询
List<Account> accounts = accountDao.findAll();
//6.打印
for (Account account : accounts) {
System.out.println(account);
System.out.println(account.getUser());
}
}
@Test
public void testFindAll(){
//5.执行查询
List<Account> accounts = accountDao.findAll();
// //6.打印
// for (Account account : accounts) {
// System.out.println(account);
System.out.println(account.getUser());
// }
}
查询一个用户的时候,延迟加载其多个关联的账户
/**
* 根据用户id 查询用户id
* @param uid
* @return
*/
List<Account> findAccountByUid(Integer uid);
<select id="findAccountByUid" resultType="account">
select * from account where uid = #{uid}
select>
<resultMap id="userAccountMap" type="user">
<id property="id" column="id">id>
<result property="username" column="username">result>
<result property="address" column="address">result>
<result property="sex" column="sex">result>
<result property="birthday" column="birthday">result>
<collection property="accounts" ofType="account" select="com.xpt.dao.IAccountDao.findAccountByUid" column="id">
collection>
resultMap>
<select id="findAll" resultMap="userAccountMap">
select * from user
select>
@Test
public void testFindAll(){
//5.执行查询
List<User> users = userDao.findAll();
// //6.打印
// for (User user : users) {
// System.out.println(user);
// System.out.println(user.getAccounts());
// }
}
@Test
public void testFirstLevelCache(){
User user1 = userDao.findById(41);
System.out.println(user1);
User user2 = userDao.findById(41);
System.out.println(user2);
System.out.println(user1 == user2);
}
@Test
public void testFirstLevelCache(){
User user1 = userDao.findById(41);
System.out.println(user1);
//关闭当前的sqlSession
sqlSession.close();
//重新获取sqlSession
sqlSession = factory.openSession(true);
//重新获取mapper对象
userDao = sqlSession.getMapper(IUserDao.class);
User user2 = userDao.findById(41);
System.out.println(user2);
System.out.println(user1 == user2);
}
除了上例的关闭再开启后,实现同样的效果可以利用
sqlSession
的清空缓存方法
@Test
public void testFirstLevelCache(){
User user1 = userDao.findById(41);
System.out.println(user1);
//关闭当前的sqlSession
// sqlSession.close();
// //重新获取sqlSession
// sqlSession = factory.openSession(true);
// //重新获取mapper对象
sqlSession.clearCache();
userDao = sqlSession.getMapper(IUserDao.class);
User user2 = userDao.findById(41);
System.out.println(user2);
System.out.println(user1 == user2);
}
先完成对id=41的用户查询,然后更新id=41,然后再完成对id=41的查询,在更新id=41的时候,
sqlSession
会清空掉当前的缓存,当第二次查询的时候,sqlSession
缓存中没有相应数据,只好在数据库中完成查询
/**
* 测试会触发清空缓存的操作
*/
@Test
public void testClearCache(){
//1.根据id查询用户
User user1 = userDao.findById(41);
System.out.println(user1);
//2.更新user1
user1.setUsername("update name");
user1.setAddress("update address");
userDao.updateUser(user1);
//提交事务,清空缓存
sqlSession.commit();
//3.再次查询id= user1.id的用户
User user2 = userDao.findById(41);
System.out.println(user2);
System.out.println(user1 == user2);
}
它指的是
Mybatis
中SqlSessionFactory
对象的缓存。由同一个SqlSessionFactory
对象创建的SqlSession
共享其缓存。
Mybatis
框架支持二级缓存(在主配置文件SqlMapConfig.xml
中配置)IUserDao.xml
中配置)select
标签中配置)SqlMapConfig.xml
中配置
<settings>
<setting name="cacheEnabled" value="true">setting>
settings>
IUserDao.xml
中配置
<cache/>
select
标签中配置
<select id="findById" parameterType="Integer" resultType="com.xpt.domain.User" useCache="true">
select * from user where id = #{id}
select>
@Test
public void testFirstLevelCache(){
SqlSession sqlSession1 = factory.openSession();
IUserDao dao1 = sqlSession1.getMapper(IUserDao.class);
User user1 = dao1.findById(41);
System.out.println(user1);
sqlSession1.close();//一级缓存消失
SqlSession sqlSession2 = factory.openSession();
IUserDao dao2 = sqlSession2.getMapper(IUserDao.class);
User user2 = dao2.findById(41);
System.out.println(user2);
sqlSession2.close();
System.out.println(user1 == user2);
}