mybatis-plus中BaseMapper入門(mén)使用
具體教程參考官網(wǎng)文檔: baomidou.com/
入門(mén)使用BaseMapper完成增刪改查
根據(jù)數(shù)據(jù)庫(kù)表制作相應(yīng)實(shí)體類(lèi)
@TableName(value = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private String password;
private String username;
// 省略set,get
}
創(chuàng)建對(duì)應(yīng)mapper類(lèi)
public interface UserMapper extends BaseMapper<User> {
//這里什么都不用寫(xiě)
}
由于BaseMapper已經(jīng)集成了基礎(chǔ)的增刪改查方法,這里對(duì)應(yīng)的mapper.xml也是不用寫(xiě)的
添加關(guān)于mapper包的注冊(cè)
@SpringBootApplication
@MapperScan("com.hyx.mybatisplusdemo.mapper")
public class MybatisplusdemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusdemoApplication.class, args);
}
}
修改配置文件
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql:///test?useUnicode=true&serverTimezone=GMT%2B8&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456
測(cè)試類(lèi)
@SpringBootTest
class MybatisplusdemoApplicationTests {
@Autowired
UserMapper userMapper;
@Test
void contextLoads() {
User user = userMapper.selectById(7l);
userMapper.deleteById(user);
System.out.println(user);
}
}
如果要自定義一些增刪改查方法,可以在配置類(lèi)中添加:
##mybatis-plus mapper xml 文件地址 mybatis-plus.mapper-locations= classpath*:mapper/*Mapper.xml ##mybatis-plus type-aliases 文件地址 mybatis-plus.type-aliases-package= com.hyx.mybatisplusdemo.entity
然后就與mybatis一樣,創(chuàng)建對(duì)應(yīng)的xml文件,去實(shí)現(xiàn)相應(yīng)的方法就可以了
BaseMapper各方法詳解
Insert
// 插入一條記錄 int insert(T 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);
Update
// 根據(jù) whereEntity 條件,更新記錄 int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper); // 根據(jù) ID 修改 int updateById(@Param(Constants.ENTITY) T entity);
Select
// 根據(jù) ID 查詢(xún) T selectById(Serializable id); // 根據(jù) entity 條件,查詢(xún)一條記錄 T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查詢(xún)(根據(jù)ID 批量查詢(xún)) List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); // 根據(jù) entity 條件,查詢(xún)?nèi)坑涗? List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查詢(xún)(根據(jù) columnMap 條件) List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); // 根據(jù) Wrapper 條件,查詢(xún)?nèi)坑涗? List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢(xún)?nèi)坑涗?。注意?只返回第一個(gè)字段的值 List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據(jù) entity 條件,查詢(xún)?nèi)坑涗洠ú⒎?yè)) IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢(xún)?nèi)坑涗洠ú⒎?yè)) IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢(xún)總記錄數(shù) Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
到此這篇關(guān)于mybatis-plus中BaseMapper入門(mén)使用的文章就介紹到這了,更多相關(guān)mybatis-plus BaseMapper入門(mén)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mybatis-Plus接口BaseMapper與Services使用詳解
- MybatisPlus?BaseMapper?實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)增刪改查源碼
- Mapper層繼承BaseMapper<T>需要引入的pom依賴(lài)方式
- mybatis抽取基類(lèi)BaseMapper增刪改查的實(shí)現(xiàn)
- 淺談Mybatis Plus的BaseMapper的方法是如何注入的
- MybatisPlus BaseMapper 中的方法全部 Invalid bound statement (not found Error處理)
- Mybatis-Plus BaseMapper的用法詳解
- BaseMapper接口的使用方法
相關(guān)文章
Java 中的 Class.forName(類(lèi)名) 使用及原理解析
Class.forName是Java中用于動(dòng)態(tài)加載類(lèi)的強(qiáng)大工具,廣泛應(yīng)用于數(shù)據(jù)庫(kù)驅(qū)動(dòng)加載、反射機(jī)制和插件系統(tǒng)等場(chǎng)景,它通過(guò)ClassLoader加載類(lèi)并執(zhí)行靜態(tài)初始化代碼,但在使用時(shí)需要注意類(lèi)路徑、初始化副作用和類(lèi)加載器的選擇等問(wèn)題,感興趣的朋友一起看看吧2024-12-12
JavaWeb之Ajax的基本使用與實(shí)戰(zhàn)案例
ajax技術(shù)是使頁(yè)面能局部刷新的一種技術(shù),下面這篇文章主要給大家介紹了關(guān)于JavaWeb之Ajax的基本使用與實(shí)戰(zhàn)案例的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
SpringBoot配置數(shù)據(jù)庫(kù)密碼加密的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot配置數(shù)據(jù)庫(kù)密碼加密的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
springboot + rabbitmq 如何實(shí)現(xiàn)消息確認(rèn)機(jī)制(踩坑經(jīng)驗(yàn))
這篇文章主要介紹了springboot + rabbitmq 如何實(shí)現(xiàn)消息確認(rèn)機(jī)制,本文給大家分享小編實(shí)際開(kāi)發(fā)中的一點(diǎn)踩坑經(jīng)驗(yàn),內(nèi)容簡(jiǎn)單易懂,需要的朋友可以參考下2020-07-07
Core Java 簡(jiǎn)單談?wù)凥ashSet(推薦)
下面小編就為大家?guī)?lái)一篇Core Java 簡(jiǎn)單談?wù)凥ashSet(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
淺析Java中為什么要設(shè)計(jì)包裝類(lèi)
我們知道Java是一個(gè)面相對(duì)象的編程語(yǔ)言,基本類(lèi)型并不具有對(duì)象的性質(zhì),為了讓基本類(lèi)型也具有對(duì)象的特征,就出現(xiàn)了包裝類(lèi)型,它相當(dāng)于將基本類(lèi)型“包裝起來(lái)”,使得它具有了對(duì)象的性質(zhì),并且為其添加了屬性和方法,豐富了基本類(lèi)型的操作2021-06-06
Java 過(guò)濾器實(shí)現(xiàn)敏感詞匯過(guò)濾功能
通過(guò)使用 Java 過(guò)濾器,我們可以輕松地實(shí)現(xiàn)敏感詞匯過(guò)濾的功能,以保護(hù)用戶(hù)免受不良內(nèi)容的侵害,讓我們通過(guò)一個(gè)簡(jiǎn)單的示例來(lái)演示我們的敏感詞匯過(guò)濾器是如何工作的,感興趣的朋友一起看看吧2024-01-01
Java語(yǔ)言描述MD5加密工具類(lèi)實(shí)例代碼
這篇文章主要介紹了Java語(yǔ)言描述MD5加密工具類(lèi)實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12

