MyBatis之注解开发

逃离我推掉我的手 2024-04-18 19:01 146阅读 0赞

mybatis常用注解:

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

使用注解完成CRUD操作:

  1. public interface IUserDao {
  2. /**
  3. * 查询所有用户
  4. * @return
  5. */
  6. @Select("select * from user")
  7. List<User> findAll();
  8. /**
  9. * 保存用户
  10. * @param user
  11. */
  12. @Insert("insert into user(username,address,sex,birthday)values(#{username},#{address},#{sex},#{birthday})")
  13. void saveUser(User user);
  14. /**
  15. * 更新用户
  16. * @param user
  17. */
  18. @Update("update user set username=#{username},sex=#{sex},birthday=#{birthday},address=#{address} where id=#{id}")
  19. void updateUser(User user);
  20. /**
  21. * 删除用户
  22. * @param userId
  23. */
  24. @Delete("delete from user where id=#{id} ")
  25. void deleteUser(Integer userId);
  26. /**
  27. * 根据id查询用户
  28. * @param userId
  29. * @return
  30. */
  31. @Select("select * from user where id=#{id} ")
  32. User findById(Integer userId);
  33. /**
  34. * 根据用户名称模糊查询
  35. * @param username
  36. * @return
  37. */
  38. // @Select("select * from user where username like #{username} ")
  39. @Select("select * from user where username like '%${value}%' ")
  40. List<User> findUserByName(String username);
  41. /**
  42. * 查询总用户数量
  43. * @return
  44. */
  45. @Select("select count(*) from user ")
  46. int findTotalUser();
  47. }

@Results、@Result、@ResultMap、@One注解

fetchType=FetchType.EAGER为立即加载,一般用在一对一
fetchType=FetchType.LAZY为延迟加载,一般用在多对多

  1. public interface IAccountDao {
  2. /**
  3. * 查询所有账户,并且获取每个账户所属的用户信息
  4. * @return
  5. */
  6. @Select("select * from account")
  7. @Results(id="accountMap",value = {
  8. @Result(id=true,column = "id",property = "id"),
  9. @Result(column = "uid",property = "uid"),
  10. @Result(column = "money",property = "money"),
  11. @Result(property = "user",column = "uid",one=@One(select="com.itheima.dao.IUserDao.findById",fetchType= FetchType.EAGER))
  12. })
  13. List<Account> findAll();
  14. }

com.itheima.dao.IUserDao.findById:

  1. @Select("select * from user where id=#{id} ")
  2. @ResultMap("userMap")
  3. User findById(Integer userId);

pojo类的写法:

在这里插入图片描述

@many注解

  1. public interface IUserDao {
  2. /**
  3. * 查询所有用户
  4. * @return
  5. */
  6. @Select("select * from user")
  7. @Results(id="userMap",value={
  8. @Result(id=true,column = "id",property = "userId"),
  9. @Result(column = "username",property = "userName"),
  10. @Result(column = "address",property = "userAddress"),
  11. @Result(column = "sex",property = "userSex"),
  12. @Result(column = "birthday",property = "userBirthday"),
  13. @Result(property = "accounts",column = "id",
  14. many = @Many(select = "com.itheima.dao.IAccountDao.findAccountByUid",
  15. fetchType = FetchType.LAZY))
  16. })
  17. }

com.itheima.dao.IAccountDao.findAccountByUid:

  1. @Select("select * from account where uid = #{userId}")
  2. List<Account> findAccountByUid(Integer userId);

pojo类的写法:
在这里插入图片描述

@CacheNamespace开启二级缓存

在这里插入图片描述

开启二级缓存后,第一次查询会执行sql,第二次及以后的查询都会从缓存中读取数据

注意:开启缓存的弊端是数据没有实时性,当数据库中的数据一旦修改,查询的数据还是缓存中的数据没有实时性。

发表评论

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

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

相关阅读

    相关 MyBatis注解开发

    除了XML映射方式,MyBatis还支持注解方式实现POJO对象和数据表之间的关联映射,使用注解的方式一般将SQL语句直接写到接口上。与XML的映射方式相比,基于注解的映射方式

    相关 Mybatis 注解开发

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