SpringBoot进阶 (7)

向导开发,自动导入依赖

<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency>

搞定数据源Druid,省略


注解版 @Mapper public interface DepartmentMapper { @Select("SELECT * FROM department WHERE id=#{id}") public Department getDeptById(Integer id); @Delete("DELETE FROM department WHERE id=#{id}") public int deleteById(Integer id); @Options(useGeneratedKeys = true,keyProperty = "id") @Insert("INSERT INTO department (`departmentName`) VALUES (#{departmentName})") public int insertDept(Department department); public int updateDept(Department department); } @RestController public class DeptController { @Autowired DepartmentMapper departmentMapper; @GetMapping("/dept/{id}") public Department getDeptment(@PathVariable("id") Integer id){ return departmentMapper.getDeptById(id); } @GetMapping("/dept") public Department insertDept(Department department){ departmentMapper.insertDept(department); return department; } }

主程序入口

@MapperScan(value = "com.howl.springboot.mapper")
重点配置文件(要在主配置文件中配置) mybatis: config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*





19. 最后一点SpringBoot启动原理

创建SpringApplication对象

保存主配置类 判断是否web应用 从类路径下找applicationContextInitializer然后保存起来 从类路径下找applicationContextListener然后保存起来 从多个配置类中找到main方法的主配置类

运行run方法

获取SpringapplicationRunListeners:从类路径下META-INF下spring.factories 回调所有的获取获取SpringapplicationRunListeners.starting()方法 封装命令行参数 准备环境:IOC环境,eg:profile 创建环境后回调SpringapplicationRunListeners.environmentprepared():表示环境准备完成 创建IOC容器 准备上下文: environment保存到IOC中 而且要执行applyInitalizers()方法(上面创建应用的时候就拿到了所有的Initializer),回调里面全部方法 回调所有的Listeners的contextPrepared(); 回调所有的Listeners的contextLoaded()方法 刷新容器:IOC容器初始化,加载组件(配置类、@bean),还有嵌入式容器 从IOC获取所有的ApplicationRunner和CommandRunner


参考来自

https://www.bilibili.com/video/BV1bb411A7bD

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zydsdg.html