栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot 3.x- MybatisPlus集成

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot 3.x- MybatisPlus集成

系列文章目录

系列文章:Spring Boot 3.x 系列教程


文章目录
  • 系列文章目录
  • 简介
  • 一、快速开始
    • 1.数据库表和数据准备
    • 2.新增项目&导入依赖
    • 3.配置
    • 4.开发编码
    • 5.运行测试
  • 二、注解
  • 三、测试
  • 四、CRUD
    • Service CRUD
    • Mapper CRUD
  • 五、完整CRUD例子


简介

官网介绍:

MyBatis-Plus (简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。


一、快速开始

数据使用mysql

1.数据库表和数据准备

DROP TABLE IF EXISTS `tb_student`;
CREATE TABLE `tb_student` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  `name` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '姓名',
  `sex` int(1) DEFAULT NULL COMMENT '性别',
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  `grade` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '年级',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

数据

INSERT INTO `tb_student` VALUES (1, '张三', 1, 14, '初中');
INSERT INTO `tb_student` VALUES (2, '李四', 1, 16, '高中');
INSERT INTO `tb_student` VALUES (3, '王五', 1, 20, '大学');
INSERT INTO `tb_student` VALUES (5, '小红', 0, 20, '大学');
INSERT INTO `tb_student` VALUES (8, '大牛', 1, 30, '博士');
2.新增项目&导入依赖

使用 SpringInitializr新增项目

导入依赖

添加mybatisplus依赖

    
            mysql
            mysql-connector-java
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.1
        
在这里插入代码片
3.配置

在application.yaml中添加数据库相关配置

spring:
  datasource:
    #数据库驱动完整类名
    driver-class-name: com.mysql.jdbc.Driver
    #数据库连接url
    url: jdbc:mysql://127.0.0.1:3306/spring-boot-data-learn
    #数据库用户名
    username: root
    #数据库密码
    password: 123456

启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:

@SpringBootApplication
@MapperScan(basePackages = "com.example.springbootmybatisplusdemo.mapper")
public class SpringBootMybatisplusDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatisplusDemoApplication.class, args);
    }

}
4.开发编码

1.新建表对应的实体类Student

@Data
//表名与实体类名称不一致需要手动指定表名
@TableName("tb_student")
public class Student implements Serializable {
    private Integer id;
    private String name;
    private Integer sex;
    private Integer age;
    private String grade;
}

2.mapper文件夹下新建StudentMapper接口

public interface StudentMapper extends BaseMapper {
}
5.运行测试
@SpringBootTest
class SpringBootMybatisplusDemoApplicationTests {

    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void testSelectList() {
        System.out.println(("----- selectAll method test ------"));
        List userList = studentMapper.selectList(null);
        userList.forEach(System.out::println);
    }

}

控制台输出:

Student(id=1, name=张三, sex=1, age=14, grade=初中)
Student(id=2, name=李四, sex=1, age=16, grade=高中)
Student(id=3, name=王五, sex=1, age=20, grade=大学)
Student(id=5, name=小红, sex=0, age=20, grade=大学)
Student(id=8, name=大牛, sex=1, age=30, grade=博士)
二、注解

注解详细内容参见官网:MybatisPlus注解

注解功能
@TableName标示实体对应的表
@TableId主键注解
@TableField字段 注解非主键
@Version乐观锁注解
@EnumValue普通枚举类注解(注解在枚举字段上)
@TableLogic表字段逻辑处理注解(逻辑删除)
@OrderBy内置 SQL 默认指定排序,优先级低于 wrapper 条件查询
三、测试

自动导入 MyBatis-Plus 测试所需相关配置,通过 @MybatisPlusTest 注解快速配置测试类。

@MybatisPlusTest
//因为使用的是外部数据库,不是嵌入式数据库,因此需要加入此注解
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class MybatisPlusTestDemo {

    @Autowired
    private StudentMapper studentMapper;

    @Test
    void testSelectAll() {
        Student student = new Student();
        student.setName("赵六");
        student.setSex(1);
        student.setAge(40);
        student.setGrade("博士");
        studentMapper.insert(student);
        assertThat(student.getId()).isNotNull();
    }
}

四、CRUD

官方文档地址:CRUD

Service CRUD

官网定义:

通用 Service CRUD 封装IService 接口,进一步封装 CRUD 采用 get 查询单行 remove 删除 list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆,

// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
// 插入(批量)
boolean saveBatch(Collection entityList);
// 插入(批量)
boolean saveBatch(Collection entityList, int batchSize);

// 根据 entity 条件,删除记录
boolean remove(Wrapper queryWrapper);
// 根据 ID 删除
boolean removeById(Serializable id);
// 根据 columnMap 条件,删除记录
boolean removeByMap(Map columnMap);
// 删除(根据ID 批量删除)
boolean removeByIds(Collection idList);

// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
boolean update(Wrapper updateWrapper);
// 根据 whereWrapper 条件,更新记录
boolean update(T updateEntity, Wrapper whereWrapper);
// 根据 ID 选择修改
boolean updateById(T entity);
// 根据ID 批量更新
boolean updateBatchById(Collection entityList);
// 根据ID 批量更新
boolean updateBatchById(Collection entityList, int batchSize);

// 根据 ID 查询
T getById(Serializable id);
// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
T getOne(Wrapper queryWrapper);
// 根据 Wrapper,查询一条记录
T getOne(Wrapper queryWrapper, boolean throwEx);
// 根据 Wrapper,查询一条记录
Map getMap(Wrapper queryWrapper);
// 根据 Wrapper,查询一条记录
 V getObj(Wrapper queryWrapper, Function mapper);

//查询所有
List list();
// 查询列表
List list(Wrapper queryWrapper);
// 查询(根据ID 批量查询)
Collection listByIds(Collection idList);
// 查询(根据 columnMap 条件)
Collection listByMap(Map columnMap);
// 查询所有列表
List> listMaps();
// 查询列表
List> listMaps(Wrapper queryWrapper);
// 查询全部记录
List listObjs();
// 查询全部记录
 List listObjs(Function mapper);
// 根据 Wrapper 条件,查询全部记录
List listObjs(Wrapper queryWrapper);
// 根据 Wrapper 条件,查询全部记录
 List listObjs(Wrapper queryWrapper, Function mapper);

//分页查
// 无条件分页查询
IPage page(IPage page);
// 条件分页查询
IPage page(IPage page, Wrapper queryWrapper);
// 无条件分页查询
IPage> pageMaps(IPage page);
// 条件分页查询
IPage> pageMaps(IPage page, Wrapper queryWrapper);
 
Mapper CRUD 

官网定义:

通用 CRUD 封装BaseMapper (opens new window)接口,为 Mybatis-Plus 启动时自动解析实体表关系映射转换为 Mybatis 内部对象注入容器

// 插入一条记录
int insert(T entity);

// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper wrapper);
// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection idList);
// 根据 ID 删除
int deleteById(Serializable id);
// 根据 columnMap 条件,删除记录
int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap);

/ 根据 whereWrapper 条件,更新记录
int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper whereWrapper);
// 根据 ID 修改
int updateById(@Param(Constants.ENTITY) T entity);

// 根据 ID 查询
T selectById(Serializable id);
// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);

// 查询(根据ID 批量查询)
List selectBatchIds(@Param(Constants.COLLECTION) Collection idList);
// 根据 entity 条件,查询全部记录
List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper);
// 查询(根据 columnMap 条件)
List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap);
// 根据 Wrapper 条件,查询全部记录
List> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper);
// 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper);

// 根据 entity 条件,查询全部记录(并翻页)
IPage selectPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper);
// 根据 Wrapper 条件,查询全部记录(并翻页)
IPage> selectMapsPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);
 
五、完整CRUD例子 

entity:

@Data
@TableName("tb_student")
public class Student implements Serializable {
    private Integer id;
    private String name;
    private Integer sex;
    private Integer age;
    private String grade;
}

mapper:

public interface StudentMapper extends BaseMapper {
}

service接口:

public interface IStudentService extends IService {
}

service实现:

@Service("studentService")
public class StudentServiceImpl extends ServiceImpl implements IStudentService {
}

测试:

 @Autowired
    private IStudentService studentService;

    @Test
    public  void testCRUD(){
       Student student= studentService.getById(5);
       System.out.println(student);
       studentService.removeById(5);

        Student student1 = new Student();
        student.setName("赵六");
        student.setSex(1);
        student.setAge(40);
        student.setGrade("博士");
       studentService.save(student1);
    }

项目结构

源代码


转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号