Java mybatis-plus详解
目錄
- 1、簡介
- 2、適用情況
- 3、mybatis-plus前期準(zhǔn)備(工程將以 H2 作為默認(rèn)數(shù)據(jù)庫進(jìn)行演示)
- 1、使用 Spring Initializer快速初始化一個 Spring Boot 工程
- 2、導(dǎo)入mybatis-plus依賴
- 3、yml文件中添加相關(guān)配置
- 4、在 Spring Boot 啟動類中添加 @MapperScan 注解,掃描 Mapper 文件夾
- 5、編寫實體類和Mapper類
- 6、service繼承IService
- 7、serviceImpl繼承ServiceImpl
- 4、mybatis-plus的sql操作(Service層)
- 1、Save:插入
- 2、SaveOrUpdate:修改插入
- 3、Remove:刪除
- 4、Update:更新
- 5、Get:單體查詢
- 6、List:多條查詢
- 7、Page:分頁查詢(需要導(dǎo)入相關(guān)的配置或依賴)
- 8、Count:記錄數(shù)據(jù)個數(shù)
- 5、詳細(xì)資料
- 總結(jié)
1、簡介
MyBatis-Plus 是一個 Mybatis 增強版工具,在 MyBatis 上擴充了其他功能沒有改變其基本功能,為了簡化開發(fā)提交效率而存在。
2、適用情況
1、對于只進(jìn)行單表操作來說,mybatis-plus代碼量比mybatis的代碼量少很多,極大的提高了開發(fā)效率
2、對于多表操作來說,更推薦mybatis,因為mybatis-plus的方法比較難以理解,用起來不太方便,不如自己寫sql語句的邏輯那么清晰明了
3、mybatis-plus前期準(zhǔn)備(工程將以 H2 作為默認(rèn)數(shù)據(jù)庫進(jìn)行演示)
1、使用 Spring Initializer快速初始化一個 Spring Boot 工程
2、導(dǎo)入mybatis-plus依賴
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <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)配置
| 1 2 3 4 5 6 7 8 9 | # 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 文件夾
| 1 | @MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper") |
5、編寫實體類和Mapper類
| 1 2 3 4 5 6 7 8 9 10 11 12 | //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
| 1 | public interface UserService extends IService<User> |
7、serviceImpl繼承ServiceImpl
| 1 | public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService |
4、mybatis-plus的sql操作(Service層)
1、Save:插入
| 1 2 3 4 5 6 | // 插入一條記錄(選擇字段,策略插入) boolean save(T entity); // 插入(批量) boolean saveBatch(Collection<T> entityList); // 插入(批量) boolean saveBatch(Collection<T> entityList, int batchSize); |
2、SaveOrUpdate:修改插入
| 1 2 3 4 5 6 7 8 | // 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:刪除
| 1 2 3 4 5 6 7 8 | // 根據(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:更新
| 1 2 3 4 5 6 7 8 9 10 | // 根據(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:單體查詢
| 1 2 3 4 5 6 7 8 9 10 | // 根據(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:多條查詢
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // 查詢所有 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)坑涗?/p> List<Object> listObjs(); // 查詢?nèi)坑涗?/p> <V> List<V> listObjs(Function<? super Object, V> mapper); // 根據(jù) Wrapper 條件,查詢?nèi)坑涗?/p> List<Object> listObjs(Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢?nèi)坑涗?/p> <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper); |
7、Page:分頁查詢(需要導(dǎo)入相關(guān)的配置或依賴)
| 1 2 3 4 5 6 7 8 | // 無條件分頁查詢 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ù)
| 1 2 3 4 | // 查詢總記錄數(shù) int count(); // 根據(jù) Wrapper 條件,查詢總記錄數(shù) int count(Wrapper<T> queryWrapper); |
5、詳細(xì)資料
由于篇幅有限,先寫到這里
具體內(nèi)容請看
1、mybatis-plus官網(wǎng):Redirect
2、MyBatis-Plus 通用IService使用詳解
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注!
總結(jié)
以上是生活随笔為你收集整理的Java mybatis-plus详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020美容师(初级)操作证考试及美容师
- 下一篇: Proteus和Keil两个软件的联合使