SpringBoot
对ssm整合,是spring全家桶的一员,用来快速搭建基于spring的java应用,是一个脚手架框架,内部集成了spring的所有产品,以及主流的第三方框架,只需要开箱即用,不需要进行配置
-
创建
-
创建yml
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 1234 url: jdbc:mysql://localhost:3306/db2 mybatis: type-aliases-package: com.entity configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
创建,实体类,mapper(mapper.xml), service, controller。
servicer中注入mapper,controller中注入service
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
Accountmapper.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.guo.mapper.AccountMapper"> <select id="list" resultType="Account"> select * from db2.account </select> </mapper>
springBoot是一个快速搭建基于spring框架的java应用的脚手架,springBoot本身没有业务,就是快速搭建工程,方便开发,对所有领域主流的技术栈进行集成,自动封装,开发者使用的话就不需要手动进行配置,开箱即用。
自动配置
SpringBoot 核心注解:SpringBootApplication
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited 元注解
@SpringBootConfiguration 其实就是@configuration注解,用的比较多。实现自动配置。将一个java类标注成为一个配置类,替代xml文件。
@Configuration public class Myconfigratiion { @Bean(value="id的名字")默认为方法名字。 public Account account() { return new Account("小李",3); } } 等同于 <beans> <bean id="account" class="com.entity.Account"> <property name="id", value="1"/> <property name="name" value="小李"/> </bean> </beans>
@ConfigurationProperties
@Data @ConfigurationProperties(prefix = "people") public class People { private Integer id; private String name; private String tel; }; people: id: 11 name: 张三 tel: 13678787878
@ComponentScan
自动扫描加载bean,通过设置basepackage指定需要扫描包,该目录下的类及其及目录下的类都会被扫描,
@ComponetScan(basePackage="com.guo")
如果没有显示设置basePackage的值,默认值就是添加了该注解的类所在的包,
启动类一定要放在父包中,所有的业务类一定要被启动类辐射到,否则无法注入ioc
@EnableAutoConfiguration
包含:
@AutoConfigurationPackage:开发者自定义的组件 controller、service、mapper、 @Import({AutoConfigurationImportSelector.class}):myconfiguration框架自带组件 反的是spring.factories
@AutoConfigurationPackage据包进行自动装配,底层是@Import({Registrar.class}),
Spring Boot 自动装载的 bean 包括两部分内容:开发者自定义的组件 controller、service、mapper、myconfiguration框架自带组件