日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mybatis-plus3.5.1学习笔记

發布時間:2025/3/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis-plus3.5.1学习笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、ID

1>id策略有6種:

想要id自增就在id上面添加

@TableId(type = IdType.AUTO)

mybaits-plus的默認的主鍵策略是:

@TableId(type = IdType.ID_WORKER)

這樣生成的是19位的數字id。

有的人喜歡使用UUID:

@TableId(type = IdType.UUID)

2、createTime(創建時間),updateTime(修改時間)

1>首先我們的數據庫字段必須要有createTime(創建時間),updateTime(修改時間)這兩個字段
2>在實體類添加注解

@TableField(fill = FieldFill.INSERT) private Date createTime;@TableField(fill = FieldFill.INSERT_UPDATE) private Date updateTime;

3>在項目目錄下新增一個handler包,在包下創建MyMetaObjectHandler.java

@Component public class MyMetaObjectHandler implements MetaObjectHandler {//使用mp實現添加操作,這個方法執行@Overridepublic void insertFill(MetaObject metaObject) {this.setFieldValByName("createTime", new Date(), metaObject);this.setFieldValByName("updateTime", new Date(), metaObject);}//使用mp實現修改操作,這個方法執行@Overridepublic void updateFill(MetaObject metaObject) {this.setFieldValByName("updateTime", new Date(), metaObject);} }

3、樂觀鎖——version

1> 數據庫表中添加:“version”,并設置默認值為1
2>實體類中添加字段,然后加上@version注解

@Version private Integer Version;

3>在項目目錄下新建一個包:config。然后在包下面新建:Mpconfig.java文件。

import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement;@EnableTransactionManagement @Configuration @MapperScan("com.bang.mp.mapper")//這里是掃描mapper public class MpConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();//樂觀鎖插件mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());//分頁插件mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return mybatisPlusInterceptor;} }

4>進行測試

//首先插入一條數據,version字段默認賦值為1 @Test void testInsert(){User user = new User();user.setAge(18);user.setName("阿昌");user.setEmail("995931576@qq.com");int result = userMapper.insert(user); } //在對這條數據進行修改,version會變成 2 @Test void testOptimisticLocker(){//查詢User user = userMapper.selectById(1364080977348956166L);//修改數據user.setName("Helen Yao");user.setEmail("helen@qq.com");//執行更新userMapper.updateById(user); }

4、分頁插件

1>在Mpconfig.java 中添加,為了方便,我在步驟3:樂觀鎖中已經添加了

//分頁插件mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));

2>測試

//分頁查詢 @Test void testPage(){//1、創建page對象//傳入參數:當前頁 和 每頁顯示記錄數Page<User> userPage = new Page<>(1,3);//調用mp分頁查詢方法//調用mp分頁查詢過程中,底層會封裝,把所有分頁數據分裝到page對象中userMapper.selectPage(userPage,null);//通過page對象獲取數據userPage.getRecords().forEach(System.out::println);//遍歷查詢的分頁數據System.out.println(userPage.getCurrent());//獲取當前頁System.out.println(userPage.getSize());//每頁顯示記錄數System.out.println(userPage.getTotal());//總記錄數System.out.println(userPage.getPages());//總頁數System.out.println(userPage.hasNext());//判斷是否有下一頁System.out.println(userPage.hasPrevious());//判斷是否有上一頁 }

5、邏輯刪除

1> 數據庫表中添加:“deleted”,并設置默認值為0,在這里mybatis-plus默認0是未刪除,1是已刪除。
2>實體類中添加字段,然后加上@TableLogic注解

@TableLogic private Integer deleted;

3>測試

/** * 測試 邏輯刪除 */ @Test public void testLogicDelete() { int result = userMapper.deleteById(1L); System.out.println(result); }

6、條件查詢

QueryWrapper queryWrapper = new QueryWrapper(); //ge >= ,gt> ,le<=, lt< queryWrapper.ge("age",30); //eq,ne//between 年齡在20歲到30歲 queryWrapper.between("age",20,30);//like 模糊查詢 queryWrapper.like("name","岳");//orderBy queryWrapper.orderByAsc("id"); queryWrapper.orderByDesc("id"); //last 就是在sql 的后面添加 queryWrapper.last("limit 1"); //查詢指定的列 queryWrapper.select("id","name"); List<User> list = userMapper.selectList(queryWrapper);

總結

以上是生活随笔為你收集整理的mybatis-plus3.5.1学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。