mybatis-plus中BaseMapper入門使用
具體教程參考官網(wǎng)文檔: baomidou.com/
入門使用BaseMapper完成增刪改查
根據(jù)數(shù)據(jù)庫表制作相應(yīng)實(shí)體類
@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類
public interface UserMapper extends BaseMapper<User> { //這里什么都不用寫 }
由于BaseMapper已經(jīng)集成了基礎(chǔ)的增刪改查方法,這里對(duì)應(yīng)的mapper.xml也是不用寫的
添加關(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è)試類
@SpringBootTest class MybatisplusdemoApplicationTests { @Autowired UserMapper userMapper; @Test void contextLoads() { User user = userMapper.selectById(7l); userMapper.deleteById(user); System.out.println(user); } }
如果要自定義一些增刪改查方法,可以在配置類中添加:
##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 查詢 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)坑涗?。注意?只返回第一個(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中BaseMapper入門使用的文章就介紹到這了,更多相關(guān)mybatis-plus BaseMapper入門內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mybatis-Plus接口BaseMapper與Services使用詳解
- MybatisPlus?BaseMapper?實(shí)現(xiàn)對(duì)數(shù)據(jù)庫增刪改查源碼
- Mapper層繼承BaseMapper<T>需要引入的pom依賴方式
- mybatis抽取基類BaseMapper增刪改查的實(shí)現(xiàn)
- 淺談Mybatis Plus的BaseMapper的方法是如何注入的
- MybatisPlus BaseMapper 中的方法全部 Invalid bound statement (not found Error處理)
- Mybatis-Plus BaseMapper的用法詳解
- BaseMapper接口的使用方法
相關(guān)文章
spring?boot中spring框架的版本升級(jí)圖文教程
Spring Boot是一款基于Spring框架的快速開發(fā)框架,它提供了一系列的開箱即用的功能和組件,這篇文章主要給大家介紹了關(guān)于spring?boot中spring框架的版本升級(jí)的相關(guān)資料,需要的朋友可以參考下2023-10-10java多線程處理執(zhí)行solr創(chuàng)建索引示例
這篇文章主要介紹了java多線程處理執(zhí)行solr創(chuàng)建索引示例,需要的朋友可以參考下2014-02-02Java聊天室之使用Socket實(shí)現(xiàn)傳遞對(duì)象
這篇文章主要為大家詳細(xì)介紹了Java簡(jiǎn)易聊天室之使用Socket實(shí)現(xiàn)傳遞對(duì)象功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以了解一下2022-10-10mybatis 如何判斷l(xiāng)ist集合是否包含指定數(shù)據(jù)
這篇文章主要介紹了mybatis 判斷l(xiāng)ist集合是否包含指定數(shù)據(jù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06SpringCloud Config分布式配置中心使用教程介紹
springcloud config是一個(gè)解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個(gè)部分,server端提供配置文件的存儲(chǔ)、以接口的形式將配置文件的內(nèi)容提供出去,client端通過接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應(yīng)用2022-12-12Java數(shù)據(jù)結(jié)構(gòu)最清晰圖解二叉樹前 中 后序遍歷
樹是一種重要的非線性數(shù)據(jù)結(jié)構(gòu),直觀地看,它是數(shù)據(jù)元素(在樹中稱為結(jié)點(diǎn))按分支關(guān)系組織起來的結(jié)構(gòu),很象自然界中的樹那樣。樹結(jié)構(gòu)在客觀世界中廣泛存在,如人類社會(huì)的族譜和各種社會(huì)組織機(jī)構(gòu)都可用樹形象表示2022-01-01