目录
9.5 在resource下创建一个目录MATE-INF 里面创建一个文件名spring.factories
11.6 在springboot启动类上加上--dao接口得扫描
11.7 测试---必须为springboot得单元测试【不能使用之前junit得测试】
14.1 我们如何使用swagger2? springboot的版本不能那么高。2.2~2.5之间
14.2 创建一个swagger配置类---所有的功能都集成在Docket类
一、什么是SpringBoot
二、SpringBoot的特点
三、springboot快速入门
四、介绍配置文件的种类
属性文件properties:
# 设置tomcat的端口号
server.port=8888
# 上下文访问路径
server.servlet.context-path=/aaa
yaml文件:
# 设置端口号
server:
port: 8887
# 设置上下文路径
servlet:
context-path: /bbb
五、开发环境配置文件的切换
在application.properties配置文件中激活相应的配置
# 引入相关的环境配置--激活开发环境
spring.profiles.active=test
# 写一些不同环境下相同的配置
六、读取springboot配置文件中的内容
@Autowired
private AliPay aliPay;
@RequestMapping("/index")
public AliPay index(){
return aliPay;
}
测试:
七、Springboot注册web三大组件
public class MyServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~");
}
}
package com.ykq.config;
import com.ykq.servlet.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.http.HttpServlet;
@Configuration //====>表示该类为配置类 等价于我们之前的xml文件
public class MyConfig {
@Bean //xml文件中<bean class=""> 把方法返回的对象交于spring容器来管理
public ServletRegistrationBean<HttpServlet> registrationBean(){
ServletRegistrationBean<HttpServlet> servletServletRegistrationBean=new ServletRegistrationBean<>();
servletServletRegistrationBean.setName("my");
servletServletRegistrationBean.addUrlMappings("/my");
servletServletRegistrationBean.setServlet(new MyServlet());
return servletServletRegistrationBean;
}
}
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("过滤器编码");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
}
//过滤的注册
@Bean
public FilterRegistrationBean<Filter> filterRegistrationBean(){
FilterRegistrationBean<Filter> filterRegistrationBean=new FilterRegistrationBean<>();
filterRegistrationBean.setName("encondigFilter");
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.setFilter(new MyFilter());
return filterRegistrationBean;
}
八、SpringBoot的底层原理
8.1 包扫描的原理
8.2 springboot自动装配原理
思考: 根据源码 我们看到上来加载了所有的自动装配了,而所有的自动装配类默认127个。这127个从哪来的。
九、自定义starter依赖
9.1 创建一个springboot工程并引入相关得依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
9.2 创建一个属性类
/**
* Created by Intellij IDEA
*
* @author 王俊凯
* @Date: 2022/11/17 14:44
* @Version 1.0
*/
package com.wjk;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.wjk")
public class WjkProperties {
private String name;
private String address;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
9.3 定义一个业务类
/**
* Created by Intellij IDEA
*
* @author 王俊凯
* @Date: 2022/11/17 14:46
* @Version 1.0
*/
package com.wjk;
public class WjkService {
private WjkProperties wjkProperties;
public WjkService() {
}
public WjkService(WjkProperties wjkProperties) {
this.wjkProperties = wjkProperties;
}
//业务代码
public void hello(){
System.out.println("姓名:"+wjkProperties.getName()+"地址:"+wjkProperties.getAddress()+"年龄:"+wjkProperties.getAge());
}
}
9.4 定义一个自动装配类
/**
* Created by Intellij IDEA
*
* @author 王俊凯
* @Date: 2022/11/17 14:49
* @Version 1.0
*/
package com.wjk;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //该类为配置类
@EnableConfigurationProperties(WjkProperties.class) //开启属性配置
@ConditionalOnClass(WjkService.class) //只要wjkService被引后入,当中的这个类才会被创建
@ConditionalOnProperty(prefix = "spring.wjk",value = "enabled",matchIfMissing = true) //如果引用者没有给定相关的属性值,则采用默认值
public class WjkAutoConfiguration {
@Autowired
private WjkProperties wjkProperties;
@Bean
@ConditionalOnMissingBean
public WjkService wjkService(){
return new WjkService(wjkProperties);
}
}
9.5 在resource下创建一个目录MATE-INF 里面创建一个文件名spring.factories
9.6 打包--maven 配置了本地仓库
打包--maven 配置了本地仓库https://blog.csdn.net/suo_jia_hao/article/details/120759708
十、springboot整合数据源
10.1 引入数据源得启动依赖
<!--引入数据源依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
10.2 指定数据源得信息
# 指定数据源信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=wjk351066
10.3 测试
@SpringBootTest
@MapperScan(basePackages = "com.wjk.dao")
class Java111501ApplicationTests {
/**
* 该依赖自动装配类 帮你建好DataSource对象 交与容器管理
*/
@Autowired
private DataSource dataSource;
@Test
void contextLoads() {
System.out.println(dataSource);
}
}
10.4 springboot整合第三方数据源
<!--引入druid得数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
# 指定数据源信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=wjk351066
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=10
10.5 测试
@SpringBootTest
class Qy158Springboot01ApplicationTests {
//数据源对象---自己有没有创建该类对象---因为你引入spring-boot-starter-jdbc启动依赖。
//该依赖得自动装配类 帮你创建好DataSource对象 交于容器管理
@Autowired
private DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource);
}
}
十一、springBoot整合Mybatis
11.1 引依赖
<!--mybatis得整合启动依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
11.2 修改配置文件
# 指定mybatis得配置内容
mybatis.mapper-locations=classpath:/mapper/*.xml
# 指定mybatis
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
11.3 写一个实体类(省略)
11.4 创建Dao接口
package com.wjk.dao;
import com.wjk.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
public interface StudentDao {
public List<Student> selectAll();
int insert(Student student);
int delete(Integer id);
int update(Student student);
}
11.5 映射文件
<?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.wjk.dao.StudentDao">
<insert id="insert">
insert into t_student values (null ,#{name},#{age},#{address})
</insert>
<update id="update">
update t_student set name =#{name},age=#{age},address=#{address} where id=#{id}
</update>
<delete id="delete">
delete from t_student where id=#{id}
</delete>
<select id="selectAll" resultType="com.wjk.entity.Student">
select * from t_student
</select>
</mapper>
11.6 在springboot启动类上加上--dao接口得扫描
11.7 测试---必须为springboot得单元测试【不能使用之前junit得测试】
@Resource
private StudentDao studentDao;
@Test
void testSelectAll(){
PageHelper.startPage(1,3);
List<Student> list = studentDao.selectAll();
System.out.println(list);
}
十二、springboot整合pageHelper
<!--pagehelper分页插件得依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
@Resource
private StudentDao studentDao;
@Test
void testSelectAll(){
PageHelper.startPage(1,3);
List<Student> list = studentDao.selectAll();
PageInfo<Student> pageInfo = new PageInfo<>(list);
System.out.println("总条数:"+pageInfo.getTotal());
System.out.println("当前页的条数:"+pageInfo.getList());
}
十三、springboot整合定时器
13.1 引入定时器的依赖
<!--引入定时器的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
13.2 定义定时器的业务类
package com.wjk.quartz;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component //交与容器管理
public class MyQuartz {
/**
* 业务代码 Scheduled规定什么时候执行业务代码
*/
@Scheduled(cron = "0/5 * * * * ? ")
public void test(){
System.out.println("********************");
}
}
13.3 开启定时器的注解
十四、 springboot整合swagger2
14.1 我们如何使用swagger2? springboot的版本不能那么高。2.2~2.5之间
<!--引入swagger2的依赖-->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
<!--swagger图形化界面-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
14.2 创建一个swagger配置类---所有的功能都集成在Docket类
@Configuration //标记为配置类
public class SwaggerConfig {
@Bean
public Docket docket(){
Docket docket = new Docket(DocumentationType.SWAGGER_2)
return docket;
}
}
14.3 开启swagger2注解
package com.wjk;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableScheduling //开启定时器注解
@EnableSwagger2 //开启swagger2的注解
public class SpringBoot1118Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot1118Application.class, args);
}
}
14.4 定义相关的接口
@RestController
public class HelloController {
@GetMapping("hello01")
public String hello01(){
return "hello01";
}
}
14.5 查看swagger2的界面
14.6 最终的配置类完整格式
/**
* Created by Intellij IDEA
*
* @author 王俊凯
* @Date: 2022/11/18 10:06
* @Version 1.0
*/
package com.wjk.config;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
@Configuration //标记为配置类
public class SwaggerConfig {
@Bean
public Docket docket(){
Docket docket = new Docket(DocumentationType.SWAGGER_2)
//组名
.groupName("wjk")
//api信息
.apiInfo(getInfo())
//选择哪些生成api接口--根据请求路径选择 (2)根据包名选择
.select()
//根据请求路径选择
.paths(Predicates.and(PathSelectors.regex("/api/.*")))
//根据报名选择
.apis(RequestHandlerSelectors.basePackage("com.wjk.controller"))
.build()
;
return docket;
}
private ApiInfo getInfo(){
Contact DEFAULT_CONTACT = new Contact("王俊凯", "http://www.jd.com", "1315272325@qq.com");
ApiInfo info = new ApiInfo("在线预约挂号系统", "在线挂号系统", "9.9", "http://www.baidu.com",
DEFAULT_CONTACT, "上海富有银行有限公司", "http://www.taobao.com", new ArrayList<VendorExtension>());
return info;
}
}
十五、thymeleaf模板引擎
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
@RestController
public class PageController {
@RequestMapping("/index")
public String index(){
return "index";
}
}