Mybatis注解开发(超详细)

末蓝、 2022-10-30 02:26 273阅读 0赞

Mybatis注解开发

  • mybatis的常用注解
  • 使用 Mybatis 注解实现基本 CRUD
    • 项目目录结构
    • 编写实体类
    • 使用注解方式开发持久层接口
    • 编写 SqlMapConfig.xml 配置文件
    • 编写测试代码
  • 使用注解实现复杂关系映射开发
    • 复杂关系映射的注解说明
    • 项目目录
    • 使用注解实现一对一复杂关系映射及立即加载
      • 添加 User 实体类及 Account 实体类
      • 添加账户的持久层接口并使用注解配置
      • 添加用户的持久层接口并使用注解配置
      • 测试一对一关联及立即加载
    • 使用注解实现一对多复杂关系映射及延迟加载
      • User 实体类加入 List``
      • 编写用户的持久层接口并使用注解配置
      • 编写账户的持久层接口并使用注解配置
      • 编写测试代码
  • 回顾下一级缓存
  • mybatis 基于注解的二级缓存
    • 在 SqlMapConfig.xml 中开启二级缓存支持
    • 在持久层接口中使用注解配置二级缓存
    • 编写测试二级缓存的测试类

mybatis的常用注解






















































注解 说明
@Insert 实现新增
@Delete 实现删除
@Update 实现更新
@Select 实现查询
@Result 实现结果集封装
@Results 可以与@Result 一起使用,封装多个结果集
@ResultMap 实现引用@Results 定义的封装
@One 实现一对一结果集封装
@Many 实现一对多结果集封装
@SelectProvider 实现动态 SQL 映射
@CacheNamespace 实现注解二级缓存的使用

使用 Mybatis 注解实现基本 CRUD

项目目录结构

在这里插入图片描述

编写实体类

User:

  1. package com.keafmd.domain;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. /** * Keafmd * * @ClassName: User * @Description: User实体类 * @author: 牛哄哄的柯南 * @date: 2021-02-16 20:28 */
  5. public class User implements Serializable {
  6. private Integer id;
  7. private String username;
  8. private String address;
  9. private String sex;
  10. private Date birthday;
  11. public Integer getId() {
  12. return id;
  13. }
  14. public void setId(Integer id) {
  15. this.id = id;
  16. }
  17. public String getUsername() {
  18. return username;
  19. }
  20. public void setUsername(String username) {
  21. this.username = username;
  22. }
  23. public String getAddress() {
  24. return address;
  25. }
  26. public void setAddress(String address) {
  27. this.address = address;
  28. }
  29. public String getSex() {
  30. return sex;
  31. }
  32. public void setSex(String sex) {
  33. this.sex = sex;
  34. }
  35. public Date getBirthday() {
  36. return birthday;
  37. }
  38. public void setBirthday(Date birthday) {
  39. this.birthday = birthday;
  40. }
  41. @Override
  42. public String toString() {
  43. return "User{" +
  44. "id=" + id +
  45. ", username='" + username + '\'' +
  46. ", address='" + address + '\'' +
  47. ", sex='" + sex + '\'' +
  48. ", birthday=" + birthday +
  49. '}';
  50. }
  51. }

使用注解方式开发持久层接口

IUserDao:

  1. package com.keafmd.dao;
  2. import com.keafmd.domain.User;
  3. import org.apache.ibatis.annotations.Delete;
  4. import org.apache.ibatis.annotations.Insert;
  5. import org.apache.ibatis.annotations.Select;
  6. import org.apache.ibatis.annotations.Update;
  7. import java.util.List;
  8. /** * Keafmd * * @ClassName: IUserDao * @Description: * @author: 牛哄哄的柯南 * @date: 2021-02-16 20:30 */
  9. /** * 在mybatis中针对CRUD一共有四个注解 * @Select @Insert @Update @Delete */
  10. public interface IUserDao {
  11. /** * 查询所有用户 * @return */
  12. @Select("select * from user")
  13. List<User> findAll();
  14. /** * 保存用户 * @param user */
  15. @Insert("insert into user(username,address,sex,birthday)values(#{username},#{address},#{sex},#{birthday})")
  16. void saveUser(User user);
  17. /** * 更新用户 * @param user */
  18. @Update("update user set username=#{username},sex=#{sex},birthday=#{birthday},address=#{address} where id=#{id}")
  19. void updateUser(User user);
  20. /** * 删除用户 * @param userId */
  21. @Delete("delete from user where id=#{id}")
  22. void deleteUser(Integer userId);
  23. /** * 根据id查询用户 * @param userId * @return */
  24. @Select("select * from user where id=#{id}")
  25. User findById(Integer userId);
  26. /** * 根据用户名称模糊查询 * @param username * @return */
  27. //@Select("select * from user where username like #{username}") //占位符
  28. @Select("select * from user where username like '%${value}%'") //字符串拼接
  29. List<User> findByName(String username);
  30. /** * 查询总数量 * @return */
  31. @Select("select count(*) from user")
  32. int findTotal();
  33. }

通过注解方式,就不需要再去编写 UserDao.xml 映射文件了。

编写 SqlMapConfig.xml 配置文件

