BaseMapper接口的使用方法
3、相關(guān)方法
3.1、BaseMapper接口
MyBatis-Plus中的基本CRUD在內(nèi)置的 BaseMapper 中都已得到了實(shí)現(xiàn),我們可以直接使用,這樣大大的簡(jiǎn)化了開(kāi)發(fā)效率。
/**
* Mapper 繼承該接口后,無(wú)需編寫(xiě) mapper.xml 文件,即可獲得CRUD功能
*/
public interface BaseMapper<T> extends Mapper<T> {
//插入一條記錄
int insert(T entity);
//根據(jù) ID 刪除
int deleteById(Serializable id);
//根據(jù)實(shí)體(ID)刪除
int deleteById(T entity);
//根據(jù) columnMap 條件,刪除記錄
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// 刪除記錄,條件生成器根據(jù)entity生成where后的條件
int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
//刪除(根據(jù)ID或?qū)嶓w 批量刪除)
int deleteBatchIds(@Param(Constants.COLL) Collection<?> idList);
// 根據(jù) ID 修改
int updateById(@Param(Constants.ENTITY) T entity);
// 根據(jù) whereEntity 條件,更新記錄
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
//根據(jù) ID 查詢
T selectById(Serializable id);
//查詢(根據(jù)ID 批量查詢)
List<T> selectBatchIds(@Param(Constants.COLL) Collection<? extends Serializable> idList);
// 查詢(根據(jù) columnMap 條件)
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
//查詢一條記錄
default T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
.......
}
//根據(jù) Wrapper 條件,判斷是否存在記錄
default boolean exists(Wrapper<T> queryWrapper) {
.....
}
// 根據(jù) Wrapper 條件,查詢總記錄數(shù)
Long selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
//根據(jù) entity 條件,查詢?nèi)坑涗?
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
//根據(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)坑涗洠ú⒎?yè))
<P extends IPage<T>> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
//根據(jù) Wrapper 條件,查詢?nèi)坑涗洠ú⒎?yè))
<P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}3.1.1、新增
在封裝的 BaseMapper 中只有一個(gè)插入語(yǔ)句,因?yàn)楹A繑?shù)據(jù)插入單條SQL無(wú)法實(shí)行,所以mybatis-plus 把批量插入放在了Service 層對(duì)應(yīng)的封裝接口里了。

@Resource
private UserMapper userMapper;
@Test
public void test2(){
//新增數(shù)據(jù)
User user1 = new User();
user1.setUsername("mybatisplus測(cè)試");
user1.setPassword("12312312");
int insert = userMapper.insert(user1);
System.out.println("受影響行數(shù):"+insert);
//自動(dòng)獲取id(默認(rèn)使用雪花算法生成的id)
Integer id = user1.getUserId();
//獲取數(shù)據(jù)
User user = userMapper.selectById(id);
System.out.println(user);
}
這個(gè)ID是MyBatis-Plus基于雪花算法生成的一個(gè)ID。之所以會(huì)為負(fù)數(shù),是因?yàn)閿?shù)據(jù)庫(kù)設(shè)置的是自增,而在User實(shí)體類(lèi)中沒(méi)有進(jìn)行設(shè)置
//IdType.ID_WORKER_STR 默認(rèn)的;底層使用了雪花算法;類(lèi)型為Integer //IdType.AUTO 數(shù)據(jù)庫(kù)自增;數(shù)據(jù)庫(kù)上必須設(shè)置為自增 //IdType.NONE 沒(méi)有設(shè)置主鍵類(lèi)型;跟隨全局;全局的主鍵策略如果沒(méi)有設(shè)置,默認(rèn)是雪花算法 //IdType.INPUT 手動(dòng)輸入;必須手動(dòng)輸入,數(shù)據(jù)庫(kù)自增也沒(méi)用; //IdType.UUID 全局唯一id;無(wú)序;字符串; //ID_WORKER_STR 全局唯一(idWorker的字符串表示); @TableId(value = "user_id",type = IdType.AUTO) private Integer userId;
設(shè)置完成后,將數(shù)據(jù)庫(kù)之前測(cè)試的數(shù)據(jù)刪除,重新運(yùn)行一下

3.1.2、刪除

數(shù)據(jù)

