Springboot-2

1. springboot自动装配原理

1.1 springboot包扫描原理

包建议放在主类所在包或者子包。默认包扫描的是主类所在的包以及子包。
主函数运行时会加载使用@SpringBootApplication标记的类,-->包含
@SpringBootApplication-->@EnableAutoConfiguration-->@AutoConfigurationPackage-->@Import({Registrar.class})-->registerBeanDefinitions
@SpringBootApplication-->@EnableAutoConfiguration都是复合注解
@EnableAutoConfiguration//开启了自动配置功能
@Import({Registrar.class}),这个注解引入了Registrar类
Registrar类可以获取扫描的包名。
如果需要人为修改扫描包的名称则需要在主类上@ComponentScan(basepackage={"包名"})

1.2 springboot自动装配原理

自己使用DispatcherServlet了吗?. 为什么DispatcherServlet能用。???
主函数运行时会加载使用@SpringBootApplication标记的类,-->包含
@SpringBootApplication-->@EnableAutoConfiguration-->@Import()导入AutoConfigurationImportSelector类
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 如何使用mp

(1)创建表并加入数据

DELETE FROM user;

INSERT INTO user (id, name, age, email) VALUES
(1, 'aa', 11, 'test1@baomidou.com'),
(2, 'ab', 12, 'test2@baomidou.com'),
(3, 'ac', 13, 'test3@baomidou.com'),
(4, 'ad', 14, 'test4@baomidou.com'),
(5, 'ae', 15, 'test5@baomidou.com');

(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.ykq</groupId>
    <artifactId>qy163-springboot03</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>qy163-springboot03</name>
    <description>Demo project for Spring Boot</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>
    </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.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql:///test1

(4)创建实体类

@Data
@NoArgsConstructor//无参
@AllArgsConstructor//有参
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

(5)创建一个dao接口继承BaseMapper

public interface UserMapper extends BaseMapper<Student> {
}

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

@SpringBootApplication//@SpringBootApplication注解一般放在项目的一个启动类上,
用来把启动类注入到容器中,用来定义容器扫描的范围,用来加载classpath环境中一些bean.
@MapperScan(basePackages = "com.zjy.dao")//扫描
public class Qy163Springboot03Application {
    public static void main(String[] args) {
        SpringApplication.run(Qy163Springboot03Application.class, args);
    }

}

(7)测试

2.3 使用mp完成CRUD

@SpringBootTest
class Springboot2ApplicationTests {
    @Resource
    private Mapper mapper;

    @Test
    void findAll() {
        student student = mapper.selectById(1);//根据主键查询id
        System.out.println(student);
    }
    @Test
    public void deleteById(){
        List<Integer> is=new ArrayList<>();
        is.add(2);
        is.add(3);
        is.add(5);
        is.add(6);
        int i = Mapper.deleteBatchIds(is);
        System.out.println(i);
    }

    //默认主键的生成策略:雪花算法-唯一id值。
    //使用递增 如果想使用递增策略:必须保证表中的id递增,而且需要在@TableId(type=IdType.AUTO)
      实体类id上面加@TableId(type=IdType.AUTO)
    @Test
    void insert(){
        student student = new student("ay",new Date(),20);
        System.out.println(mapper.insert(student));
    }
    @Test
    void update(){
        student student = new student();
        student.setSmpno(0);
        student.setSname("atm");
        student.setSdate(new Date());
        student.setSno(20);
        System.out.println(mapper.updateById(student));
    }

2.4 使用mp完成条件查询

 @Test
    void testFind() {
        QueryWrapper<student> o = new QueryWrapper<>();
        o.likeRight("sname", "D");
        o.between("sno",20,40);
        List<student> students1 = mapper.selectList(o);
        students1.forEach(System.out::println);
    }

    //分页需求--PageHelper---默认mp分页需要加分页拦截器
   @Test
    void Page(){
    Page<student> page = new Page<>(1,3);
    mapper.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);
        mapper.all(page, null);
        System.out.println("当前页的记录"+page.getRecords());//获取当页的记录
        System.out.println("获取总页数"+page.getPages());//获取当页的记录
        System.out.println("获取总条数"+page.getTotal());//获取当页的记录
    }
posted @   海绵小方块  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示