SqlMapConfig.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
  3. <configuration>
  4. <!--引入外部配置文件-->
  5. <properties resource="jdbcConfig.properties"></properties>
  6. <!--配置别名-->
  7. <typeAliases>
  8. <package name="com.keafmd.domain"/>
  9. </typeAliases>
  10. <!--配置环境-->
  11. <environments default="mysql">
  12. <environment id="mysql">
  13. <transactionManager type="JDBC"></transactionManager>
  14. <dataSource type="POOLED">
  15. <property name="driver" value="${jdbc.driver}"></property>
  16. <property name="url" value="${jdbc.url}"></property>
  17. <property name="username" value="${jdbc.username}"></property>
  18. <property name="password" value="${jdbc.password}"></property>
  19. </dataSource>
  20. </environment>
  21. </environments>
  22. <!--指定带有注解的dao接口所在位置-->
  23. <mappers>
  24. <package name="com.keafmd.dao"></package>
  25. </mappers>
  26. </configuration>

编写测试代码

AnnotationCRUDTest:

  1. package com.keafmd.test;
  2. import com.keafmd.dao.IUserDao;
  3. import com.keafmd.domain.User;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8. import org.junit.After;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11. import java.io.InputStream;
  12. import java.util.Date;
  13. import java.util.List;
  14. /** * Keafmd * * @ClassName: AnnotationCRUDTest * @Description: 注解开发CRUD测试 * @author: 牛哄哄的柯南 * @date: 2021-02-16 21:05 */
  15. public class AnnotationCRUDTest {
  16. private InputStream in;
  17. private SqlSessionFactory factory;
  18. private SqlSession session;
  19. private IUserDao userDao;
  20. @Before
  21. public void init() throws Exception{
  22. in = Resources.getResourceAsStream("SqlMapConfig.xml");
  23. factory = new SqlSessionFactoryBuilder().build(in);
  24. session = factory.openSession();
  25. userDao = session.getMapper(IUserDao.class);
  26. }
  27. @After
  28. public void destory() throws Exception{
  29. session.commit();
  30. session.close();
  31. in.close();
  32. }
  33. @Test
  34. public void testSave(){
  35. User user = new User();
  36. user.setUsername("mybatis annotation");
  37. user.setAddress("北京");
  38. userDao.saveUser(user);
  39. }
  40. @Test
  41. public void testUpdate(){
  42. User user = new User();
  43. user.setId(55);
  44. user.setUsername("mybatis annotation");
  45. user.setAddress("北京");
  46. user.setSex("男");
  47. user.setBirthday(new Date());
  48. userDao.updateUser(user);
  49. }
  50. @Test
  51. public void testDelete(){
  52. userDao.deleteUser(54);
  53. }
  54. @Test
  55. public void testFindOne(){
  56. User user = userDao.findById(55);
  57. System.out.println(user);
  58. }
  59. @Test
  60. public void testFindByName(){
  61. //List<User> users = userDao.findByName("%Keafmd%");
  62. List<User> users = userDao.findByName("Keafmd");
  63. for (User user : users) {
  64. System.out.println(user);
  65. }
  66. }
  67. @Test
  68. public void testFindTotal(){
  69. int total = userDao.findTotal();
  70. System.out.println(total);
  71. }
  72. }

使用注解实现复杂关系映射开发

实现复杂关系映射之前我们可以在映射文件中通过配置<resultMap>来实现,在使用注解开发时我们需要借助@Results 注解,@Result 注解,@One 注解,@Many 注解。

复杂关系映射的注解说明

@Results 注解
代替的是标签<resultMap>
该注解中可以使用单个@Result 注解,也可以使用@Result 集合
@Results({@Result(),@Result()})或@Results(@Result())

@Resutl 注解
代替了<id> 标签和<result>标签
@Result 中的属性介绍:






























@Result 中的属性 介绍
id 是否是主键字段
column 数据库的列名
property 需要装配的属性名
one 需要使用的@One 注解(@Result(one=@One)()))
many 需要使用的@Many 注解(@Result(many=@many)()))

@One 注解(一对一)
代替了<assocation>标签,是多表查询的关键,在注解中用来指定子查询返回单一对象。
@One 注解属性介绍:
select 指定用来多表查询的 sqlmapper
fetchType 会覆盖全局的配置参数 lazyLoadingEnabled。
使用格式:@Result(column=” “,property=””,one=@One(select=””))

@Many 注解(多对一)
代替了<collection>标签,是是多表查询的关键,在注解中用来指定子查询返回对象集合。
注意:聚集元素用来处理“一对多”的关系。需要指定映射的 Java 实体类的属性,属性的 javaType(一般为 ArrayList)但是注解中可以不定义。
使用格式:@Result(property=””,column=””,many=@Many(select=””))

项目目录

在这里插入图片描述

使用注解实现一对一复杂关系映射及立即加载

需求:加载账户信息时并且加载该账户的用户信息,根据情况可实现立即加载。(注解方式实现)

添加 User 实体类及 Account 实体类

