Mybatis如何通过注解开启使用二级缓存
这篇文章主要介绍了Mybatis基于注解开启使用二级缓存,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
创新互联专业为企业提供雅安网站建设、雅安做网站、雅安网站设计、雅安网站制作等企业网站建设、网页设计与制作、雅安企业网站模板建站服务,十载雅安做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
本文主要是补充一下Mybatis中基于注解的二级缓存的开启使用方法。
1.在Mybatis的配置文件中开启二级缓存
<?xml version="1.0" encoding="UTF-8" ?>
开启缓存
2.领域类以及Dao
public class User implements Serializable{ private Integer userId; private String userName; private Date userBirthday; private String userSex; private String userAddress; private Listaccounts; 省略get和set方法...... } import com.example.domain.User; import org.apache.ibatis.annotations.*; import org.apache.ibatis.mapping.FetchType; import java.util.List; @CacheNamespace(blocking = true) public interface UserDao { /** * 查找所有用户 * @return */ @Select("select * from User") @Results(id = "userMap",value = {@Result(id = true,column = "id",property = "userId"), @Result(column = "username",property = "userName"), @Result(column = "birthday",property = "userBirthday"), @Result(column = "sex",property = "userSex"), @Result(column = "address",property = "userAddress"), @Result(column = "id",property = "accounts",many = @Many(select = "com.example.dao.AccountDao.findAccountByUid",fetchType = FetchType.LAZY)) }) List findAll(); /** * 保存用户 * @param user */ @Insert("insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})") void saveUser(User user); /** * 更新用户 * @param user */ @Update("update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}") void updateUser(User user); /** * 删除用户 * @param id */ @Delete("delete from user where id=#{id}") void deleteUser(Integer id); /** * 查询用户根据ID * @param id * @return */ @Select("select * from user where id=#{id}") @ResultMap(value = {"userMap"}) User findById(Integer id); /** * 根据用户名称查询用户 * @param name * @return */ // @Select("select * from user where username like #{name}") @Select("select * from user where username like '%${value}%'") List findByUserName(String name); /** * 查询用户数量 * @return */ @Select("select count(*) from user") int findTotalUser(); }
3.在对应的Dao类上面增加注释以开启二级缓存
@CacheNamespace(blocking = true)
4.测试
public class UserCacheTest { private InputStream in; private SqlSessionFactory sqlSessionFactory; @Before public void init()throws Exception{ in = Resources.getResourceAsStream("SqlMapConfig.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); } @After public void destory()throws Exception{ in.close(); } @Test public void testFindById(){ //第一查询 SqlSession sqlSession1 = sqlSessionFactory.openSession(); UserDao userDao1 = sqlSession1.getMapper(UserDao.class); User user1 = userDao1.findById(41); System.out.println(user1); //关闭一级缓存 sqlSession1.close(); //第二次查询 SqlSession sqlSession2 = sqlSessionFactory.openSession(); UserDao userDao2 = sqlSession2.getMapper(UserDao.class); User user2 = userDao2.findById(41); System.out.println(user2); sqlSession1.close(); System.out.println(user1 == user2); } }
(1)未开启二级缓存时
(2)开启二级缓存时
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
新闻名称:Mybatis如何通过注解开启使用二级缓存
URL网址:http://pwwzsj.com/article/pocgjg.html