使用Mybatis-plus清空表數(shù)據(jù)的操作方法
方法一:
public interface userInfoMapper extends BaseMapper<UserInfo> { //清空指定表 @Update("truncate table user") void deleteUserTemp(); }
方法二:
直接使用自帶的remove接口 ,同時(shí)使用QueryWrapper參數(shù)如:
userInfoTempService.remove(new QueryWrapper<>())
引申一下 Mybatis-plus這個(gè)好用的框架:
我們知道 MyBatis 是一個(gè)基于 java 的持久層框架,它內(nèi)部封裝了 jdbc,極大提高了我們的開發(fā)效率。
但是使用 Mybatis 開發(fā)也有很多痛點(diǎn):
每個(gè) Dao 接口都需要自己定義一堆增刪改查方法:
public interface UserDao { // 獲取所有用戶信息 List<User> getUserList(); // 根絕 id 獲取用戶信息 User getUserById(int id); // 新增用戶信息 boolean add(User user); // 更新用戶信息 boolean update(User user); // 刪除用戶信息 boolean delete(int id); }
每個(gè) Mapper 文件都需要寫一堆
基本的增刪改查語(yǔ)句。
3.如果查詢的列表需要分頁(yè),我們還需要給查詢方法封裝成分頁(yè)對(duì)象。
你可能會(huì)說:Mybatis 還能有痛點(diǎn)?用著多方便!
對(duì)于小項(xiàng)目而言,用著確實(shí)還行。但是遇到大項(xiàng)目,光 Dao 接口都有幾百個(gè),如果還要手動(dòng)定義一堆增刪改查方法和 sql 語(yǔ)句,那也很浪費(fèi)時(shí)間。
那有沒有這樣一個(gè)框架:
1.封裝了 Mybatis,自帶 CRUD 方法,我們不需要自己定義 CRUD 方法。
2.提供各種查詢方法,不需要在 mapper 文件中寫一些基礎(chǔ)的 sql 語(yǔ)句。
3.封裝了分頁(yè)功能,讓分頁(yè)查詢無比絲滑。
有的,MybatisPlus 閃亮登場(chǎng)。
依賴:
<!-- 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é)果集,如果是多個(gè)會(huì)拋出異常,隨機(jī)取一條加上限制條件 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 條件,更新記錄 需要設(shè)置 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
// 無條件分頁(yè)查詢 IPage<T> page(IPage<T> page); // 條件分頁(yè)查詢 IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper); // 無條件分頁(yè)查詢 IPage<Map<String, Object>> pageMaps(IPage<T> page); // 條件分頁(yè)查詢 IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
Count
// 查詢總記錄數(shù) int count(); // 根據(jù) Wrapper 條件,查詢總記錄數(shù) int count(Wrapper<T> queryWrapper);
query
// 鏈?zhǔn)讲樵?普通 QueryChainWrapper<T> query(); // 鏈?zhǔn)讲樵?lambda 式。注意:不支持 Kotlin LambdaQueryChainWrapper<T> lambdaQuery(); // 示例: query().eq("column", value).one(); lambdaQuery().eq(Entity::getId, value).list();
update
// 鏈?zhǔn)礁?普通 UpdateChainWrapper<T> update(); // 鏈?zhǔn)礁?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)坑涗洝W⒁猓?只返回第一個(gè)字段的值 List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據(jù) entity 條件,查詢?nèi)坑涗洠ú⒎?yè)) IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢?nèi)坑涗洠ú⒎?yè)) 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
RocketMQ的消費(fèi)者類型與最佳實(shí)踐詳解
這篇文章主要介紹了RocketMQ的消費(fèi)者類型與最佳實(shí)踐詳解,在?RocketMQ?5.0?中,更加強(qiáng)調(diào)了客戶端類型的概念,尤其是消費(fèi)者類型,為了滿足多樣的?RocketMQ?中一共有三種不同的消費(fèi)者類型,分別是?PushConsumer、SimpleConsumer?和?PullConsumer,需要的朋友可以參考下2023-10-10SpringSecurity 自定義認(rèn)證登錄的項(xiàng)目實(shí)踐
本文主要介紹了SpringSecurity 自定義認(rèn)證登錄的項(xiàng)目實(shí)踐,以手機(jī)驗(yàn)證碼登錄為例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08Java精品項(xiàng)目瑞吉外賣之登陸的完善與退出功能篇
這篇文章主要為大家詳細(xì)介紹了java精品項(xiàng)目-瑞吉外賣訂餐系統(tǒng),此項(xiàng)目過大,分為多章獨(dú)立講解,本篇內(nèi)容為新增菜品和分頁(yè)查詢功能的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05spring,mybatis事務(wù)管理配置與@Transactional注解使用詳解
這篇文章主要介紹了spring,mybatis事務(wù)管理配置與@Transactional注解使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07IDEA項(xiàng)目如何實(shí)現(xiàn)打jar包
這篇文章主要介紹了IDEA項(xiàng)目如何實(shí)現(xiàn)打jar包問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Java結(jié)構(gòu)型設(shè)計(jì)模式之適配器模式詳解
適配器模式,即將某個(gè)類的接口轉(zhuǎn)換成客戶端期望的另一個(gè)接口的表示,主要目的是實(shí)現(xiàn)兼容性,讓原本因?yàn)榻涌诓黄ヅ?,沒辦法一起工作的兩個(gè)類,可以協(xié)同工作。本文將通過示例詳細(xì)介紹適配器模式,需要的可以參考一下2022-09-09