User:

  1. package com.keafmd.domain;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. import java.util.List;
  5. /** * Keafmd * * @ClassName: User * @Description: User实体类 * @author: 牛哄哄的柯南 * @date: 2021-02-16 20:28 */
  6. public class User implements Serializable {
  7. private Integer userId;
  8. private String userName;
  9. private String userAddress;
  10. private String userSex;
  11. private Date userBirthday;
  12. public Integer getUserId() {
  13. return userId;
  14. }
  15. public void setUserId(Integer userId) {
  16. this.userId = userId;
  17. }
  18. public String getUserName() {
  19. return userName;
  20. }
  21. public void setUserName(String userName) {
  22. this.userName = userName;
  23. }
  24. public String getUserAddress() {
  25. return userAddress;
  26. }
  27. public void setUserAddress(String userAddress) {
  28. this.userAddress = userAddress;
  29. }
  30. public String getUserSex() {
  31. return userSex;
  32. }
  33. public void setUserSex(String userSex) {
  34. this.userSex = userSex;
  35. }
  36. public Date getUserBirthday() {
  37. return userBirthday;
  38. }
  39. public void setUserBirthday(Date userBirthday) {
  40. this.userBirthday = userBirthday;
  41. }
  42. @Override
  43. public String toString() {
  44. return "User{" +
  45. "userId=" + userId +
  46. ", userName='" + userName + '\'' +
  47. ", userAddress='" + userAddress + '\'' +
  48. ", userSex='" + userSex + '\'' +
  49. ", userBirthday=" + userBirthday +
  50. '}';
  51. }
  52. }

Account:

  1. package com.keafmd.domain;
  2. import java.io.Serializable;
  3. /** * Keafmd * * @ClassName: Account * @Description: 账户实体类 * @author: 牛哄哄的柯南 * @date: 2021-02-16 22:51 */
  4. public class Account implements Serializable {
  5. private Integer id;
  6. private Integer uid;
  7. private Double money;
  8. //多对一(mybatis中称之为一对一)的映射,一个账户只能属于一个用户 *
  9. private User user;
  10. public User getUser() {
  11. return user;
  12. }
  13. public void setUser(User user) {
  14. this.user = user;
  15. }
  16. public Integer getId() {
  17. return id;
  18. }
  19. public void setId(Integer id) {
  20. this.id = id;
  21. }
  22. public Integer getUid() {
  23. return uid;
  24. }
  25. public void setUid(Integer uid) {
  26. this.uid = uid;
  27. }
  28. public Double getMoney() {
  29. return money;
  30. }
  31. public void setMoney(Double money) {
  32. this.money = money;
  33. }
  34. @Override
  35. public String toString() {
  36. return "Account{" +
  37. "id=" + id +
  38. ", uid=" + uid +
  39. ", money=" + money +
  40. '}';
  41. }
  42. }

添加账户的持久层接口并使用注解配置

IAccountDao:

  1. package com.keafmd.dao;
  2. import com.keafmd.domain.Account;
  3. import org.apache.ibatis.annotations.One;
  4. import org.apache.ibatis.annotations.Result;
  5. import org.apache.ibatis.annotations.Results;
  6. import org.apache.ibatis.annotations.Select;
  7. import org.apache.ibatis.mapping.FetchType;
  8. import java.util.List;
  9. /** * Keafmd * * @ClassName: IAccountDao * @Description: * @author: 牛哄哄的柯南 * @date: 2021-02-16 22:54 */
  10. public interface IAccountDao {
  11. /** * 查询所有账户,并且获取每个账户下的用户信息,一对一 * @return */
  12. @Select("select * from account")
  13. @Results(id="accountMap",value = {
  14. @Result(id = true,column = "id",property = "id"),
  15. @Result(column = "uid",property = "uid"),
  16. @Result(column = "money",property = "money"),
  17. @Result(property = "user",column = "uid",one=@One(select="com.keafmd.dao.IUserDao.findById",fetchType= FetchType.EAGER))
  18. })
  19. List<Account> findAll();
  20. }

添加用户的持久层接口并使用注解配置

  1. package com.keafmd.dao;
  2. import com.keafmd.domain.User;
  3. import org.apache.ibatis.annotations.*;
  4. import org.apache.ibatis.mapping.FetchType;
  5. import java.util.List;
  6. /** * Keafmd * * @ClassName: IUserDao * @Description: * @author: 牛哄哄的柯南 * @date: 2021-02-16 20:30 */
  7. /** * 在mybatis中针对CRUD一共有四个注解 * @Select @Insert @Update @Delete */
  8. public interface IUserDao {
  9. /** * 查询所有用户 * @return */
  10. @Select("select * from user")
  11. @Results(id="userMap",value={
  12. @Result(id = true,column = "id",property = "userId"),
  13. @Result(column = "id",property = "userId"),
  14. @Result(column = "username",property = "userName"),
  15. @Result(column = "sex",property = "userSex"),
  16. @Result(column = "birthday",property = "userBirthday")
  17. })
  18. List<User> findAll();
  19. /** * 根据id查询用户 * @param userId * @return */
  20. @Select("select * from user where id=#{id}")
  21. //@ResultMap(value={"userMap"})
  22. @ResultMap("userMap")
  23. User findById(Integer userId);
  24. /** * 根据用户名称模糊查询 * @param username * @return */
  25. @Select("select * from user where username like #{username}") //占位符
  26. @ResultMap("userMap")
  27. List<User> findByName(String username);
  28. }

