使用Mybatis-plus清空表數(shù)據(jù)的操作方法
方法一:
public interface userInfoMapper extends BaseMapper<UserInfo> { //清空指定表 @Update("truncate table user") void deleteUserTemp(); }
方法二:
直接使用自帶的remove接口 ,同時使用QueryWrapper參數(shù)如:
userInfoTempService.remove(new QueryWrapper<>())
引申一下 Mybatis-plus這個好用的框架:
我們知道 MyBatis 是一個基于 java 的持久層框架,它內(nèi)部封裝了 jdbc,極大提高了我們的開發(fā)效率。
但是使用 Mybatis 開發(fā)也有很多痛點:
每個 Dao 接口都需要自己定義一堆增刪改查方法:
public interface UserDao { // 獲取所有用戶信息 List<User> getUserList(); // 根絕 id 獲取用戶信息 User getUserById(int id); // 新增用戶信息 boolean add(User user); // 更新用戶信息 boolean update(User user); // 刪除用戶信息 boolean delete(int id); }
每個 Mapper 文件都需要寫一堆
基本的增刪改查語句。
3.如果查詢的列表需要分頁,我們還需要給查詢方法封裝成分頁對象。
你可能會說:Mybatis 還能有痛點?用著多方便!
對于小項目而言,用著確實還行。但是遇到大項目,光 Dao 接口都有幾百個,如果還要手動定義一堆增刪改查方法和 sql 語句,那也很浪費時間。
那有沒有這樣一個框架:
1.封裝了 Mybatis,自帶 CRUD 方法,我們不需要自己定義 CRUD 方法。
2.提供各種查詢方法,不需要在 mapper 文件中寫一些基礎的 sql 語句。
3.封裝了分頁功能,讓分頁查詢無比絲滑。
有的,MybatisPlus 閃亮登場。
依賴:
<!-- mybatis-plus 依賴--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency>
-----------------------------------------------------------------------------------------------------------
MybatisPlus常用API-增刪改查
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);
Save
// 插入一條記錄(選擇字段,策略插入) boolean save(T entity); // 插入(批量) boolean saveBatch(Collection<T> entityList); // 插入(批量) boolean saveBatch(Collection<T> entityList, int batchSize);
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);
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);
Update
// 根據(jù) UpdateWrapper 條件,更新記錄 需要設置 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);
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);
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);
Count
// 查詢總記錄數(shù) int count(); // 根據(jù) Wrapper 條件,查詢總記錄數(shù) int count(Wrapper<T> queryWrapper);
query
// 鏈式查詢 普通 QueryChainWrapper<T> query(); // 鏈式查詢 lambda 式。注意:不支持 Kotlin LambdaQueryChainWrapper<T> lambdaQuery(); // 示例: query().eq("column", value).one(); lambdaQuery().eq(Entity::getId, value).list();
update
// 鏈式更改 普通 UpdateChainWrapper<T> update(); // 鏈式更改 lambda 式。注意:不支持 Kotlin LambdaUpdateChainWrapper<T> lambdaUpdate(); // 示例: update().eq("column", value).remove(); lambdaUpdate().eq(Entity::getId, value).update(entity);
Delete
// 根據(jù) entity 條件,刪除記錄 int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper); // 刪除(根據(jù)ID 批量刪除) int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); // 根據(jù) ID 刪除 int deleteById(Serializable id); // 根據(jù) columnMap 條件,刪除記錄 int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
Select
// 根據(jù) ID 查詢 T selectById(Serializable id); // 根據(jù) entity 條件,查詢一條記錄 T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查詢(根據(jù)ID 批量查詢) List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); // 根據(jù) entity 條件,查詢?nèi)坑涗? List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查詢(根據(jù) columnMap 條件) List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); // 根據(jù) Wrapper 條件,查詢?nèi)坑涗? List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢?nèi)坑涗?。注意?只返回第一個字段的值 List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據(jù) entity 條件,查詢?nèi)坑涗洠ú⒎摚? IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢?nèi)坑涗洠ú⒎摚? IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢總記錄數(shù) Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
到此這篇關(guān)于使用Mybatis-plus清空表數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Mybatis-plus清空表數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring,mybatis事務管理配置與@Transactional注解使用詳解
這篇文章主要介紹了spring,mybatis事務管理配置與@Transactional注解使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07