mybatis-plus的使用

1. springboot自动装配原理

1.1代码块展示@SpringBootConfiguration注解流程

启动类的@SpringBootAppliation注解是一个组合注解

@SpringBootApplication
@MapperScan(basePackages = "com.xyh.mapper") //为mapper接口生成代理实现类
public class MarieBoot2Application {

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

}

其中@EnableAutoConfiguration 注解又分为以下两种注解

@AutoConfigurationPackage
@Import(AutoConfigurationPackages.Registrar.class)  //给容器中导入一个组件
public @interface AutoConfigurationPackage {}

其中@AutoConfigurationPackage 注解则由以下注解构成

@Import(AutoConfigurationPackages.Registrar.class) 
public @interface AutoConfigurationPackage {

1.2注解含义

@SpringBootConfiguration : 标注在某个类上,表示这是一个Spring Boot的配置类;
@ComponentScan : 配置扫描路径,用于加载使用注解格式定义的bean
@EnableAutoConfiguration : 开启自动装配功能

@AutoConfigurationPackage 指定了默认的包规则指定了默认的包规则就是将主程序类所在包及所有子包下的组件扫描到Spring容器中;
@Import(AutoConfigurationImportSelector.class) : 通过 @Import 注解导入 AutoConfigurationImportSelector类,然后通过该类的selectImports方法去读取
MATE-INF/spring.factories文件中配置的组件的全类名,并按照一定的规则过滤掉不符合要求的组件的全类名,将剩余读取到的各个组件的全类名集合返回给IOC容器并将这些组件注册为bean

1.3 springboot包扫描原理

包建议大家放在主类所在包或者子包。默认包扫描的是主类所在的包以及子包。

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

如果需要人为修改扫描包的名称则需要在主类上@ComponentScan(basepackage={"包名"})

1.4 springboot自动装配原理

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

2. springboot整合mybatis-plus

2.1 mybatis-plus概述

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

不能替代mybatis ,以后对于单表操作的所有功能,都可以使用mp完成。但是链表操作的功能还得要校验mybatis.

2.2 如何使用mp

(1)创建一个springboot工程并引入相关的依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xyh</groupId>
    <artifactId>marie-boot3</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>marie-boot3</name>
    <description>marie-boot3</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
</project>


(2)配置数据源

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql:///555

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

(3)创建实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    private Integer smpno;
    private String sname;
    private Date sdate;
    private Integer sno;
}

(4)创建一个dao接口

public interface StudentMapper extends BaseMapper<Student> {
}

(5)为接口生成代理实现类

@SpringBootApplication
@MapperScan(basePackages = "com.xyh.dao")
@EnableSwagger2
public class MarieBoot3Application {
    public static void main(String[] args) {
        SpringApplication.run(MarieBoot3Application.class, args);
    }

}

(6)测试

@SpringBootTest
class MarieBoot3ApplicationTests {

    @Resource
    private StudentMapper studentMapper;

    /**
     * 根据id查询
     */
    @Test
    void findById() {
        Student student = studentMapper.selectById(1);
        System.out.println(student);
    }

总结: 1.引入mp依赖 2. 创建数据源 3. 实体类 4.dao并继承BaseMapper<>接口 5.接口扫描

6.测试

2.3 使用mp完成CRUD

@SpringBootTest
class MarieBoot3ApplicationTests {

    @Resource
    private StudentMapper studentMapper;

    /**
     * 根据id查询
     */
    @Test
    void findById() {
        Student student = studentMapper.selectById(1);
        System.out.println(student);
    }

    /**
     * 删除
     */
    @Test
    void delete(){
        List<Integer> ids = new ArrayList<>();
        ids.add(5);
        ids.add(6);
        int i = studentMapper.deleteBatchIds(ids);
        System.out.println(i);
    }

    /**
     * 插入
     */
    //默认主键的生成策略:雪花算法-唯一id值。
    //使用递增 如果想使用递增策略:必须保证表中的id递增,而且需要在@TableId(type=IdType.AUTO)
    @Test
    void insert(){
        Student xyz = new Student("aaa",new Date(),10);
        int insert = studentMapper.insert(xyz);
        System.out.println(insert);
    }

    /**
     * 修改
     */
    @Test
    void update(){
        Student student = new Student("bbb",new Date(),20);
        student.setSmpno(8);
        studentMapper.updateById(student);
    }
}

2.4 使用mp完成条件查询

@Test
    public void testFind(){
        //Wrapper<T> queryWrapper:查询对象。 条件接口。QUeryWrapper 查询条件类  UpdateWrapper更新条件类  LambdaWrapper 使用lambda表达式
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.likeRight("sname","_y");
        wrapper.or();
        wrapper.between("smpno",4,6);
        wrapper.orderByDesc("smpno");
        wrapper.select("smpno","sname");
        List<Student> list = studentMapper.selectList(wrapper);
        list.forEach(System.out::println);
    }

    /**
     * 分页查询
     */
    //分页需求--PageHelper---默认mp分页需要加分页拦截器
    @Test
    void findPage(){
        Page<Student> page = new Page<>(1,3);
        studentMapper.selectPage(page, null);
        System.out.println("当前页的记录"+page.getRecords());//获取当页的记录
        System.out.println("获取总页数"+page.getPages());//获取当页的记录
        System.out.println("获取总条数"+page.getTotal());//获取当页的记录
    }

2.5 联表使用mp的分页对象

@Test
    void findPage2(){
        Page<Student> page = new Page<>(1,3);
        studentMapper.findPage(page, null);
        System.out.println("当前页的记录"+page.getRecords());//获取当页的记录
        System.out.println("获取总页数"+page.getPages());//获取当页的记录
        System.out.println("获取总条数"+page.getTotal());//获取当页的记录
    }
posted @   北洛1024  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示