测试一对一关联及立即加载

  1. package com.keafmd.test;
  2. import com.keafmd.dao.IAccountDao;
  3. import com.keafmd.dao.IUserDao;
  4. import com.keafmd.domain.Account;
  5. import com.keafmd.domain.User;
  6. import org.apache.ibatis.io.Resources;
  7. import org.apache.ibatis.session.SqlSession;
  8. import org.apache.ibatis.session.SqlSessionFactory;
  9. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  10. import org.junit.After;
  11. import org.junit.Before;
  12. import org.junit.Test;
  13. import java.io.InputStream;
  14. import java.util.List;
  15. /** * Keafmd * * @ClassName: AnnotationCRUDTest * @Description: 注解开发CRUD测试 * @author: 牛哄哄的柯南 * @date: 2021-02-16 21:05 */
  16. public class AccountTest {
  17. private InputStream in;
  18. private SqlSessionFactory factory;
  19. private SqlSession session;
  20. private IAccountDao accountDao;
  21. @Before
  22. public void init() throws Exception{
  23. in = Resources.getResourceAsStream("SqlMapConfig.xml");
  24. factory = new SqlSessionFactoryBuilder().build(in);
  25. session = factory.openSession();
  26. accountDao = session.getMapper(IAccountDao.class);
  27. }
  28. @After
  29. public void destory() throws Exception{
  30. session.commit();
  31. session.close();
  32. in.close();
  33. }
  34. @Test
  35. public void testFindAll(){
  36. List<Account> accounts = accountDao.findAll();
  37. for (Account account : accounts) {
  38. System.out.println("-----每个账户信息-----");
  39. System.out.println(account);
  40. System.out.println(account.getUser());
  41. }
  42. }
  43. }

运行结果:

  1. 2021-02-17 03:04:39,939 163 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
  2. 2021-02-17 03:04:40,190 414 [ main] DEBUG source.pooled.PooledDataSource - Created connection 1176735295.
  3. 2021-02-17 03:04:40,190 414 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
  4. 2021-02-17 03:04:40,195 419 [ main] DEBUG keafmd.dao.IAccountDao.findAll - ==> Preparing: select * from account
  5. 2021-02-17 03:04:40,226 450 [ main] DEBUG keafmd.dao.IAccountDao.findAll - ==> Parameters:
  6. 2021-02-17 03:04:40,267 491 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ====> Preparing: select * from user where id=?
  7. 2021-02-17 03:04:40,268 492 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ====> Parameters: 41(Integer)
  8. 2021-02-17 03:04:40,270 494 [ main] DEBUG m.keafmd.dao.IUserDao.findById - <==== Total: 1
  9. 2021-02-17 03:04:40,271 495 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ====> Preparing: select * from user where id=?
  10. 2021-02-17 03:04:40,271 495 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ====> Parameters: 45(Integer)
  11. 2021-02-17 03:04:40,272 496 [ main] DEBUG m.keafmd.dao.IUserDao.findById - <==== Total: 1
  12. 2021-02-17 03:04:40,273 497 [ main] DEBUG keafmd.dao.IAccountDao.findAll - <== Total: 3
  13. -----每个账户信息-----
  14. Account{ id=1, uid=41, money=1000.0}
  15. User{ userId=41, userName='update user clear cache', userAddress='null', userSex='男', userBirthday=Tue Feb 27 17:47:08 CST 2018}
  16. -----每个账户信息-----
  17. Account{ id=2, uid=45, money=1000.0}
  18. User{ userId=45, userName='新一', userAddress='null', userSex='男', userBirthday=Sun Mar 04 12:04:06 CST 2018}
  19. -----每个账户信息-----
  20. Account{ id=3, uid=41, money=2000.0}
  21. User{ userId=41, userName='update user clear cache', userAddress='null', userSex='男', userBirthday=Tue Feb 27 17:47:08 CST 2018}
  22. 2021-02-17 03:04:40,274 498 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
  23. 2021-02-17 03:04:40,274 498 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
  24. 2021-02-17 03:04:40,274 498 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 1176735295 to pool.
  25. Process finished with exit code 0

使用注解实现一对多复杂关系映射及延迟加载

需求:查询用户信息时,也要查询他的账户列表。使用注解方式实现。
分析:一个用户具有多个账户信息,所以形成了用户(User)与账户(Account)之间的一对多关系。

User 实体类加入 List<Account>

