Java mybatis-plus詳解
1、簡介
MyBatis-Plus 是一個 Mybatis 增強版工具,在 MyBatis 上擴充了其他功能沒有改變其基本功能,為了簡化開發(fā)提交效率而存在。
2、適用情況
1、對于只進行單表操作來說,mybatis-plus代碼量比mybatis的代碼量少很多,極大的提高了開發(fā)效率
2、對于多表操作來說,更推薦mybatis,因為mybatis-plus的方法比較難以理解,用起來不太方便,不如自己寫sql語句的邏輯那么清晰明了
3、mybatis-plus前期準備(工程將以 H2 作為默認數(shù)據(jù)庫進行演示)
1、使用 Spring Initializer快速初始化一個 Spring Boot 工程
2、導入mybatis-plus依賴
<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文件中添加相關配置
# 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 啟動類中添加 @MapperScan 注解,掃描 Mapper 文件夾
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
5、編寫實體類和Mapper類
//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 條件,更新記錄 需要設置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:單體查詢
// 根據(jù) ID 查詢 T getById(Serializable id); // 根據(jù) Wrapper,查詢一條記錄。結(jié)果集,如果是多個會拋出異常,隨機取一條加上限制條件 wrapper.last("LIMIT 1") T getOne(Wrapper<T> queryWrapper); // 根據(jù) Wrapper,查詢一條記錄 T getOne(Wrapper<T> queryWrapper, boolean throwEx); // 根據(jù) Wrapper,查詢一條記錄 Map<String, Object> getMap(Wrapper<T> queryWrapper); // 根據(jù) Wrapper,查詢一條記錄 <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper)
6、List:多條查詢
// 查詢所有 List<T> list(); // 查詢列表 List<T> list(Wrapper<T> queryWrapper); // 查詢(根據(jù)ID 批量查詢) Collection<T> listByIds(Collection<? extends Serializable> idList); // 查詢(根據(jù) columnMap 條件) Collection<T> listByMap(Map<String, Object> columnMap); // 查詢所有列表 List<Map<String, Object>> listMaps(); // 查詢列表 List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper); // 查詢?nèi)坑涗? List<Object> listObjs(); // 查詢?nèi)坑涗? <V> List<V> listObjs(Function<? super Object, V> mapper); // 根據(jù) Wrapper 條件,查詢?nèi)坑涗? List<Object> listObjs(Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢?nèi)坑涗? <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
7、Page:分頁查詢(需要導入相關的配置或依賴)
// 無條件分頁查詢 IPage<T> page(IPage<T> page); // 條件分頁查詢 IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper); // 無條件分頁查詢 IPage<Map<String, Object>> pageMaps(IPage<T> page); // 條件分頁查詢 IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
8、Count:記錄數(shù)據(jù)個數(shù)
// 查詢總記錄數(shù) int count(); // 根據(jù) Wrapper 條件,查詢總記錄數(shù) int count(Wrapper<T> queryWrapper);
5、詳細資料
由于篇幅有限,先寫到這里
具體內(nèi)容請看
1、mybatis-plus官網(wǎng):https://mp.baomidou.com/
2、MyBatis-Plus 通用IService使用詳解
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!
相關文章
SpringBoot2.6.x默認禁用循環(huán)依賴后的問題解決
由于SpringBoot從底層逐漸引導開發(fā)者書寫規(guī)范的代碼,同時也是個憂傷的消息,循環(huán)依賴的應用場景實在是太廣泛了,所以SpringBoot 2.6.x不推薦使用循環(huán)依賴,本文給大家說下SpringBoot2.6.x默認禁用循環(huán)依賴后的應對策略,感興趣的朋友一起看看吧2022-02-02Java讀取properties配置文件時,出現(xiàn)中文亂碼的解決方法
下面小編就為大家?guī)硪黄狫ava讀取properties配置文件時,出現(xiàn)中文亂碼的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11SpringBoot中配置雙數(shù)據(jù)源的實現(xiàn)示例
在許多應用程序中,可能會遇到需要連接多個數(shù)據(jù)庫的情況,本文主要介紹了SpringBoot中配置雙數(shù)據(jù)源的實現(xiàn)示例,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-08-08SpringBoot如何根據(jù)用戶系統(tǒng)時區(qū)動態(tài)展示時間
這篇文章主要介紹了SpringBoot如何根據(jù)用戶系統(tǒng)時區(qū)動態(tài)展示時間,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01