SpringBoot整合Mybatis-Plus的增删改查操作
插入操作
1. 根据entity条件插入一条记录(insert)
方法定义
/**
* 插入一条记录
*
* @param entity 实体对象
*/
int insert(T entity);
@Test
public void testInsert(){
User user = new User();
user.setAge(20);
user.setEmail("test@itcast.cn");
user.setName("曹操");
user.setUserName("caocao");
user.setPassword("123456");
int result = this.userMapper.insert(user); //返回后的result是受影响的行数,不是自增后的id
System.out.println("result = " + result);
System.out.println(user.getId()); //自增后的id会返回到对象中
}
MP支持的id策略
package com.baomidou.mybatisplus.annotation;
import lombok.Getter;
/**
* 生成ID类型枚举类
*
* @author hubin
* @since 2015-11-10
*/
@Getter
public enum IdType {
/**
* 数据库ID自增
*/
AUTO(0),
/**
* 该类型为未设置主键类型
*/
NONE(1),
/**
* 用户输入ID
* <p>该类型可以通过自己注册自动填充插件进行填充</p>
*/
INPUT(2),
/* 以下3种类型、只有当插入对象ID 为空,才自动填充。 */
/**
* 全局唯一ID (idWorker)
*/
ID_WORKER(3),
/**
* 全局唯一ID (UUID)
*/
UUID(4),
/**
* 字符串全局唯一ID (idWorker 的字符串表示)
*/
ID_WORKER_STR(5);
private final int key;
IdType(int key) {
this.key = key;
}
}
2. @TableField用法
在MP中通过@TableField注解可以指定字段的一些属性,常常解决的问题有2个:
1、对象中的属性名和字段名不一致的问题(非驼峰)
2、对象中的属性字段在表中不存在的问题
@TableField(select = false) //字段不加入查询字段
private String password;
@TableField(value = "email") //解决字段名不一致
private String mail;
@TableField(exist = false)
private String address; //该数据在字段表中不存在
更新操作
1. 根据id进行更新(updateById)
方法定义:
/**
* 根据 ID 修改
*
* @param entity 实体对象
*/
int updateById(@Param(Constants.ENTITY) T entity);
@Test
public void testUpdateById(){
User user = new User();
//ID为6 类型为Long
user.setId(6L);
user.setAge(25); //修改的数据
this.userMapper.updateById(user); //根据id更新不为null的字段
}
2. 根据条件更新(update)
方法定义:
/**
* 根据 whereEntity 条件,更新记录
*
* @param entity 实体对象 (set 条件值,可以为 null)
* @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
*/
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T>
updateWrapper);
@Test
public void testUpdate(){
User user = new User();
user.setAge(34); //更新的字段
//更新的条件
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("id",6);
//执行更新操作
int result = this.userMapper.update(user, wrapper);
System.out.println("result = " + result);
}
@Test
public void testUpdate() {
//更新的条件以及字段
UpdateWrapper<User> wrapper = new UpdateWrapper<>();
wrapper.eq("id", 6).set("age", 23);
//执行更新操作
int result = this.userMapper.update(null, wrapper);
System.out.println("result = " + result);
}
删除操作
1. 根据id进行删除(deleteById)
方法定义:
/**
* 根据 ID 删除
*
* @param id 主键ID
*/
int deleteById(Serializable id);
@Test
public void testDeleteById(){
int result = this.userMapper.deleteById(6);
System.out.println("result = " + result);
}
2. 根据columnMap条件进行删除(deleteByMap)
方法定义
/**
* 根据 columnMap 条件,删除记录
*
* @param columnMap 表字段 map 对象
*/
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
@Test
public void testDeleteByMap(){
HashMap<String, Object> columnMap = new HashMap<>();
columnMap.put("age",18);
columnMap.put("name","张三");
//将columnMap中的元素设置为删除的条件,多个之间为and关系
int result = this.userMapper.deleteByMap(columnMap);
System.out.println("result = " + result);
}
3. 根据entity条件进行删除(delete)
方法定义:
/**
* 根据 entity 条件,删除记录
*
* @param wrapper 实体对象封装操作类(可以为 null)
*/
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
@Test
public void testDelete(){
User user = new User();
user.setAge(23);
user.setName("曹操"); //and
//将实体对象进行包装,包装为操作条件
QueryWrapper<User> wrapper = new QueryWrapper<>(user);
int result = this.userMapper.delete(wrapper);
System.out.println("result = " + result);
}
4. 根据id批量删除(deleteBatchIds)
方法定义:
/**
* 删除(根据ID 批量删除)
*
* @param idList 主键ID列表(不能为 null 以及 empty)
*/
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable>
idList);
@Test
public void testDeleteBatchIds(){ //批量删除测试
//根据id集合批量删除
int result = this.userMapper.deleteBatchIds(Arrays.asList(4L, 8L));
System.out.println("result = " + result);
}
查询操作
1. 根据id进行查询(selectById)
方法定义
/**
* 根据 ID 查询
*
* @param id 主键ID
*/
T selectById(Serializable id);
@Test
public void testSelectById(){
//根据id查数据
User user = this.userMapper.selectById(2L);
System.out.println("result = " + user);
}
2. 根据id批量查询(selectBatchIds)
方法定义
/**
* 查询(根据ID 批量查询)
*
* @param idList 主键ID列表(不能为 null 以及 empty)
*/
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable>
idList);
@Test
public void testSelectBatchIds(){
//根据id集合批量查询
List<User> users = this.userMapper.selectBatchIds(Arrays.asList(2L, 3L, 5L));
for (User user: users){
System.out.println(user);
}
}
3. 根据entity条件查询一条记录(selectOne)
方法定义
/**
* 根据 entity 条件,查询一条记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
@Test
public void testSelectOne(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name","曹操");
//根据条件查询一条数据,如果结果超过一条会报错
User user = this.userMapper.selectOne(wrapper);
System.out.println(user);
}
4. 根据wrapper条件查询总记录数(selectCount)
方法定义
/**
* 根据 Wrapper 条件,查询总记录数
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
@Test
public void testSelectCount(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.gt("age",21);
//根据条件查询数据条数
Integer count = this.userMapper.selectCount(wrapper);
System.out.println(count);
}
5. 根据entity条件查询全部记录(selectList)
方法定义
/**
* 根据 entity 条件,查询全部记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<
@Test
public void testSelectList(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.gt("age",23); //年龄大于23
//根据条件查询数据
List<User> users = this.userMapper.selectList(wrapper);
for (User user: users){
System.out.println("user = " + user);
}
}
6. 根据entity条件查询全部记录并翻页(selectPage)
方法定义
/**
* 根据 entity 条件,查询全部记录(并翻页)
*
* @param page 分页查询条件(可以为 RowBounds.DEFAULT)
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
配置分页插件
package com.example.mybatisplus;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.example.mybatisplus.mapper")
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
@Test
public void testSelectPage(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.gt("age",19); //年龄大于20
Page<User> page = new Page<>(1, 4);
//根据条件查询数据
IPage<User> iPage = this.userMapper.selectPage(page, wrapper);
System.out.println("数据总条数:" + iPage.getTotal());
System.out.println("总页数:" + iPage.getPages());
List<User> users = iPage.getRecords();
for (User user: users){
System.out.println(user);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~