User:

  1. package com.keafmd.domain;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. import java.util.List;
  5. /** * Keafmd * * @ClassName: User * @Description: User实体类 * @author: 牛哄哄的柯南 * @date: 2021-02-16 20:28 */
  6. public class User implements Serializable {
  7. private Integer userId;
  8. private String userName;
  9. private String userAddress;
  10. private String userSex;
  11. private Date userBirthday;
  12. //一对多关系映射:一个用户对应多个账户
  13. private List<Account> accounts;
  14. public List<Account> getAccounts() {
  15. return accounts;
  16. }
  17. public void setAccounts(List<Account> accounts) {
  18. this.accounts = accounts;
  19. }
  20. public Integer getUserId() {
  21. return userId;
  22. }
  23. public void setUserId(Integer userId) {
  24. this.userId = userId;
  25. }
  26. public String getUserName() {
  27. return userName;
  28. }
  29. public void setUserName(String userName) {
  30. this.userName = userName;
  31. }
  32. public String getUserAddress() {
  33. return userAddress;
  34. }
  35. public void setUserAddress(String userAddress) {
  36. this.userAddress = userAddress;
  37. }
  38. public String getUserSex() {
  39. return userSex;
  40. }
  41. public void setUserSex(String userSex) {
  42. this.userSex = userSex;
  43. }
  44. public Date getUserBirthday() {
  45. return userBirthday;
  46. }
  47. public void setUserBirthday(Date userBirthday) {
  48. this.userBirthday = userBirthday;
  49. }
  50. @Override
  51. public String toString() {
  52. return "User{" +
  53. "userId=" + userId +
  54. ", userName='" + userName + '\'' +
  55. ", userAddress='" + userAddress + '\'' +
  56. ", userSex='" + userSex + '\'' +
  57. ", userBirthday=" + userBirthday +
  58. '}';
  59. }
  60. }

编写用户的持久层接口并使用注解配置

  1. package com.keafmd.dao;
  2. import com.keafmd.domain.User;
  3. import org.apache.ibatis.annotations.*;
  4. import org.apache.ibatis.mapping.FetchType;
  5. import java.util.List;
  6. /** * Keafmd * * @ClassName: IUserDao * @Description: * @author: 牛哄哄的柯南 * @date: 2021-02-16 20:30 */
  7. /** * 在mybatis中针对CRUD一共有四个注解 * @Select @Insert @Update @Delete */
  8. public interface IUserDao {
  9. /** * 查询所有用户 * @return */
  10. @Select("select * from user")
  11. @Results(id="userMap",value={
  12. @Result(id = true,column = "id",property = "userId"),
  13. @Result(column = "id",property = "userId"),
  14. @Result(column = "username",property = "userName"),
  15. @Result(column = "sex",property = "userSex"),
  16. @Result(column = "birthday",property = "userBirthday"),
  17. @Result(property = "accounts" ,column = "id",
  18. many = @Many(select = "com.keafmd.dao.IAccountDao.findAccountByUid",
  19. fetchType = FetchType.LAZY))
  20. })
  21. List<User> findAll();
  22. /** * 根据id查询用户 * @param userId * @return */
  23. @Select("select * from user where id=#{id}")
  24. //@ResultMap(value={"userMap"})
  25. @ResultMap("userMap")
  26. User findById(Integer userId);
  27. /** * 根据用户名称模糊查询 * @param username * @return */
  28. @Select("select * from user where username like #{username}") //占位符
  29. @ResultMap("userMap")
  30. List<User> findByName(String username);
  31. }

编写账户的持久层接口并使用注解配置

  1. package com.keafmd.dao;
  2. import com.keafmd.domain.Account;
  3. import org.apache.ibatis.annotations.One;
  4. import org.apache.ibatis.annotations.Result;
  5. import org.apache.ibatis.annotations.Results;
  6. import org.apache.ibatis.annotations.Select;
  7. import org.apache.ibatis.mapping.FetchType;
  8. import java.util.List;
  9. /** * Keafmd * * @ClassName: IAccountDao * @Description: * @author: 牛哄哄的柯南 * @date: 2021-02-16 22:54 */
  10. public interface IAccountDao {
  11. /** * 查询所有账户,并且获取每个账户下的用户信息,一对一 ,* 这里用不到这个findAll() * @return */
  12. @Select("select * from account")
  13. @Results(id="accountMap",value = {
  14. @Result(id = true,column = "id",property = "id"),
  15. @Result(column = "uid",property = "uid"),
  16. @Result(column = "money",property = "money"),
  17. @Result(property = "user",column = "uid",one=@One(select="com.keafmd.dao.IUserDao.findById",fetchType= FetchType.EAGER))
  18. })
  19. List<Account> findAll();
  20. /** * 根据用户id查询账户信息 * @param userId * @return */
  21. @Select("select * from account where uid = #{userId}")
  22. List<Account> findAccountByUid(Integer userId);
  23. }

编写测试代码

  1. package com.keafmd.test;
  2. import com.keafmd.dao.IUserDao;
  3. import com.keafmd.domain.User;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8. import org.junit.After;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11. import java.io.InputStream;
  12. import java.util.Date;
  13. import java.util.List;
  14. /** * Keafmd * * @ClassName: AnnotationCRUDTest * @Description: 注解开发CRUD测试 * @author: 牛哄哄的柯南 * @date: 2021-02-16 21:05 */
  15. public class UserTest {
  16. private InputStream in;
  17. private SqlSessionFactory factory;
  18. private SqlSession session;
  19. private IUserDao userDao;
  20. @Before
  21. public void init() throws Exception{
  22. in = Resources.getResourceAsStream("SqlMapConfig.xml");
  23. factory = new SqlSessionFactoryBuilder().build(in);
  24. session = factory.openSession();
  25. userDao = session.getMapper(IUserDao.class);
  26. }
  27. @After
  28. public void destory() throws Exception{
  29. session.commit();
  30. session.close();
  31. in.close();
  32. }
  33. @Test
  34. public void testFindAll(){
  35. List<User> users = userDao.findAll();
  36. for (User user : users) {
  37. System.out.println("-----每个用户的信息");
  38. System.out.println(user);
  39. System.out.println(user.getAccounts());
  40. }
  41. }
  42. }

