SpringBoot概述概述SpringBoot时Spring提供的一个子项目用于快速构建Spring应用程序SpringBoot特性起步依赖、自动配置、其他特性起步依赖本质上就是一个Maven坐标整合完成了一个功能需要的所有坐标自动配置遵循约定大约配置的原则在boot程序启动后一些bean对象会自动注入到ioc容器不需要手动生命简化开发其他特性内嵌的Tomcat、Jettly无需部署WAR文件外部化配置不需要XML配置properties/yml配置文件properties配置文件1.application.propertiesserver.port9090 server.servlet.context-path/start2.application.yml/ application.yamlserver: port: 9191 servlet: context-path: /start2yaml配置文件 书写与获取①第三方技术配置信息②自定义配置信息email: user: 593140521qq.com code: jfejwezhcrzcbbbb host: smtp.qq.com auth: true书写· 值前面必须有空格作为分隔符· 使用空格作为缩进表示层级关系相同的层级左侧对齐获取· Value“${键名})· ConfigurationProperties(prefix 前缀)整合MyBatiscreate database if not exists mybatis; use mybatis; create table user( id int unsigned primary key auto_increment comment ID, name varchar(100) comment 姓名, age tinyint unsigned comment 年龄, gender tinyint unsigned comment 性别, 1:男, 2:女, phone varchar(11) comment 手机号 ) comment 用户表; insert into user(id, name, age, gender, phone) VALUES (null,白眉鹰王,55,1,18800000000); insert into user(id, name, age, gender, phone) VALUES (null,金毛狮王,45,1,18800000001); insert into user(id, name, age, gender, phone) VALUES (null,青翼蝠王,38,1,18800000002); insert into user(id, name, age, gender, phone) VALUES (null,紫衫龙王,42,2,18800000003); insert into user(id, name, age, gender, phone) VALUES (null,光明左使,37,1,18800000004); insert into user(id, name, age, gender, phone) VALUES (null,光明右使,48,1,18800000005);package com.itheima.mybatisquickstart.pojo; public class User { private Integer id; private String name; private Short age; private Short gender; private String phone; public User() { } public User(Integer id, String name, Short age, Short gender, String phone) { this.id id; this.name name; this.age age; this.gender gender; this.phone phone; } public Integer getId() { return id; } public void setId(Integer id) { this.id id; } public String getName() { return name; } public void setName(String name) { this.name name; } public Short getAge() { return age; } public void setAge(Short age) { this.age age; } public Short getGender() { return gender; } public void setGender(Short gender) { this.gender gender; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone phone; } Override public String toString() { return User{ id id , name name \ , age age , gender gender , phone phone \ }; } }Bean管理Bean扫描·标签 context:component-scan base-packagecom.itheima/·注解 ComponentScan(basePackages com.itheima)Bean注册*如果要注册的bean对象来自于第三方不是自定义的无法用Conponent 及衍生注解生命bean的·Bean如果要注册第三方bean建议在配置类中集中注册·Import导入 配置类导入 ImportSelector 接口实现类EnableXxxx注解封装Import注解注册条件SpringBoot提供了设置注册生效条件的注解Conditional自动配置管理自动配置原理①在主启动类上添加了SpringBootApplication注解这个注解组合了EnableAutoCongfiguration注解②EnableAutoCongfiguration注解又组合了Import注解导入了AutoCongfigurationImportSelector类③实现selectImprots方法这个方法经过层层调用最终会读取META-INF目录下的后缀名为imorts的文件自定义starter需求自定义mybatis的starter步骤·创建 dmybatis-spring-boot-autoconfigure模块提供自动配置功能并自定义配置文件META-INF/spring/xxx.imports·创建dmybatis-spring-boot-starter模块在starter中引入自动配置模块1.创建autoconfigure模块编写自动配置类Configuration public class CommonConfig { // 示例根据配置文件条件注册 Bean Bean ConditionalOnProperty(prefix country, name {name, system}) public Country country(Value(${country.name}) String name, Value(${country.system}) String system) { return new Country(name, system); } Bean public Province province() { return new Province(); } }2.创建自动配置入口类AutoConfiguration Import(CommonConfig.class) public class CommonAutoConfig { }3.配置自动配置加载文件cn.itcast.config.CommonAutoConfig4.创建starter模块引入autoconfigure依赖dependency groupIdcn.itcast/groupId artifactIddmybatis-spring-boot-autoconfigure/artifactId version1.0.0/version /dependency