Mybatis plus,Mybatis_day4_Mybatis的注解開發

 2023-10-17 阅读 29 评论 0

摘要:這幾年來注解開發越來越流行,Mybatis 也可以使用注解開發方式,這樣我們就可以減少編寫 Mapper 映射文件。 mybatis 的常用注解說明 @Insert:實現新增@Update:實現更新@Delete:實現刪除@Select:實現查詢@Result:實現結果集封裝@Results:
  • 這幾年來注解開發越來越流行,Mybatis 也可以使用注解開發方式,這樣我們就可以減少編寫 Mapper 映射文件。

mybatis 的常用注解說明

  • @Insert:實現新增
  • @Update:實現更新
  • @Delete:實現刪除
  • @Select:實現查詢
  • @Result:實現結果集封裝
  • @Results:可以與@Result 一起使用,封裝多個結果集
  • @ResultMap:實現引用@Results 定義的封裝
  • @One:實現一對一結果集封裝
  • @Many:實現一對多結果集封裝
  • @SelectProvider: 實現動態 SQL 映射
  • @CacheNamespace:實現注解二級緩存的使用

使用 Mybatis 注解實現基本 CRUD

  1. 編寫實體類
public class User implements Serializable {
private Integer userId;
private String userName;
private Date userBirthday;
private String userSex;
private String userAddress;public Integer getUserId() {return userId;
}
public void setUserId(Integer userId) {this.userId = userId;
}
.....
  • 注意:此處User的屬性名和數據庫表的列名不一致。

  1. 使用注解方式開發持久層接口
public interface IUserDao {
/**
* 查詢所有用戶
* @return
*/
@Select("select * from user")
@Results(id="userMap",value= {@Result(id=true,column="id",property="userId"),@Result(column="username",property="userName"),@Result(column="sex",property="userSex"),@Result(column="address",property="userAddress"),@Result(column="birthday",property="userBirthday")})
List<User> findAll();/**
* 根據 id 查詢一個用戶
* @param userId
* @return
*/
@Select("select * from user where id = #{uid} ")
@ResultMap("userMap")
User findById(Integer userId);/**
* 保存操作
* @param user
* @return
*/
@Insert("insert into user(username,sex,birthday,address)values(#{username},#{sex},#{birthday},#{address})")
int saveUser(User user);/**
* 更新操作
* @param user
* @return
*/
@Update("update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id =#{id} ")
int updateUser(User user);/**
* 刪除用戶
* @param userId
* @return
*/
@Delete("delete from user where id = #{uid} ")
int deleteUser(Integer userId);/**
* 查詢使用聚合函數
* @return
*/
@Select("select count(*) from user ")
int findTotal();/**
* 模糊查詢
* @param name
* @return
*/
@Select("select * from user where username like #{username} ")
List<User> findByName(String name);
}

  1. 編寫 SqlMapConfig 配置文件

<!-- 配置映射信息 -->
<mappers><!-- 配置 dao 接口的位置,它有兩種方式
第一種:使用 mapper 標簽配置 class 屬性
第二種:使用 package 標簽,直接指定 dao 接口所在的包
--><package name="cn.myp666.dao"/>
</mappers>



使用注解實現復雜關系映射開發

  • 實現復雜關系映射之前我們可以在映射文件中通過配置<resultMap>來實現,在使用注解開發時我們需要借助@Results 注解,@Result 注解,@One 注解,@Many 注解。

復雜關系映射的注解說明
  • @Results 注解

    • 代替的是標簽<resultMap>
    • 該注解中可以使用單個@Result 注解,也可以使用@Result 集合
      • @Results({@Result(),@Result()})或@Results(@Result())
  • @Result 注解

    • 代替了<id>標簽和<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=” "))

使用注解實現一對多復雜關系映射
  1. 添加賬戶的持久層接口并使用注解配置
public interface IAccountDao {
/**
* 查詢所有賬戶,采用延遲加載的方式查詢賬戶的所屬用戶
* @return
*/
@Select("select * from account")
@Results(id="accountMap",value= {@Result(id=true,column="id",property="id"),@Result(column="uid",property="uid"),@Result(column="money",property="money"),@Result(column="uid",property="user",one=@One(select="cn.myp666.dao.IUserDao.findById",fetchType=FetchType.LAZY)
)
})
List<Account> findAll();
}

  1. 添加用戶的持久層接口并使用注解配置
public interface IUserDao {
/**
* 查詢所有用戶
* @return
*/
@Select("select * from user")
@Results(id="userMap",value= {@Result(id=true,column="id",property="userId"),@Result(column="username",property="userName"),@Result(column="sex",property="userSex"),@Result(column="address",property="userAddress"),@Result(column="birthday",property="userBirthday")
})
List<User> findAll();/**
* 根據 id 查詢一個用戶
* @param userId
* @return
*/
@Select("select * from user where id = #{uid} ")
@ResultMap("userMap")
User findById(Integer userId);
}


使用注解實現一對多復雜關系映射
  1. 編寫用戶的持久層接口并使用注解配置
public interface IUserDao {
/**
* 查詢所有用戶
* @return
*/
@Select("select * from user")
@Results(id="userMap",value= {@Result(id=true,column="id",property="userId"),@Result(column="username",property="userName"),@Result(column="sex",property="userSex"),@Result(column="address",property="userAddress"),@Result(column="birthday",property="userBirthday"),@Result(column="id",property="accounts",many=@Many(select="cn.myp666.dao.IAccountDao.findByUid",fetchType=FetchType.LAZY))})List<User> findAll();
}
  • @Many:相當于<collection>的配置
  • select 屬性:代表將要執行的 sql 語句
  • fetchType 屬性:代表加載方式,一般如果要延遲加載都設置為LAZY的值

  1. 編寫賬戶的持久層接口并使用注解配置
public interface IAccountDao {
/**
* 根據用戶 id 查詢用戶下的所有賬戶
* * @param userId
* @return
*/
@Select("select * from account where uid = #{uid} ")
List<Account> findByUid(Integer userId);
}

Mybatis plus?

mybatis 基于注解的二級緩存

  1. 在 SqlMapConfig 中開啟二級緩存支持
<!-- 配置二級緩存 -->
<settings>
<!-- 開啟二級緩存的支持 --><setting name="cacheEnabled" value="true"/>
</settings>
  1. 在持久層接口中使用注解配置二級緩存
@CacheNamespace(blocking=true)//mybatis 基于注解方式實現配置二級緩存
public interface IUserDao {}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/143610.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息