Java mybatis-plus詳解
1、簡(jiǎn)介
MyBatis-Plus 是一個(gè) Mybatis 增強(qiáng)版工具,在 MyBatis 上擴(kuò)充了其他功能沒(méi)有改變其基本功能,為了簡(jiǎn)化開(kāi)發(fā)提交效率而存在。
2、適用情況
1、對(duì)于只進(jìn)行單表操作來(lái)說(shuō),mybatis-plus代碼量比mybatis的代碼量少很多,極大的提高了開(kāi)發(fā)效率
2、對(duì)于多表操作來(lái)說(shuō),更推薦mybatis,因?yàn)閙ybatis-plus的方法比較難以理解,用起來(lái)不太方便,不如自己寫(xiě)sql語(yǔ)句的邏輯那么清晰明了
3、mybatis-plus前期準(zhǔn)備(工程將以 H2 作為默認(rèn)數(shù)據(jù)庫(kù)進(jìn)行演示)
1、使用 Spring Initializer快速初始化一個(gè) Spring Boot 工程
2、導(dǎo)入mybatis-plus依賴(lài)
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>spring-latest-version</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>Latest Version</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> </dependencies>
3、yml文件中添加相關(guān)配置
# DataSource Config spring: datasource: driver-class-name: org.h2.Driver schema: classpath:db/schema-h2.sql data: classpath:db/data-h2.sql url: jdbc:h2:mem:test username: root password: test
4、在 Spring Boot 啟動(dòng)類(lèi)中添加 @MapperScan 注解,掃描 Mapper 文件夾
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
5、編寫(xiě)實(shí)體類(lèi)和Mapper類(lèi)
//entity @Data public class User { private Long id; private String name; private Integer age; private String email; } //Mapper public interface UserMapper extends BaseMapper<User> { }
6、service繼承IService
public interface UserService extends IService<User>
7、serviceImpl繼承ServiceImpl
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService
4、mybatis-plus的sql操作(Service層)
1、Save:插入
// 插入一條記錄(選擇字段,策略插入) boolean save(T entity); // 插入(批量) boolean saveBatch(Collection<T> entityList); // 插入(批量) boolean saveBatch(Collection<T> entityList, int batchSize);
2、SaveOrUpdate:修改插入
// TableId 注解存在更新記錄,否插入一條記錄 boolean saveOrUpdate(T entity); // 根據(jù)updateWrapper嘗試更新,否繼續(xù)執(zhí)行saveOrUpdate(T)方法 boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper); // 批量修改插入 boolean saveOrUpdateBatch(Collection<T> entityList); // 批量修改插入 boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
3、Remove:刪除
// 根據(jù) entity 條件,刪除記錄 boolean remove(Wrapper<T> queryWrapper); // 根據(jù) ID 刪除 boolean removeById(Serializable id); // 根據(jù) columnMap 條件,刪除記錄 boolean removeByMap(Map<String, Object> columnMap); // 刪除(根據(jù)ID 批量刪除) boolean removeByIds(Collection<? extends Serializable> idList);
4、Update:更新
// 根據(jù) UpdateWrapper 條件,更新記錄 需要設(shè)置sqlset boolean update(Wrapper<T> updateWrapper); // 根據(jù) whereWrapper 條件,更新記錄 boolean update(T updateEntity, Wrapper<T> whereWrapper); // 根據(jù) ID 選擇修改 boolean updateById(T entity); // 根據(jù)ID 批量更新 boolean updateBatchById(Collection<T> entityList); // 根據(jù)ID 批量更新 boolean updateBatchById(Collection<T> entityList, int batchSize);
5、Get:?jiǎn)误w查詢(xún)
// 根據(jù) ID 查詢(xún) T getById(Serializable id); // 根據(jù) Wrapper,查詢(xún)一條記錄。結(jié)果集,如果是多個(gè)會(huì)拋出異常,隨機(jī)取一條加上限制條件 wrapper.last("LIMIT 1") T getOne(Wrapper<T> queryWrapper); // 根據(jù) Wrapper,查詢(xún)一條記錄 T getOne(Wrapper<T> queryWrapper, boolean throwEx); // 根據(jù) Wrapper,查詢(xún)一條記錄 Map<String, Object> getMap(Wrapper<T> queryWrapper); // 根據(jù) Wrapper,查詢(xún)一條記錄 <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper)
6、List:多條查詢(xún)
// 查詢(xún)所有 List<T> list(); // 查詢(xún)列表 List<T> list(Wrapper<T> queryWrapper); // 查詢(xún)(根據(jù)ID 批量查詢(xún)) Collection<T> listByIds(Collection<? extends Serializable> idList); // 查詢(xún)(根據(jù) columnMap 條件) Collection<T> listByMap(Map<String, Object> columnMap); // 查詢(xún)所有列表 List<Map<String, Object>> listMaps(); // 查詢(xún)列表 List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper); // 查詢(xún)?nèi)坑涗? List<Object> listObjs(); // 查詢(xún)?nèi)坑涗? <V> List<V> listObjs(Function<? super Object, V> mapper); // 根據(jù) Wrapper 條件,查詢(xún)?nèi)坑涗? List<Object> listObjs(Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢(xún)?nèi)坑涗? <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
7、Page:分頁(yè)查詢(xún)(需要導(dǎo)入相關(guān)的配置或依賴(lài))
// 無(wú)條件分頁(yè)查詢(xún) IPage<T> page(IPage<T> page); // 條件分頁(yè)查詢(xún) IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper); // 無(wú)條件分頁(yè)查詢(xún) IPage<Map<String, Object>> pageMaps(IPage<T> page); // 條件分頁(yè)查詢(xún) IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
8、Count:記錄數(shù)據(jù)個(gè)數(shù)
// 查詢(xún)總記錄數(shù) int count(); // 根據(jù) Wrapper 條件,查詢(xún)總記錄數(shù) int count(Wrapper<T> queryWrapper);
5、詳細(xì)資料
由于篇幅有限,先寫(xiě)到這里
具體內(nèi)容請(qǐng)看
1、mybatis-plus官網(wǎng):https://mp.baomidou.com/
2、MyBatis-Plus 通用IService使用詳解
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
SpringBoot2.6.x默認(rèn)禁用循環(huán)依賴(lài)后的問(wèn)題解決
由于SpringBoot從底層逐漸引導(dǎo)開(kāi)發(fā)者書(shū)寫(xiě)規(guī)范的代碼,同時(shí)也是個(gè)憂(yōu)傷的消息,循環(huán)依賴(lài)的應(yīng)用場(chǎng)景實(shí)在是太廣泛了,所以SpringBoot 2.6.x不推薦使用循環(huán)依賴(lài),本文給大家說(shuō)下SpringBoot2.6.x默認(rèn)禁用循環(huán)依賴(lài)后的應(yīng)對(duì)策略,感興趣的朋友一起看看吧2022-02-02Java讀取properties配置文件時(shí),出現(xiàn)中文亂碼的解決方法
下面小編就為大家?guī)?lái)一篇Java讀取properties配置文件時(shí),出現(xiàn)中文亂碼的解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11SpringBoot中配置雙數(shù)據(jù)源的實(shí)現(xiàn)示例
在許多應(yīng)用程序中,可能會(huì)遇到需要連接多個(gè)數(shù)據(jù)庫(kù)的情況,本文主要介紹了SpringBoot中配置雙數(shù)據(jù)源的實(shí)現(xiàn)示例,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08SpringBoot如何根據(jù)用戶(hù)系統(tǒng)時(shí)區(qū)動(dòng)態(tài)展示時(shí)間
這篇文章主要介紹了SpringBoot如何根據(jù)用戶(hù)系統(tǒng)時(shí)區(qū)動(dòng)態(tài)展示時(shí)間,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01springboot整合x(chóng)xl-job的示例代碼
這篇文章主要介紹了springboot整合x(chóng)xl-job的示例代碼,主要分為三大模塊,分別是調(diào)度中心、執(zhí)行器和配置定時(shí)任務(wù)的過(guò)程,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06Java?深入淺出解析面向?qū)ο笾橄箢?lèi)和接口
本章具體介紹了抽象類(lèi)和接口,整篇文章用目前流行的手機(jī)來(lái)舉例,圖解穿插代碼案例。?JAVA成仙路從基礎(chǔ)開(kāi)始講,后續(xù)會(huì)講到JAVA高級(jí),中間會(huì)穿插面試題和項(xiàng)目實(shí)戰(zhàn),希望能給大家?guī)?lái)幫助2022-03-03