运行testFindAll()的结果:

  1. 2021-02-17 03:14:19,655 378 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
  2. 2021-02-17 03:14:20,124 847 [ main] DEBUG source.pooled.PooledDataSource - Created connection 1176735295.
  3. 2021-02-17 03:14:20,124 847 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
  4. 2021-02-17 03:14:20,135 858 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - ==> Preparing: select * from user
  5. 2021-02-17 03:14:20,183 906 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - ==> Parameters:
  6. 2021-02-17 03:14:20,471 1194 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - <== Total: 9
  7. -----每个用户的信息
  8. 2021-02-17 03:14:20,473 1196 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
  9. 2021-02-17 03:14:20,474 1197 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 41(Integer)
  10. 2021-02-17 03:14:20,475 1198 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 2
  11. User{ userId=41, userName='update user clear cache', userAddress='null', userSex='男', userBirthday=Tue Feb 27 17:47:08 CST 2018}
  12. [Account{ id=1, uid=41, money=1000.0}, Account{ id=3, uid=41, money=2000.0}]
  13. -----每个用户的信息
  14. 2021-02-17 03:14:20,476 1199 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
  15. 2021-02-17 03:14:20,476 1199 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 42(Integer)
  16. 2021-02-17 03:14:20,477 1200 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
  17. User{ userId=42, userName='update', userAddress='null', userSex='男', userBirthday=Mon Feb 08 19:37:31 CST 2021}
  18. []
  19. -----每个用户的信息
  20. 2021-02-17 03:14:20,479 1202 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
  21. 2021-02-17 03:14:20,480 1203 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 43(Integer)
  22. 2021-02-17 03:14:20,481 1204 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
  23. User{ userId=43, userName='小二王', userAddress='null', userSex='女', userBirthday=Sun Mar 04 11:34:34 CST 2018}
  24. []
  25. -----每个用户的信息
  26. 2021-02-17 03:14:20,481 1204 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
  27. 2021-02-17 03:14:20,482 1205 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 45(Integer)
  28. 2021-02-17 03:14:20,483 1206 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 1
  29. User{ userId=45, userName='新一', userAddress='null', userSex='男', userBirthday=Sun Mar 04 12:04:06 CST 2018}
  30. [Account{ id=2, uid=45, money=1000.0}]
  31. -----每个用户的信息
  32. 2021-02-17 03:14:20,484 1207 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
  33. 2021-02-17 03:14:20,484 1207 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 50(Integer)
  34. 2021-02-17 03:14:20,484 1207 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
  35. User{ userId=50, userName='Keafmd', userAddress='null', userSex='男', userBirthday=Mon Feb 08 15:44:01 CST 2021}
  36. []
  37. -----每个用户的信息
  38. 2021-02-17 03:14:20,485 1208 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
  39. 2021-02-17 03:14:20,485 1208 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 51(Integer)
  40. 2021-02-17 03:14:20,486 1209 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
  41. User{ userId=51, userName='update DAO', userAddress='null', userSex='男', userBirthday=Tue Feb 09 11:31:38 CST 2021}
  42. []
  43. -----每个用户的信息
  44. 2021-02-17 03:14:20,488 1211 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
  45. 2021-02-17 03:14:20,489 1212 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 52(Integer)
  46. 2021-02-17 03:14:20,491 1214 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
  47. User{ userId=52, userName='Keafmd DAO', userAddress='null', userSex='男', userBirthday=Tue Feb 09 11:29:41 CST 2021}
  48. []
  49. -----每个用户的信息
  50. 2021-02-17 03:14:20,491 1214 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
  51. 2021-02-17 03:14:20,492 1215 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 53(Integer)
  52. 2021-02-17 03:14:20,493 1216 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
  53. User{ userId=53, userName='Keafmd laset insertid 1', userAddress='null', userSex='男', userBirthday=Fri Feb 12 20:53:46 CST 2021}
  54. []
  55. -----每个用户的信息
  56. 2021-02-17 03:14:20,493 1216 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
  57. 2021-02-17 03:14:20,493 1216 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 55(Integer)
  58. 2021-02-17 03:14:20,494 1217 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
  59. User{ userId=55, userName='mybatis annotation', userAddress='null', userSex='男', userBirthday=Tue Feb 16 22:15:36 CST 2021}
  60. []
  61. 2021-02-17 03:14:20,495 1218 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
  62. 2021-02-17 03:14:20,495 1218 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
  63. 2021-02-17 03:14:20,495 1218 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 1176735295 to pool.
  64. Process finished with exit code 0

可以看出来延迟加载,在每次加载每个用户时都会查询一次。

修改测试代码:

  1. @Test
  2. public void testFindAll(){
  3. List<User> users = userDao.findAll();
  4. /*for (User user : users) { System.out.println("-----每个用户的信息"); System.out.println(user); System.out.println(user.getAccounts()); }*/
  5. }

