spring自动装配原理

1. 正文

1. springboot自动装配原理---
2. springboot整合mbatis-plus
3. springboot整合swagger2
4. springboot整合定时器

2. springboot自动装配原理

2.1 springboot包扫描原理

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

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

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

2.2 springboot自动装配原理

思考: 有没有自己使用DispatcherServlet. 为什么DispatcherServlet能用。

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

3. springboot整合mybatis-plus

3.1 mybatis-plus概述

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

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

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

3.2 如何使用mp

(1)创建表并加入数据

DELETE FROM student;

INSERT INTO student(sid, sname, age, cid) VALUES
(1, 'Jone', 18, '1'),
(2, 'Jack', 20, '2m'),
(3, 'Tom', 28, '3'),
(4, 'Sandy', 21, '4m'),
(5, 'Billie', 24, '5');

(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=root
spring.datasource.url=jdbc:mysql:///homework

(4)创建实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "student")
public class Student implements Serializable {
    @TableId(type = IdType.AUTO)
    private Integer sid;

    @TableField(value = "SName")
    private String sname;

    private Integer age;

    private Integer cid;
    @TableField(exist = false)
    private Class aClass;

}

(5)创建一个dao接口

public interface StudentDao extends BaseMapper<Student> {
    IPage<Student> findPage(IPage<Student> iPage, @Param("ew") Wrapper<Student> wrapper);//mybatis-plus支持单表查询,多表查询需要写方法

}

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

@SpringBootApplication
@MapperScan(basePackages = "com.lht.dao")
public class Lht411SpringbootApplication {

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

}


(7)测试

```java

@SpringBootTest
class Lht411SpringbootApplicationTests {

 
    @Autowired
    private StudentDao studentDao;
    @Test
    void findBySid() {
        Student student = studentDao.selectById(1);
        System.out.println(student);
    }

}

3.3 使用mp完成CRUD

@SpringBootTest
class Lht411SpringbootApplicationTests {

     @Autowired
    private StudentDao studentDao;
    @Test
    void findBySid() {
        Student student = studentDao.selectById(1);
        System.out.println(student);
    }

    @Test
    void findAll() {
        List<Student> students = studentDao.selectList(null);
        System.out.println(students);
    }
    
    /**
     * 添加
     */
    @Test
    void insert() {
        Student student1 = new Student();
        student1.setSname("asd");
        student1.setAge(16);
        student1.setCid(1);
        int insert = studentDao.insert(student1);
        System.out.println(insert);
    }
    /**
     * 更新
     */
    @Test
    void update() {
        Student student1 = new Student();
        student1.setSid(30);
        student1.setSname("aaaaaaaaaaaaaaaaaaaa");
        student1.setAge(16);
        student1.setCid(1);
        int i = studentDao.updateById(student1);
        System.out.println(i);
    }

    /**
     * 删除
     */
    @Test
    void delete() {
        int i = studentDao.deleteById(29);
        System.out.println(i);
    }
    /**
     * 根据ID删除多个
     */
    @Test
    void deleteBatchIds() {
        List<Integer> ids = new ArrayList<>();
        ids.add(25);
        ids.add(27);
        ids.add(28);
        int i = studentDao.deleteBatchIds(ids);
        System.out.println(i);
    }
}

    //默认主键的生成策略:雪花算法-唯一id值。
    //使用递增 如果想使用递增策略:必须保证表中的id递增,而且需要在@TableId(type=IdType.AUTO)
    @Test
    public void testInsert(){
        User user=new User("蔡雨新",16,"110@qq.com");
        int insert = userMapper.insert(user);
        System.out.println(insert);
    }

    @Test
    public void testUpdate(){
        User user=new User("乔文庆",18,"120@qq.com");
        user.setUid(7L);
        userMapper.updateById(user);
    }

}

3.4 使用mp完成条件查询

    @Test
    void find1(){
        QueryWrapper<Student> wrapper=new QueryWrapper<>(); //条件接口。QUeryWrapper 查询条件类  UpdateWrapper更新条件类  LambdaWrapper 使用lambda表达式
        wrapper.likeRight("sName","_a");
        wrapper.between("age",16,25);
        wrapper.orderByAsc("age");
        wrapper.select("sName","age");
        List<Student> students = studentDao.selectList(wrapper);
        System.out.println(students);
    }

    //分页需求--PageHelper---默认mp分页需要加分页拦截器
    /**
     * 单表分页查询
     */
    @Test
    public void testPage(){
        Page<Student> page=new Page<>(1,3); //current:当前第几页  size:每页显示条数
        studentDao.selectPage(page,null);//把查询分页的结果封装到page对象中
        System.out.println("当前页的记录"+page.getRecords());//获取当前页的记录
        System.out.println("获取总页数"+page.getPages());//获取当前页的记录
        System.out.println("获取总条数"+page.getTotal());//获取当前页的记录
    }

3.5 联表使用mp的分页对象

//联表操作--使用mp中自动的分页。
     @Autowired
    private StudentDao studentDao;
       /**
     * 条件分页查询
     */
    @Test
    public void testPage2(){
        Page<Student> page=new Page<>(1,5);
        QueryWrapper<Student> wrapper=new QueryWrapper<>();
        wrapper.gt("age",15);
        studentDao.findPage(page,wrapper);
        System.out.println("当前页的记录"+page.getRecords());
        System.out.println("获取总页数"+page.getPages());
        System.out.println("获取总条数"+page.getTotal());
    }
posted @   吃小孩子不吐皮  阅读(402)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示