MyBatisPlus实现增删改查
实体类GkUser
package com.geekmice.springbootselfexercise.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.ToString;
import org.nustaq.serialization.annotations.Serialize;
/**
* (GkUser)实体类
*
* @author pingmingbo
* @since 2024-06-21 14:05:12
*/
@TableName(value = "gk_user")
@Data
@ToString
@Serialize
public class GkUser{
/**
* 主键ID
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 姓名
*/
@TableField(value = "name")
private String name;
/**
* 年龄
*/
@TableField(value = "age")
private Integer age;
/**
* 邮箱
*/
@TableField(value = "email")
private String email;
}
数据层GkUserDao
package com.geekmice.springbootselfexercise.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.geekmice.springbootselfexercise.entity.GkUser;
/**
* (GkUser)表数据库访问层
*
* @author pingmingbo
* @since 2024-06-21 14:05:12
*/
public interface GkUserDao extends BaseMapper<GkUser> {
}
映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.geekmice.springbootselfexercise.dao.GkUserDao">
<resultMap type="com.geekmice.springbootselfexercise.entity.GkUser" id="GkUserMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="age" column="age" jdbcType="INTEGER"/>
<result property="email" column="email" jdbcType="VARCHAR"/>
</resultMap>
</mapper>
业务层GkUserService
package com.geekmice.springbootselfexercise.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.geekmice.springbootselfexercise.entity.GkUser;
/**
* (GkUser)表服务接口
*
* @author pingmingbo
* @since 2024-06-21 14:05:12
*/
public interface GkUserService extends IService<GkUser> {
}
package com.geekmice.springbootselfexercise.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geekmice.springbootselfexercise.dao.GkUserDao;
import com.geekmice.springbootselfexercise.entity.GkUser;
import com.geekmice.springbootselfexercise.service.GkUserService;
import org.springframework.stereotype.Service;
/**
* (GkUser)表服务实现类
*
* @author pingmingbo
* @since 2024-06-21 14:05:12
*/
@Service("gkUserService")
public class GkUserServiceImpl extends ServiceImpl<GkUserDao, GkUser> implements GkUserService {
}
基本操作
package com.geekmice.springbootselfexercise.first;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.geekmice.springbootselfexercise.SpringBootSelfExerciseApplication;
import com.geekmice.springbootselfexercise.dao.GkUserDao;
import com.geekmice.springbootselfexercise.entity.GkUser;
import com.geekmice.springbootselfexercise.service.GkUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* @BelongsProject: spring-boot-self-exercise
* @BelongsPackage: com.geekmice.springbootselfexercise.first
* @Author: pingmingbo
* @CreateTime: 2024-07-09 10:12
* @Description: mybatisplus实现增删改查
* @Version: 1.0
*/
@SpringBootTest(classes = SpringBootSelfExerciseApplication.class)
@RunWith(SpringRunner.class)
@Slf4j
public class FirstTest {
@Resource
GkUserDao gkUserDao;
@Resource
GkUserService gkUserService;
@Test
public void t1() {
// 初始数据
// id name age email
// 1 Jone 18 test1@baomidou.com
// 2 Jack 20 test2@baomidou.com
// 3 Tom 28 test3@baomidou.com
// 4 Sandy 21 test4@baomidou.com
// 5 Billie 24 test5@baomidou.com
// 1、查询
GkUser gkUser = gkUserDao.selectById(1);
log.info("1.1、根据主键id查询 gkUser : [{}]", gkUser);
List<GkUser> gkUsers = gkUserDao.selectList(null);
log.info("1.2、查询所有 gkUsers : [{}]", CollectionUtils.size(gkUsers));
// todo 分页查询 通过分页插件PaginationInnerInterceptor实现
// 批量查询
List<Integer> list = new ArrayList(16);
list.add(1);
list.add(2);
List<GkUser> batchUsers = gkUserDao.selectBatchIds(list);
log.info("1.3、批量主键查询 batchUsers : [{}]", CollectionUtils.size(batchUsers));
// 根据条件批量查询
QueryWrapper<GkUser> queryWrapper = new QueryWrapper<>();
queryWrapper.le("id", 2);
List<GkUser> gkUsers1 = gkUserDao.selectList(queryWrapper);
log.info("1.4、根据条件批量查询 gkUsers1 : [{}]", CollectionUtils.size(gkUsers1));
// 2、增加
GkUser insertGkUser = gkUserDao.selectById(6);
if (Objects.isNull(insertGkUser)) {
GkUser domain = new GkUser();
domain.setAge(10);
domain.setEmail("test6@baomidou.com");
domain.setName("Rose");
gkUserDao.insert(domain);
}
GkUser afterInsertGkUser = gkUserDao.selectById(6);
log.info("2.1 添加数据 afterInsertGkUser : [{}]", afterInsertGkUser);
// 3、修改
GkUser updateGkUser = gkUserDao.selectById(6);
GkUser domain = new GkUser();
if (Objects.nonNull(updateGkUser)) {
domain.setId(6L);
domain.setAge(6);
domain.setEmail("test66@baomidou.com");
domain.setName("rose");
gkUserDao.updateById(domain);
log.info("3.1 更新一条数据 domain ");
}
System.out.println("aaa");
UpdateWrapper<GkUser> singleUpdateWrapper = new UpdateWrapper<>();
singleUpdateWrapper.lambda().in(GkUser::getId,list);
singleUpdateWrapper.set("name","abc");
gkUserService.update(singleUpdateWrapper);
log.info("3.2 定制化修改");
// 4、删除
// 定制化条件删除
QueryWrapper<GkUser> deleteWrapper = new QueryWrapper<>();
deleteWrapper.lambda().le(GkUser::getId,1);
gkUserService.remove(deleteWrapper);
log.info("4.1 根据条件删除");
gkUserService.removeById(2);
log.info("4.2 根据主键删除");
}
}
分页查询
配置分页插件
package com.geekmice.springbootselfexercise.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @BelongsProject: spring-boot-self-exercise
* @BelongsPackage: com.geekmice.springbootselfexercise.config
* @Author: pingmingbo
* @CreateTime: 2023-08-09 09:29
* @Description: mybatis配置信息
* @Version: 1.0
*/
@Configuration
@MapperScan(value = "com.geekmice.springbootselfexercise.dao")
public class MybatisPlusConfig {
/**
* 分页插件配置
*/
@Bean(name = "mybatisPlusInterceptor")
public MybatisPlusInterceptor mybatisPlusInterceptor (){
MybatisPlusInterceptor interceeptor = new MybatisPlusInterceptor();
interceeptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceeptor;
}
}
如何使用
Page<GkUser> gkUserPage = new Page<>(1,3);
Page<GkUser> records = gkUserDao.selectPage(gkUserPage, null);
log.info("records : [{}]" , records);
> Preparing: SELECT id,name,age,email FROM gk_user LIMIT ?
> Parameters: 3(Long)
< Columns: id, name, age, email
< Row: 3, Tom, 28, test3@baomidou.com
<== Row: 4, Sandy, 21, test4@baomidou.com
<== Row: 5, Billie, 24, test5@baomidou.com
<== Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@67376bae]
2024-07-10 01:37:14.271 INFO 9164 — [ main] c.g.s.first.FirstTest : records : [com.baomidou.mybatisplus.extension.plugins.pagination.Page@135a8808]
2024-07-10 01:37:14.790 INFO 9164 — [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated…
2024-07-10 01:37:14.873 INFO 9164 — [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
Process finished with exit code 0