springboot整合mybatis-plus

1.回顾

1. springboot概述: 简化spring工程的搭建。
2. springboot配置文件: (1)properties (2)yml 语法格式
3. java代码如何获取配置文件中的内容。
   [1]类上加个注解@ConfigurationProperties(prefix="")
   [2]在属性上@Value("${key}")----只能读取基本和字符串。

4. springboot整合mybatis持久化框架。
   [1]引入starter启动依赖 
   [2]修改配置文件---数据源的信息  映射文件的路径
   [3]dao接口---@Mapper或者@MapperScanner(basepackages="")  

2.大纲

1. springboot自动装配原理---
2. springboot整合mbatis-plus

3.springboot自动装配原理

3.1 springboot包扫描原理

包建议大家放在主类所在包或者子包。默认包扫描的是主类所在的包以及子包。
主函数在运行时会加载一个使用@SpringBootApplication标记的类。而该注解是一个复合注解,包含
@EnableAutoConfiguration,这个注解开启了自动配置功能。 该注解也是一个复合注解,包含
@AutoConfigurationPackage。 该注解中包含@Import({Registrar.class}),这个注解引入Registrar类。
该类中存在registerBeanDefinitions,可以获取扫描的包名。

3.2 springboot自动装配原理

主函数在运行会执行一个使用@SpringbootApplication注解的类,该注解是一个复合注解,包含@EnableAutoConfiguration, 该注解开启自动配置功能,该注解也是一个复合注解,包含@Import() 该注解需要导入AutoConfigurationImportSelector类。 该类会加载很多自动装配类,而这些自动装配类完成相应的自动装配原理。
详细请点击

4.Spring boot整合mbatis-plus

4.1 mbatis-plus概述

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在
MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
![](https://img-blog.csdnimg.cn/8c5205804ca341409e20a7c32ce138e8.png

愿景
我们的愿景是成为 MyBatis 最好的搭档,就像 魂斗罗 中的 1P、2P,基友搭配,效率翻倍。

4.2 特性

无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
内置性能分析插件:可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

4.3 快速使用

(1) 先创建一个spring boot项目

(2) 准备数据源,现有一张 Student表,其表结构如下:
在这里插入图片描述

其对应的数据库脚本如下:
create table student
(
    sid   int auto_increment primary key,
    sname varchar(20) null,
    age   int         null,
    cid   int         null
);

(3) 引入 Spring Boot Starter 父工程:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5+ 版本</version>
    <relativePath/>
</parent>

引入相关依赖:

		<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        //注意:这里引入mybatis-plus依赖,就可以不用引入mybatis依赖
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>最新版本</version>
        </dependency>

(4) 在 application.properties配置文件中添加 MYSQL 数据库的相关配置:

#数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql:///work

(5) 在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 dao文件夹:
在这里插入图片描述
(6) 测试
编写实体类 Student.java(此处使用了 Lombok简化代码)

@Data
public class Student implements Serializable {

//由于mybatis-plus中主键默认是id,这里是sid,所以加上@TableId(type = IdType.AUTO)注解并且递增
    @TableId(type = IdType.AUTO)
    private Integer sid;
    
    private String sname;
    
    private Integer age;

    private Integer cid;

    @TableField(exist = false)
    private Class aaa;

    public Student() {
    }

    public Student(String sname, Integer age, Integer cid) {
        this.sname = sname;
        this.age = age;
        this.cid = cid;
    }
}

编写 dao包下的 StudentDao接口

public interface StudentDao extends BaseMapper<Student> {

}

添加测试类,进行功能测试:

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hm.dao.StudentDao;
import com.hm.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@SpringBootTest
class Springboot03ApplicationTests {

    @Resource
    private StudentDao studentDao;

/*查   根据id查询*/
    @Test
    void contextLoads() {
        Student student = studentDao.selectById(5);
        System.out.println(student);
    }

/*增*/
    @Test
    public void insert(){
        //默认主键的生成策略:雪花算法-唯一-i d值。使用递增如果想使用递增策略:必须保证表中的id递增
        Student student = new Student("啊啊啊",20,2);

        int insert = studentDao.insert(student);
        System.out.println(insert);
    }

/*删*/
    @Test
    public void delete(){
    //单行删除
        /*int i = studentDao.deleteById(8);
        System.out.println(i);*/
    //批量删除
        List<Integer> list = new ArrayList<>();
        list.add(9);
        list.add(10);
        int i = studentDao.deleteBatchIds(list);
        System.out.println(i);
    }

/*改*/
    @Test
    public void update(){
        Student student = new Student("嘎嘎嘎",89,1);
        student.setSid(7);
        studentDao.updateById(student);
    }
}

如果要条件查询和分页查询

/*条件查询*/
    @Test
    public void testFind(){

        //条件接口。QUeryWrapper 查询条件类UpdatelWrapper 更新条件类LambdalWrapper 使用ambda 表达式
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.likeRight("sname","_哈");
        List<Student> list = studentDao.selectList(wrapper);
        list.forEach(System.out::println);
    }
    
/*分页查询*/
    //分页需求--PageHelper---默认mybatis-plus分页需要加分页拦截器
    @Test
    public void testPage(){
        Page<Student> page = new Page<>(1,3);//current:当前第几页  size:每页显示条数
        studentDao.selectPage(page,null);//把查询分页的结果封装到page对象中  条件是null,就是查询全部数据
        System.out.println("当前页的记录"+page.getRecords());//获取当前页的记录
        System.out.println("获取总页数"+page.getPages());//获取当前页的记录
        System.out.println("获取总条数"+page.getTotal());//获取当前页的记录

    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值