运行结果:

  1. 2021-02-17 03:17:26,203 166 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
  2. 2021-02-17 03:17:26,500 463 [ main] DEBUG source.pooled.PooledDataSource - Created connection 1176735295.
  3. 2021-02-17 03:17:26,500 463 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
  4. 2021-02-17 03:17:26,505 468 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - ==> Preparing: select * from user
  5. 2021-02-17 03:17:26,540 503 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - ==> Parameters:
  6. 2021-02-17 03:17:26,613 576 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - <== Total: 9
  7. 2021-02-17 03:17:26,614 577 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
  8. 2021-02-17 03:17:26,614 577 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
  9. 2021-02-17 03:17:26,614 577 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 1176735295 to pool.
  10. Process finished with exit code 0

这样通过对比就可以很明显的看出来延迟加载的效果。

回顾下一级缓存

运行代码:

  1. @Test
  2. public void testFindOne(){
  3. User user = userDao.findById(55);
  4. System.out.println(user);
  5. User user2 = userDao.findById(55);
  6. System.out.println(user2);
  7. System.out.println(user==user2);
  8. }

运行结果:

  1. 2021-02-17 03:26:22,736 164 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
  2. 2021-02-17 03:26:22,970 398 [ main] DEBUG source.pooled.PooledDataSource - Created connection 85445963.
  3. 2021-02-17 03:26:22,971 399 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@517cd4b]
  4. 2021-02-17 03:26:22,975 403 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ==> Preparing: select * from user where id=?
  5. 2021-02-17 03:26:23,001 429 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ==> Parameters: 55(Integer)
  6. 2021-02-17 03:26:23,054 482 [ main] DEBUG m.keafmd.dao.IUserDao.findById - <== Total: 1
  7. 2021-02-17 03:26:23,055 483 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
  8. 2021-02-17 03:26:23,055 483 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 55(Integer)
  9. 2021-02-17 03:26:23,056 484 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
  10. User{ userId=55, userName='mybatis annotation', userAddress='null', userSex='男', userBirthday=Tue Feb 16 22:15:36 CST 2021}
  11. User{ userId=55, userName='mybatis annotation', userAddress='null', userSex='男', userBirthday=Tue Feb 16 22:15:36 CST 2021}
  12. true
  13. 2021-02-17 03:26:23,056 484 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@517cd4b]
  14. 2021-02-17 03:26:23,056 484 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@517cd4b]
  15. 2021-02-17 03:26:23,057 485 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 85445963 to pool.
  16. Process finished with exit code 0

mybatis 基于注解的二级缓存

在 SqlMapConfig.xml 中开启二级缓存支持

  1. <!--配置开启二级缓存-->
  2. <settings>
  3. <setting name="cacheEnabled" value="true"/>
  4. </settings>

在持久层接口中使用注解配置二级缓存

  1. package com.keafmd.dao;
  2. import com.keafmd.domain.User;
  3. import org.apache.ibatis.annotations.*;
  4. import org.apache.ibatis.mapping.FetchType;
  5. import java.util.List;
  6. /** * Keafmd * * @ClassName: IUserDao * @Description: * @author: 牛哄哄的柯南 * @date: 2021-02-16 20:30 */
  7. /** * 在mybatis中针对CRUD一共有四个注解 * @Select @Insert @Update @Delete */
  8. @CacheNamespace(blocking = true) //mybatis 基于注解方式实现配置二级缓存 *这里*
  9. public interface IUserDao {
  10. /** * 查询所有用户 * @return */
  11. @Select("select * from user")
  12. @Results(id="userMap",value={
  13. @Result(id = true,column = "id",property = "userId"),
  14. @Result(column = "id",property = "userId"),
  15. @Result(column = "username",property = "userName"),
  16. @Result(column = "sex",property = "userSex"),
  17. @Result(column = "birthday",property = "userBirthday"),
  18. @Result(property = "accounts" ,column = "id",
  19. many = @Many(select = "com.keafmd.dao.IAccountDao.findAccountByUid",
  20. fetchType = FetchType.LAZY))
  21. })
  22. List<User> findAll();
  23. /** * 根据id查询用户 * @param userId * @return */
  24. @Select("select * from user where id=#{id}")
  25. //@ResultMap(value={"userMap"})
  26. @ResultMap("userMap")
  27. User findById(Integer userId);
  28. /** * 根据用户名称模糊查询 * @param username * @return */
  29. @Select("select * from user where username like #{username}") //占位符
  30. @ResultMap("userMap")
  31. List<User> findByName(String username);
  32. }

编写测试二级缓存的测试类

  1. package com.keafmd.test;
  2. import com.keafmd.dao.IUserDao;
  3. import com.keafmd.domain.User;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8. import org.junit.After;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11. import java.io.InputStream;
  12. import java.util.List;
  13. /** * Keafmd * * @ClassName: SecondLevelCatchTest * @Description: 二级缓存测试 * @author: 牛哄哄的柯南 * @date: 2021-02-16 23:37 */
  14. public class SecondLevelCatchTest {
  15. private InputStream in;
  16. private SqlSessionFactory factory;
  17. @Before
  18. public void init() throws Exception{
  19. in = Resources.getResourceAsStream("SqlMapConfig.xml");
  20. factory = new SqlSessionFactoryBuilder().build(in);
  21. }
  22. @After
  23. public void destory() throws Exception{
  24. in.close();
  25. }
  26. @Test
  27. public void testFindOne(){
  28. SqlSession session = factory.openSession();
  29. IUserDao userDao = session.getMapper(IUserDao.class);
  30. User user = userDao.findById(55);
  31. System.out.println(user);
  32. session.close();//释放一级缓存
  33. SqlSession session1 = factory.openSession();//再次打开session
  34. IUserDao userDao1 = session1.getMapper(IUserDao.class);
  35. User user2 = userDao1.findById(55);
  36. System.out.println(user2);
  37. System.out.println(user==user2);
  38. }
  39. }

