springboot整合mybatis-plus

文章详细解释了SpringBoot的自动装配原理,包括包扫描和启用自动配置的过程。同时,介绍了如何整合MyBatis-Plus进行数据库操作,从创建数据表、引入依赖、配置数据源、创建实体类和Mapper接口,到实现CRUD操作、条件查询和分页功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.springboot自动装配原理

        1.springboot包扫描原理

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

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

如果需要认为修改扫描包的名称, 需要在主类上加上@conponentScan(basepackage={“包名”}

         1.2springboot自动装配原理

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

2.springboot整合mybatis-plus

        2.1 mybatis-plus概述

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

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

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

         2.2如何使用mybatis-plus

1.创建学生表,并添加数据 

 2.创建一个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.example</groupId>
    <artifactId>springboot04</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot04</name>
    <description>springboot04</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!--        mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3 .配置数据源-数据库连接池

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db3
spring.datasource.username=root
spring.datasource.password=wenqing.

 4.创建实体类

@Data
@ToString
public class Student {
    @TableId(type = IdType.AUTO)
    private int sid;
    private String sname;
    private int age ;
    private int cid;  

}

 5.创建一个mapper接口

@Mapper
public interface StudentMapper extends BaseMapper<Student> {
   
}

6.为接口生成代理实现类

@SpringBootApplication
@MapperScan(basePackages = "com.qwq.mapper")
public class Springboot04Application {

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

}

7.测试  

@SpringBootTest
class Springboot04ApplicationTests {
    @Autowired
    private StudentMapper studentMapper;

    // 根据id查询数据
    @Test
    void selectById() {
        System.out.println(studentMapper.selectById(1));
    }
}

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

 2.3使用mybatis-plus完成CRUD

@SpringBootTest
class Springboot04ApplicationTests {
    @Autowired
    private StudentMapper studentMapper;

    // 根据id查询数据
    @Test
    void selectById() {
        System.out.println(studentMapper.selectById(1));
    }

    //查询所有
    @Test
    void selectAll(){
        system.out.println(studentMapper.selectList(null));
    }


    // 添加数据
    @Test
    void insertStudent(){
        Student student = new Student();
        student.setAge(190);
        student.setSname("shitoudan");
        student.setCid(1);
        System.out.println(studentMapper.insert(student));
    }

    //删除数据
    @Test
    void deleteStudent(){
        //System.out.println(studentMapper.deleteById(1));//删除
        //批量删除
        List<Integer> arrayList = new ArrayList<>();
        arrayList.add(15);
        arrayList.add(16);
        System.out.println(studentMapper.deleteBatchIds(arrayList));
    }

    //修改数据
    @Test
    void updateStudent(){
        Student student = new Student();
        student.setCid(1);
        student.setAge(90);
        student.setSname("wang99999");
        student.setSid(3);
        studentMapper.updateById(student);
    }
    

}

2.4使用mybatis-plus完成条件查询和分页

 @Test
    void findAll(){

        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        //wrapper.ge("age",90);  // ge 大于等于
        // wrapper.gt("age",18);// gt 大于
        wrapper.between("age" , 18,23);  // 年龄在  什么之间
        // wrapper.like("sname","%q%");  // 模糊查询
        wrapper.likeRight("sname","_i");  // 名字第二个字母为 i

        wrapper.select("sname","age");// 只查询sname 和age
        List<Student> studentList = studentMapper.selectList(wrapper);
        studentList.forEach(System.out::println);
    }


 //分页查询,默认mp分页需要添加分页拦截器MybatisPlusConfig
    @Test
    void testPage(){
        Page<Student> page = new Page<>(1, 5);// current表示当前第几页,size:一页共几条
        //分页查询,如果需要添加条件,创建wraaper
        studentMapper.selectPage(page, null);
        System.out.println("当前页的记录:"+ page.getRecords()); //获取当前页的记录
        System.out.println("总页数:"+page.getPages()); //获取总页数
        System.out.println("总条数:"+page.getRecords()); //获取总条数
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值