国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

使用Mybatis-plus清空表數(shù)據(jù)

這篇具有很好參考價(jià)值的文章主要介紹了使用Mybatis-plus清空表數(shù)據(jù)。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

方法一:

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 閃亮登場。
依賴:

<!-- 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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • mybatis-plus分頁查詢?nèi)N方法

    mybatis-plus分頁查詢?nèi)N方法

    說明: 1、mybatis-plus中分頁接口需要包含一個(gè)IPage類型的參數(shù)。 2、多個(gè)實(shí)體參數(shù),需要添加@Param參數(shù)注解,方便在xml中配置sql時(shí)獲取參數(shù)值。 注意這里我雖然加了@Param但是我并沒有使用 這是控制臺打印的查詢語句,大家發(fā)現(xiàn)最后的LIMIT 函數(shù)沒,正常來說mybatis-plus里是沒有寫

    2024年01月25日
    瀏覽(19)
  • 【MyBatis-Plus】MyBatis進(jìn)階使用

    【MyBatis-Plus】MyBatis進(jìn)階使用

    目錄 一、MyBatis-Plus簡介 1.1 介紹 1.2 優(yōu)點(diǎn) 1.3 結(jié)構(gòu) 二、MyBatis-Plus基本使用 2.1 配置 2.2 代碼生成 2.3 CRUD接口測試 三、MyBatis-Plus策略詳解 3.1 主鍵生成策略 3.2 雪花ID生成器 3.3 字段自動(dòng)填充策略 3.4 邏輯刪除 四、MyBatis-Plus插件使用 4.1?樂觀鎖插件 4.1.1?什么是樂觀鎖和悲觀鎖? 4.

    2024年02月04日
    瀏覽(21)
  • Mybatis-plus 使用

    Mybatis-plus 使用

    ? ? ? ? mybatis-plus提供了 ·@TableName·, @TableId, @TableField,?@TableLogic 四種注解,其含義分別為: ?@TableName ? ? ? ? ?@TableName(\\\"SPF_Require_Vehicle\\\")? ? 用于聲明當(dāng)前class所對應(yīng)數(shù)據(jù)庫中的表,如果class的名字和表的名字完全相同,則不需要添加該注解,如果不一樣,則需要用該注解進(jìn)

    2024年02月07日
    瀏覽(23)
  • Mybatis 與Mybatis-plus同時(shí)引入同一個(gè)項(xiàng)目中配置方法

    原項(xiàng)目系統(tǒng)中已經(jīng)引入了 Mybatis 和 pagehelper ,當(dāng)引入Mybatis-plus 后啟動(dòng)項(xiàng)目報(bào)錯(cuò) SqlSessionFactory 錯(cuò)誤,排查原因后為依賴沖突導(dǎo)致,需排除部分jar包,并修改相關(guān)配置。 1.mybatis中 2. pagehelper 中 3.引入Mybatis-plus 4.引入autoconfigure 5. 修改配置文件,將原 mybatis 改成 mybatis-plus。根據(jù)自

    2024年02月16日
    瀏覽(91)
  • mybatis-plus分頁total為0,分頁失效,mybatis-plus多租戶插件使用

    背景:項(xiàng)目使用mybatis分頁插件不生效,以及多租戶使用時(shí)讀取配置異常 多租戶插件使用遇到的問題: 最開始在MyTenantLineHandler中使用 @Value(\\\"${tables}\\\"),服務(wù)啟動(dòng)時(shí)能從配置中心拉取到配置,但在運(yùn)行時(shí)獲取到的值為空,試了很多方法都不生效,后面將配置中心的配置在調(diào)用My

    2024年02月06日
    瀏覽(22)
  • Mybatis-plus的使用

    Mybatis-plus的使用 一、簡介 Mybatis-plus的基于mybatis的,簡化了單表mybatis的操作。 注意:它并沒有提升性能,只是簡化了開發(fā)過程。 二、在springboot中的基本使用 1、導(dǎo)入依賴 2、添加相應(yīng)的數(shù)據(jù)庫配置(application.properties) 3、在Application類上添加dao接口的路徑掃描 4、編寫實(shí)體類

    2024年02月09日
    瀏覽(20)
  • mybatis-plus數(shù)據(jù)權(quán)限實(shí)現(xiàn)

    數(shù)據(jù)權(quán)限是平臺系統(tǒng)中不可分割的一部分,在mybatis框架中,大部分都是基于mybatis攔截器進(jìn)行數(shù)據(jù)權(quán)限的插入,有的將數(shù)據(jù)權(quán)限參數(shù)作為XML的標(biāo)簽,有的是基于注解方式,但是不管這兩種方式如何,都必須在攔截器中處理自己解析SQL,稍有不慎或者說沒解析到就會出現(xiàn)各種奇

    2024年02月05日
    瀏覽(27)
  • spring boot集成mybatis-plus——Mybatis Plus 查詢數(shù)據(jù)(圖文講解)

    spring boot集成mybatis-plus——Mybatis Plus 查詢數(shù)據(jù)(圖文講解)

    ?更新時(shí)間 2023-01-03 16:07:12 大家好,我是小哈。 本小節(jié)中,我們將學(xué)習(xí)如何通過 Mybatis Plus 查詢數(shù)據(jù)庫表中的數(shù)據(jù)。 在前面小節(jié)中,我們已經(jīng)定義好了一個(gè)用于測試的用戶表, 執(zhí)行腳本如下: 定義一個(gè)名為? User ?實(shí)體類: 不明白 Mybatis Plus 實(shí)體類注解的小伙伴,可參考前面

    2024年02月02日
    瀏覽(23)
  • Mybatis-plus框架使用配置

    MyBatis-Plus(簡稱 MP)是一個(gè)基于 MyBatis 的增強(qiáng)工具,它對 Mybatis 的基礎(chǔ)功能進(jìn)行了增強(qiáng),但未做任何改變。使得我們可以可以在 Mybatis 開發(fā)的項(xiàng)目上直接進(jìn)行升級為 Mybatis-plus,正如它對自己的定位,它能夠幫助我們進(jìn)一步簡化開發(fā)過程,提高開發(fā)效率。 Mybatis-Plus 其實(shí)可以看

    2024年02月09日
    瀏覽(23)
  • Mybatis-plus動(dòng)態(tài)數(shù)據(jù)源

    Mybatis-plus動(dòng)態(tài)數(shù)據(jù)源

    由于服務(wù)沒有做微服務(wù)部署,需要在后臺管理系統(tǒng)訪問其他服務(wù)的庫,所以需要用到動(dòng)態(tài)數(shù)據(jù)源切換 引入依賴 mybatis-plus動(dòng)態(tài)數(shù)據(jù)源依賴 更改配置 配置類 添加注解 @DS注解我一般放在dao層,因?yàn)橛X得這樣更合理 啟動(dòng)測試 問題: 動(dòng)態(tài)數(shù)據(jù)源切換時(shí)效 當(dāng)服務(wù)層接口添加事務(wù)注解

    2024年04月12日
    瀏覽(42)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包