刪除一行數(shù)據(jù)
//通過(guò)id刪除一行數(shù)據(jù)
int i = userMapper.deleteById(15);
System.out.println("受影響行數(shù):"+i);
批量刪除
//批量刪除
List<Integer> idList = Arrays.asList(16,17, 18);
int i1 = userMapper.deleteBatchIds(idList);
System.out.println("受影響行數(shù):"+i1 );
通過(guò)Map設(shè)置條件
//根據(jù)條件刪除
Map<String, Object> map = new HashMap<>();
map.put("username","新增");
map.put("password","456454");
//刪除username為新增,且password為456454的數(shù)據(jù)
int i = userMapper.deleteByMap(map);
System.out.println("受影響行數(shù):"+ i );
3.1.3、修改

User user = userMapper.selectById(11);
System.out.println(user);
//對(duì)user進(jìn)行修改
user.setUsername("修改");
user.setPassword("2131278");
int i = userMapper.updateById(user);
System.out.println("受影響行數(shù):"+ i );
//修改后的User
User user1 = userMapper.selectById(11);
System.out.println(user1);
3.1.4、查詢

查詢一條數(shù)據(jù)
//根據(jù)ID查詢 User user = userMapper.selectById(1); System.out.println(user); //根據(jù) LambdaQueryWrapper 的條件查詢 User user1 = userMapper.selectOne(new LambdaQueryWrapper<User>().like(User::getUsername,"root")); System.out.println(user1);

根據(jù)ID批量查詢
//根據(jù)ID批量查詢 List<Integer> list = Arrays.asList(1, 2, 3); List<User> users = userMapper.selectBatchIds(list); users.forEach(System.out::println);

通過(guò)map條件查詢,鍵值對(duì)應(yīng)數(shù)據(jù)的字段和值。
//通過(guò)map條件查詢
Map<String , Object> map = new HashMap<>();
map.put("username","root");
List<User> users = userMapper.selectByMap(map);
users.forEach(System.out::println);
查詢所有數(shù)據(jù)
//查詢所有數(shù)據(jù),參數(shù)為null List<User> users = userMapper.selectList(null); users.forEach(System.out::println);

到此這篇關(guān)于BaseMapper接口的使用的文章就介紹到這了,更多相關(guān)BaseMapper接口的使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mybatis-Plus接口BaseMapper與Services使用詳解
- MybatisPlus?BaseMapper?實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)增刪改查源碼
- Mapper層繼承BaseMapper<T>需要引入的pom依賴方式
- mybatis抽取基類(lèi)BaseMapper增刪改查的實(shí)現(xiàn)
- 淺談Mybatis Plus的BaseMapper的方法是如何注入的
- mybatis-plus中BaseMapper入門(mén)使用
- MybatisPlus BaseMapper 中的方法全部 Invalid bound statement (not found Error處理)
- Mybatis-Plus BaseMapper的用法詳解
相關(guān)文章
windows下java環(huán)境變量的設(shè)置方法
在“系統(tǒng)變量”中,設(shè)置3項(xiàng)屬性,JAVA_HOME,PATH,CLASSPATH(大小寫(xiě)無(wú)所謂),若已存在則點(diǎn)擊“編輯”,不存在則點(diǎn)擊“新建”2013-09-09
SpringBoot中的靜態(tài)資源訪問(wèn)的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot中的靜態(tài)資源訪問(wèn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java中RestTemplate調(diào)用API的幾種常用寫(xiě)法
本文介紹了RestTemplate調(diào)用http的幾種寫(xiě)法,包括post+get,及傳遞參數(shù)的方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-10-10
SpringBoot2.0整合jackson配置日期格式化和反序列化的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot2.0整合jackson配置日期格式化和反序列化的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
使用Java和PostgreSQL存儲(chǔ)向量數(shù)據(jù)的實(shí)現(xiàn)指南
在當(dāng)今的數(shù)字化時(shí)代,數(shù)據(jù)存儲(chǔ)的方式和技術(shù)正變得越來(lái)越復(fù)雜和多樣化,隨著機(jī)器學(xué)習(xí)和數(shù)據(jù)科學(xué)的發(fā)展,向量數(shù)據(jù)的存儲(chǔ)和管理變得尤為重要,本文將詳細(xì)介紹如何使用 Java 和 PostgreSQL 數(shù)據(jù)庫(kù)來(lái)存儲(chǔ)向量數(shù)據(jù),需要的朋友可以參考下2024-09-09