不开启二级缓存配置的运行结果:

  1. 2021-02-17 03:31:32,119 320 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
  2. 2021-02-17 03:31:32,509 710 [ main] DEBUG source.pooled.PooledDataSource - Created connection 85445963.
  3. 2021-02-17 03:31:32,510 711 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@517cd4b]
  4. 2021-02-17 03:31:32,514 715 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ==> Preparing: select * from user where id=?
  5. 2021-02-17 03:31:32,551 752 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ==> Parameters: 55(Integer)
  6. 2021-02-17 03:31:32,633 834 [ main] DEBUG m.keafmd.dao.IUserDao.findById - <== Total: 1
  7. 2021-02-17 03:31:32,634 835 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
  8. 2021-02-17 03:31:32,634 835 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 55(Integer)
  9. 2021-02-17 03:31:32,635 836 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
  10. User{ userId=55, userName='mybatis annotation', userAddress='null', userSex='男', userBirthday=Tue Feb 16 22:15:36 CST 2021}
  11. 2021-02-17 03:31:32,635 836 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@517cd4b]
  12. 2021-02-17 03:31:32,636 837 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@517cd4b]
  13. 2021-02-17 03:31:32,636 837 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 85445963 to pool.
  14. 2021-02-17 03:31:32,636 837 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
  15. 2021-02-17 03:31:32,636 837 [ main] DEBUG source.pooled.PooledDataSource - Checked out connection 85445963 from pool.
  16. 2021-02-17 03:31:32,636 837 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@517cd4b]
  17. 2021-02-17 03:31:32,636 837 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ==> Preparing: select * from user where id=?
  18. 2021-02-17 03:31:32,637 838 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ==> Parameters: 55(Integer)
  19. 2021-02-17 03:31:32,639 840 [ main] DEBUG m.keafmd.dao.IUserDao.findById - <== Total: 1
  20. 2021-02-17 03:31:32,641 842 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
  21. 2021-02-17 03:31:32,641 842 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 55(Integer)
  22. 2021-02-17 03:31:32,642 843 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
  23. User{ userId=55, userName='mybatis annotation', userAddress='null', userSex='男', userBirthday=Tue Feb 16 22:15:36 CST 2021}
  24. false
  25. Process finished with exit code 0

开启二级缓存配置的运行结果:

  1. 2021-02-17 03:29:23,197 373 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
  2. 2021-02-17 03:29:23,605 781 [ main] DEBUG source.pooled.PooledDataSource - Created connection 500179317.
  3. 2021-02-17 03:29:23,606 782 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1dd02175]
  4. 2021-02-17 03:29:23,617 793 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ==> Preparing: select * from user where id=?
  5. 2021-02-17 03:29:23,668 844 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ==> Parameters: 55(Integer)
  6. 2021-02-17 03:29:23,777 953 [ main] DEBUG m.keafmd.dao.IUserDao.findById - <== Total: 1
  7. 2021-02-17 03:29:23,781 957 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
  8. 2021-02-17 03:29:23,782 958 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 55(Integer)
  9. 2021-02-17 03:29:23,782 958 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
  10. User{ userId=55, userName='mybatis annotation', userAddress='null', userSex='男', userBirthday=Tue Feb 16 22:15:36 CST 2021}
  11. 2021-02-17 03:29:23,790 966 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1dd02175]
  12. 2021-02-17 03:29:23,791 967 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1dd02175]
  13. 2021-02-17 03:29:23,791 967 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 500179317 to pool.
  14. 2021-02-17 03:29:23,800 976 [ main] DEBUG com.keafmd.dao.IUserDao - Cache Hit Ratio [com.keafmd.dao.IUserDao]: 0.5
  15. User{ userId=55, userName='mybatis annotation', userAddress='null', userSex='男', userBirthday=Tue Feb 16 22:15:36 CST 2021}
  16. false
  17. Process finished with exit code 0

效果很明显,开启使用二级缓存时第二次并没有发起查询,证明使用的就是二级缓存。

以上就是Mybatis注解开发(超详细)的全部内容。

看完如果对你有帮助,感谢点赞支持!
如果你是电脑端的话,看到右下角的 “一键三连” 了吗,没错点它[哈哈]

在这里插入图片描述

加油!

共同努力!

Keafmd

发表评论

表情:
评论列表 (有 0 条评论,273人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Mybatis 注解开发

    这几年来注解开发越来越流行,Mybatis 也可以使用注解开发方式,这样我们就可以减少编写 Mapper 映射 文件了。本次我们先围绕一些基本的 CRUD 来学习,再学习复杂映