方法一:
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 文件都需要寫一堆
基本的增刪改查語句。
3.如果查詢的列表需要分頁,我們還需要給查詢方法封裝成分頁對象。
你可能會說:Mybatis 還能有痛點(diǎn)?用著多方便!
對于小項(xiàng)目而言,用著確實(shí)還行。但是遇到大項(xiàng)目,光 Dao 接口都有幾百個(gè),如果還要手動(dòng)定義一堆增刪改查方法和 sql 語句,那也很浪費(fèi)時(shí)間。
那有沒有這樣一個(gè)框架:
1.封裝了 Mybatis,自帶 CRUD 方法,我們不需要自己定義 CRUD 方法。
2.提供各種查詢方法,不需要在 mapper 文件中寫一些基礎(chǔ)的 sql 語句。
3.封裝了分頁功能,讓分頁查詢無比絲滑。
有的,MybatisPlus 閃亮登場。
依賴:文章來源:http://www.zghlxwxcb.cn/news/detail-408371.html
<!-- mybatis-plus 依賴-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
?-----------------------------------------------------------------------------------------------------------文章來源地址http://www.zghlxwxcb.cn/news/detail-408371.html
MybatisPlus常用API-增刪改查
?Get
// 根據(jù) ID 查詢
T getById(Serializable id);
// 根據(jù) Wrapper,查詢一條記錄。結(jié)果集,如果是多個(gè)會拋出異常,隨機(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
// 無條件分頁查詢
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
// 鏈?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)坑涗洠ú⒎摚?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ù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!