這篇文章主要為大家展示了“Java中mybatis-plus怎么用”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Java中mybatis-plus怎么用”這篇文章吧。
MyBatis-Plus 是一個 Mybatis 增強版工具,在 MyBatis 上擴充了其他功能沒有改變其基本功能,為了簡化開發提交效率而存在。
1、對于只進行單表操作來說,mybatis-plus代碼量比mybatis的代碼量少很多,極大的提高了開發效率
2、對于多表操作來說,更推薦mybatis,因為mybatis-plus的方法比較難以理解,用起來不太方便,不如自己寫sql語句的邏輯那么清晰明了
<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.h3database</groupId> <artifactId>h3</artifactId> <scope>runtime</scope> </dependency> </dependencies>
# DataSource Config spring: datasource: driver-class-name: org.h3.Driver schema: classpath:db/schema-h3.sql data: classpath:db/data-h3.sql url: jdbc:h3:mem:test username: root password: test
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.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> { }
public interface UserService extends IService<User>
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService
// 插入一條記錄(選擇字段,策略插入) boolean save(T entity); // 插入(批量) boolean saveBatch(Collection<T> entityList); // 插入(批量) boolean saveBatch(Collection<T> entityList, int batchSize);
// TableId 注解存在更新記錄,否插入一條記錄 boolean saveOrUpdate(T entity); // 根據updateWrapper嘗試更新,否繼續執行saveOrUpdate(T)方法 boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper); // 批量修改插入 boolean saveOrUpdateBatch(Collection<T> entityList); // 批量修改插入 boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
// 根據 entity 條件,刪除記錄 boolean remove(Wrapper<T> queryWrapper); // 根據 ID 刪除 boolean removeById(Serializable id); // 根據 columnMap 條件,刪除記錄 boolean removeByMap(Map<String, Object> columnMap); // 刪除(根據ID 批量刪除) boolean removeByIds(Collection<? extends Serializable> idList);
// 根據 UpdateWrapper 條件,更新記錄 需要設置sqlset boolean update(Wrapper<T> updateWrapper); // 根據 whereWrapper 條件,更新記錄 boolean update(T updateEntity, Wrapper<T> whereWrapper); // 根據 ID 選擇修改 boolean updateById(T entity); // 根據ID 批量更新 boolean updateBatchById(Collection<T> entityList); // 根據ID 批量更新 boolean updateBatchById(Collection<T> entityList, int batchSize);
// 根據 ID 查詢 T getById(Serializable id); // 根據 Wrapper,查詢一條記錄。結果集,如果是多個會拋出異常,隨機取一條加上限制條件 wrapper.last("LIMIT 1") T getOne(Wrapper<T> queryWrapper); // 根據 Wrapper,查詢一條記錄 T getOne(Wrapper<T> queryWrapper, boolean throwEx); // 根據 Wrapper,查詢一條記錄 Map<String, Object> getMap(Wrapper<T> queryWrapper); // 根據 Wrapper,查詢一條記錄 <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper)
// 查詢所有 List<T> list(); // 查詢列表 List<T> list(Wrapper<T> queryWrapper); // 查詢(根據ID 批量查詢) Collection<T> listByIds(Collection<? extends Serializable> idList); // 查詢(根據 columnMap 條件) Collection<T> listByMap(Map<String, Object> columnMap); // 查詢所有列表 List<Map<String, Object>> listMaps(); // 查詢列表 List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper); // 查詢全部記錄 List<Object> listObjs(); // 查詢全部記錄 <V> List<V> listObjs(Function<? super Object, V> mapper); // 根據 Wrapper 條件,查詢全部記錄 List<Object> listObjs(Wrapper<T> queryWrapper); // 根據 Wrapper 條件,查詢全部記錄 <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
// 無條件分頁查詢 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);
// 查詢總記錄數 int count(); // 根據 Wrapper 條件,查詢總記錄數 int count(Wrapper<T> queryWrapper);
以上是“Java中mybatis-plus怎么用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。