1. springboot简介2. 本地maven方式创建springboot项目3. springboot场景启动器原理4. 联网创建springboot项目5. springboot启动+自动配置原理
5.1 springboot场景启动器5.2 springboot项目依赖的jar包的版本管理5.3 项目自动配置如何实现的 6. springboot-配置文件
6.1 application.properties6.2 application-后缀.yml 7. springboot-ssm整合
7.1 创建springboot项目,引入依赖7.2 使用mbg逆向工程逆向生成javabean+mapper文件7.3 完成Controller处理用户的请求7.4 准备mybatis的全局配置文件7.5 创建application.yml文件编写配置7.6 告诉springboot项目的主程序扫描mapper接口包并创建组件对象 8. springboot-ssm 整合druid
1. springboot项目默认自带的数据库连接池2. druid连接池
2.1 引入Druid依赖2.2 指定springboot创建连接池使用Druid2.3 druid的后台监控功能 9.springboot-单元测试的使用10.springboot-整合事务
1. springboot简介开发Maven项目[maven可以帮助我们进行项目的构建管理、快速的依赖第三方的jar包、对项目进行拆分开发]
Maven项目开发时,web类型的项目比较多,基本上大部分的mavenweb项目都需要使用框架,主流框架都是ssm,基本上所有的项目依赖的jar包都差不多。
Springboot项目可以基于maven项目进行依赖管理:
springboot创建了n多个 maven类型的项目,每个项目中都管理了一组依赖,范围时compile的,每个组都代表一个开发的场景,很多依赖都由springboot提供的默认配置。
我们在开发项目时,只需要引入springboot写好的maven项目的依赖,就可以拥有该场景对应的所有的依赖的jar包了。(依赖传递性)
springboot是框架之上的框架,就是通过maven的pom文件对依赖进行了分组管理
约定大于配置
springboot核心功能:1)起步依赖 2)自动配置
项目需求:浏览器访问当前项目/hello,返回一个hello字符串
1.先创建java maven项目
2.修改项目的pom文件
2.1 需要继承springboot项目的父工程
org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE
2.2 依赖当前场景需要的springboot的场景启动器
org.springframework.boot spring-boot-starter-web
2.3 手动创建部署当前项目到web场景启动器携带的tomcat中并运行tomcat的主程序
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication //表示当前类为springboot的主程序
public class HelloWordofflineApplication {
//程序入口:Tomact的程序入口也是main方法。Java程序基本都是
public static void main(String[] args) {
SpringApplication.run(HelloWordofflineApplication.class,args);
}
}
2.4 在主程序所在的包或子包下创建处理用户请求的Controller
package com.atgui.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//@Controller
@RestController//相当于当前Controller上标注了@Controller注解+每个方法上标注了@ResponseBody
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello-----------";
}
}
3. springboot场景启动器原理
场景启动器:一个场景启动器对应一个maven工程。该maven工程将当前 场景需要的依赖都已经在自己的pom中依赖好了
org.springframework.boot spring-boot-starter-web
4. 联网创建springboot项目org.springframework.boot spring-boot-starter 2.2.2.RELEASE compile org.springframework.boot spring-boot-starter-json 2.2.2.RELEASE compile org.springframework.boot spring-boot-starter-tomcat 2.2.2.RELEASE compile org.springframework.boot spring-boot-starter-validation 2.2.2.RELEASE compile tomcat-embed-el org.apache.tomcat.embed org.springframework spring-web 5.2.2.RELEASE compile org.springframework spring-webmvc 5.2.2.RELEASE compile
1、创建springboot项目:https://start.aliyun.com
2、选择要使用的依赖:spring-web
注意:
联网创建的pom文件中的***相当于本地的
项目打包运行:
springboot提供了打包插件,使用时可以将springboot项目内置的tomcat一起打包
打成的jar包可以通过:java -jar jar包名称 直接运行访问
将连网的pom文件中涉及打包的部分复制到本地创建的项目的pom文件中
org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin 2.3.7.RELEASE repackage repackage
主要是下面这部分:
5. springboot启动+自动配置原理 5.1 springboot场景启动器org.springframework.boot spring-boot-maven-plugin 2.3.0.RELEASE repackage repackage
对maven依赖的封装[创建一个maven项目将一组依赖添加到pom文件中]
5.2 springboot项目依赖的jar包的版本管理springboot项目创建时会继承spring-boot-starter-parent工程
org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE
spring-boot-starter-parent项目又继承了:
org.springframework.boot spring-boot-dependencies 2.2.2.RELEASE ../../spring-boot-dependencies
spring-boot-dependencies项目中:
管理了springboot项目需要用到jar包的所有的版本
还通过dependencymanagement管理了多个依赖
如果自己的项目继承了spring-boot-starter-parent,就可以直接声明spring-boot-dependencies中依赖管理的jar包进行使用,无需执行版本
springboot项目初始化启动时,所有的默认配置和组件对象的扫描创建注入到容器中都是由主程序的注解来完成的
@SpringBootApplication
@Target(ElementType.TYPE) //当前注解可以标注的地方
@Retention(RetentionPolicy.RUNTIME) //当前注解起作用的时机
@documented //文档
@Inherited //当前注解是否可以被继承
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
}
@SpringBootConfiguration //作用和Configuration一样,也是表示当前类是一个配置组件类
@ComponentScan //组件扫描 ,默认配置可以在主程序所在包及它的子包下扫描所有的组件类并创建对象注入到容器中
@EnableAutoConfiguration //启用自动配置的注解
@AutoConfigurationPackage
@import(AutoConfigurationimportSelector.class) //AutoConfigurationimportSelector 自动配置的导入选择器
public @interface EnableAutoConfiguration {
}
AutoConfigurationimportSelector
@Override
public String[] selectimports(Annotationmetadata annotationmetadata) {
if (!isEnabled(annotationmetadata)) {
return NO_importS;
}
AutoConfigurationmetadata autoConfigurationmetadata = AutoConfigurationmetadataLoader
.loadmetadata(this.beanClassLoader);
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(autoConfigurationmetadata,
annotationmetadata);
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
protected AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationmetadata autoConfigurationmetadata,
Annotationmetadata annotationmetadata) {
if (!isEnabled(annotationmetadata)) {
return EMPTY_ENTRY;
}
AnnotationAttributes attributes = getAttributes(annotationmetadata);
List configurations = getCandidateConfigurations(annotationmetadata, attributes);
configurations = removeDuplicates(configurations);
Set exclusions = getExclusions(annotationmetadata, attributes);
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = filter(configurations, autoConfigurationmetadata);
fireAutoConfigurationimportEvents(configurations, exclusions);
return new AutoConfigurationEntry(configurations, exclusions);
}
getCandidateConfigurations:
此方法中会在项目的类路径下扫描meta-INF目录下的spring.properties文件
spring.properties文件中描述了当前spring场景启动器需要使用的配置类全类名[配置对象可以自动加载springboot的默认配置还可以自动读取application.properties中的配置参数使用],配置类也通过@Configuration注解标注,会被容器创建对象,但是一般配置类会标注约束[在特定情况下才会被创建对象]。有些配置类中也会对配置类对应的场景需要使用的模板类对象进行初始化。
配置对象会根据依赖的场景启动器挑选需要初始化的配置对象进行使用
springboot-autoconfigure 包:自动配置包
6. springboot-配置文件springboot项目创建时默认会在resources下提供application.properties文件,可以编写自定义配置
有些情况会存在默认配置[上边的自动配置]
6.1 application.properties语法和我们以前的properties文件语法一样
server.port=10001 server.servlet.context-path=/app6.2 application-后缀.yml
yml文件可以对应每个实际环境编写一个,然后在全局配置文件application.properties中选择激活哪一个环境的yml配置
yml文件和properties文件中如果存在相同的配置,优先使用properties文件中的
yml文件的语法:
属性和他的子属性需要换行,子属性使用空格(推荐两个空格)表示层级关系,属性和属性以及 属性和值之间都需要使用:分割 , 属性和值之间还需要有一个空格
yml文件会对配置进行自动归组
项目启动时默认会读取application名称的yml和application配置文件。
server:
port: 9002
servlet:
context-path: /myappdebug
session:
timeout: 30m
spring:
application:
name: app
如果希望对应环境的配置文件起作用,可以在application.properties选择要激活的yml文件
需求:查询数据库t_admin表中的所有的数据的json显示到页面中
7.1 创建springboot项目,引入依赖 处理浏览器请求,引入springweb
操作数据库,引入jdbcAPI依赖
要使用mysql数据库,引入mysql的驱动
使用mybatis的依赖操作数据库+中间包
2.1 在项目中引入逆向工程的jar包+maven插件
org.mybatis.generator mybatis-generator-core 1.3.7
org.mybatis.generator mybatis-generator-maven-plugin 1.3.7 mysql mysql-connector-java 5.1.42
2.2 准备逆向工程的配置文件到项目的resources目录下
2.3 执行逆向工程
@RestController
public class AdminController {
@Autowired
AdminService adminService;
// @GetMapping 相当于@RequestMapping( method=RequestMethod.GET)
// @PostMapping
// @PutMapping
// @DeleteMapping
@GetMapping("/admins")
public List getAllAdmins(){
return adminService.getAllAdmins();
}
}
service:
@Service
public class AdminServiceImpl implements AdminService {
@Autowired
TAdminMapper adminMapper;
@Override
public List getAllAdmins() {
return adminMapper.selectByExample(null);
}
}
7.4 准备mybatis的全局配置文件
在reousrce下创建 mybatis-config.xml
7.5 创建application.yml文件编写配置
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/scw?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
mybatis:
# mybatis 所有mapper映射文件的路径
mapper-locations: classpath:mybatis/mapper/*.xml
# mybatis全局配置文件的路径
config-location: classpath:mybatis-config.xml
7.6 告诉springboot项目的主程序扫描mapper接口包并创建组件对象
@SpringBootApplication
//指定mapper接口(组件)所在的包 :必须写到mapper接口所在的包
@MapperScan(basePackages = "com.atguigu.ssm.mapper" )
public class SpringbootSsmApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootSsmApplication.class, args);
}
}
8. springboot-ssm 整合druid
1. springboot项目默认自带的数据库连接池
数据库连接池的类名:com.zaxxer.hikari.HikariDataSource
HikariDataSource:性能非常高、但是没有后台监控系统
2. druid连接池 性能适中,自带后台管理系统可以监控sql的执行情况
以后开发中优先使用druid
2.1 引入Druid依赖2.2 指定springboot创建连接池使用Druidcom.alibaba druid 1.1.23
方式1: 在application.yml中指定:
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/scw?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
# 指定创建druid连接池
type: com.alibaba.druid.pool.DruidDataSource
方式2: 通过@Bean的方式初始化druid数据库连接池对象
//在组件类中通过@Bean标注的方法的返回值也会被注入到容器中
@Bean
@ConfigurationProperties(prefix = "spring.datasource") //此注解可以根据指定的前缀加载配置文件中的属性,前缀后的属性名如果和返回的对象的属性名一样则自动将属性值设置给该属性
public DataSource getDruidDataSource(){
DruidDataSource dataSource = new DruidDataSource();
//设置数据库连接参数
return dataSource;
}
注意这里import javax.sql.DataSource; 不要错了。
在主程序中配置:
@Bean
@ConfigurationProperties(prefix = "spring.datasource") //此注解可以根据指定的前缀加载配置文件中的属性,前缀后的属性名如果和返回的对象的属性名一样则自动将属性值设置给该属性
public DataSource getDruidDataSource(){
DruidDataSource dataSource = new DruidDataSource();
try {
//启用druid连接池的监控的filter
dataSource.setFilters("stat, wall");
} catch (SQLException e) {
e.printStackTrace();
}
//设置数据库连接参数
return dataSource;
}
//配置Druid的监控
//1、配置一个管理后台的Servlet
@Bean
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map initParams = new HashMap<>();
initParams.put("loginUsername", "admin");
initParams.put("loginPassword", "123456");
initParams.put("allow", "");// 默认就是允许所有访问
initParams.put("deny", "192.168.15.21");
bean.setInitParameters(initParams);
return bean;
}
//2、配置一个web监控的filter
@Bean
public FilterRegistrationBean webStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map initParams = new HashMap<>();
initParams.put("exclusions", "*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
在浏览器中访问:http://localhost:端口号/druid/xx 可以访问druid的后台监控系统
9.springboot-单元测试的使用如果springboot项目中引入的是junit4,单元测试类:
@SpringBootTest //可以使用spring容器
@RunWith(SpringRunner.class)
public class SpringbootSsmApplicationTests {
@Autowired
AdminService adminService;
@Test
public void contextLoads() {
System.out.println("adminService.getAllAdmins() = " + adminService.getAllAdmins());
}
}
如果springboot项目引入的是junit5依赖,单元测试类:
@SpringBootTest
class SpringbootSsmApplicationTests {
@Autowired
AdminService adminService;
@Test
void contextLoads() {
System.out.println("adminService.getAllAdmins() = " + adminService.getAllAdmins());
}
}
10.springboot-整合事务
springboot就是基于maven进行开发的项目,简化maven项目的开发
在ssm整合的项目中只要配置了事务管理器,启用了声明式的事务,在业务层使用@Transactional注解就可以启用事务
启用事务
1、在主程序类名上添加EnableTransactionManagement 启用声明式事务管理功能
2、在业务层类名上添加@Transactional //使用事务管理



