1. 開始
? ? ? ? 先進行和以前一樣的項目配置、數據庫連接配置,在這些基礎上,額外引入 Mybatis-Plus 依賴即可。
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.11</version></dependency>
? ? ? ? 編寫實體類,一定要按照命名規范,一一對應表的字段。
@Getter
@Setter
@ToString
public class User {private Integer id;private String userName;private String password;private Integer deleteFlag;private Date createTime;private Date updateTime;
}
? ? ? ? 做好上面的準備后,就可以使用 Mybatis-Plus 編寫 Mapper 層了。
2. 使用 Mybatis-Plus 編寫 Mapper 層
????????
?????????
? ? ? ? 現在,如果直接調用 BaseMapper 中的方法會報錯,因為數據庫中的表名是 user_info,而實體類的名稱是 User。因此按照規范命名尤為重要。不過,Mybatis-Plus 也提供了補救方法:在實體類上添加?@TableName("user_info") 注解來手動標識。
? ? ? ? 類似地,在屬性上添加?@TableField() 注解手動標識屬性對應的表字段名。
3. 條件構造器
? ? ? ? QueryWrapper:用于構造 select 和 delete 語句的 where 條件。
? ? ? ? 例如:
select id, user_name, password from user_info
where id=1 and user_name="%S%"
// 構造條件QueryWrapper<User> qw = new QueryWrapper<User>().select("id", "user_name", "password") // 填入的均為數據表字段名.eq("id", 1).like("user_name", "S");// 調用 select 方法userMapper.selectList(qw);
? ? ? ? lt:less than,小于
? ? ? ? le:less than or equal to,小于或等于
? ? ? ? gt:greater than,大于
? ? ? ? ge:greater?than or equal to, 大于或等于
? ? ? ? eq:equals,等于
? ? ? ? nq:not equals,不等于
? ? ? ? 注意,構造條件其實就是在拼接 sql,括號內填入的均是數據表字段名。此時 mybatisplus 將查詢出來的數據賦值給 java 對象時,TableField 注解將不起作用,因此要求 Java 對象屬性名與數據表字段名相同,或滿足自動駝峰轉換的規范。
? ? ? ? UpdateWrapper:用于構造更新語句。
? ? ? ? 例如:
update user_info set delete_flag=0 where id in (1,2)
UpdateWrapper<User> uw = new UpdateWrapper<User>().set("delete_flag", 1).in("id", List.of(1,2));userMapper.update(uw);
? ? ? ? 或:
UpdateWrapper<User> uw = new UpdateWrapper<User>().setSql("delete_flag = 1").in("id", List.of(1,2));userMapper.update(uw);
? ? ? ? LambdaQueryWrapper:使用上面的方法構造條件有兩個缺點,第一是沒法使用 TableField 注解,第二是所有字段名都是用字符串寫死的,后續一旦修改會比較麻煩。為了解決這個問題,Mybatis-Plus 提供了基于 Lambda 的條件構造器,通過 Lambda 表達式來引用實體類的屬性,避免硬編碼,并能很好利用編譯器為我們檢查。
QueryWrapper<User> qw = new QueryWrapper<>();qw.lambda().select(User::getId, User::getUserName, User::getPassword).eq(User::getId, 1);List<User> userList = userMapper.selectList(qw);
4. 自定義 SQL
? ? ? ? 首先,mybatisplus 支持全部 mybatis 自定義 sql 的方式(mybatisplus 只對?mybatis 做升級而不做改動)。在這個基礎上,mybatisplus 的 Wrapper 也為自定義 sql 提供了支持。
例 1:
? ? ? ? SQL 語句:
select id, user_name from user_info where id = 1
?????????Mapper 層:
@Mapper
public interface UserMapper extends BaseMapper<User> {// 上層代碼調用該方法時,需傳入 Wrapper 實例作為條件構造參數List<User> selectByCondition(@Param("ew") Wrapper<User> ew);
}
<select id="selectByCondition" resultType="com.boilermaker.mybatispluslearning.model.User">select id, user_name from user_info ${ew.customSqlSegment}</select>
? ? ? ? 測試:
@Testvoid selectByCondition() {// 構造 Wrapper 對象,作為參數傳入 Mapper 層QueryWrapper<User> qw = new QueryWrapper<User>().eq("id" ,1);// 調用 Mapper 層userMapper.selectByCondition(qw).forEach(System.out::println);}
例 2:
? ? ? ? SQL 語句:
update book_info set price = price + 10 where id in (1,2,3)
? ? ? ? Mapper 層:
@Mapper
public interface BookMapper extends BaseMapper<Book> {void updateByCondition(@Param("addPrice") int addPrice, @Param("ew") Wrapper<Book> ew);
}
<update id="updateByCondition">update book_infoset price = price + #{addPrice} ${ew.customSqlSegment}</update>
? ? ? ? 測試:
@Testvoid updateByCondition() {// 構造 Wrapper 實例,作為第二個參數傳入QueryWrapper<Book> qw = new QueryWrapper<Book>().in("id", List.of(1,2,3));// 調用 Mapper 層bookMapper.updateByCondition(10, qw);}