- 编写业务代码,
Controller
package com.andan.web.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Name UserController
* @Author Administrator
* @Date 2022-08-09 10:29
* 用户控制类
*/
@RestController
@RequestMapping("/users")
public class UserController {
// Handler
@GetMapping("/{id}")
public String save(@PathVariable Integer id){
System.out.println("id = " + id);
return "save success";
}
}
2 概念
作用:为了快速开发 Spring
项目:简化配置 ,简化依赖引入。
Spring
的缺点:配置繁琐、依赖繁琐。
可以使用 SpringBoot
的自动配置和场景启动器(起步依赖)克服上述缺点。
SpringBoot
提供了一种 快速使用Spring的方式,基于 约定优于配置的思想。不需要关注配置,重点关注业务逻辑开发,缩短了项目周期。
SpringBoot
实现了0配置,1个依赖完成项目搭建。
SpringBoot通过两点实现了上述效果:
SpringBoot
还提供了一些嵌入式 web
服务器、安全、指标、健康监测、外部配置等。
注意: SpringBoot
只是提供了一种快速开发 Spring
项目的方式,而非对 Spring
功能上的增强。
又名场景启动器
起步依赖,就是依赖。可以理解为一个依赖组,一组依赖。
像导入普通依赖一样,导入即可。
starter
SpringBoot
中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.2
com.andan
spring_boot_io
0.0.1-SNAPSHOT
spring_boot_io
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
- 实际开发
- 使用任意坐标时,仅书写
GAV
中的G
和A
,V
由SpringBoot
提供 - 如发生坐标错误,再指定
version
(要小心版本冲突)
junit
junit
${junit.version}
javax.servlet
javax.servlet-api
${servlet-api.version}
org.springframework.boot
spring-boot-starter-parent
2.7.2
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
- Jetty比Tomcat更轻量级,可扩展性更强(相较于Tomcat),谷歌应用引擎(GAE)已经全面切换为Jetty
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.springframework.boot
spring-boot-starter-jetty
3. 配置
SpringBoot
支持 yml/yaml
和 properties
等格式的配置文件。
按照 约定,配置文件的文件名为 application.xxx
三种配置文件的优先级: properties > yml > yaml
,
多个配置(文件)中配置了相同的 key
,优先级高的生效;多个配置(文件)中配置了不同的 key
,同时生效。
示例如下:
properties
server.port=8080
xml
(SpringBoot
不支持)
8080
yml/yaml
-简洁,层次结构清晰,以数据为核心(推荐)
server:
port: 8080
- 大小写敏感
- 数据值前边必须有空格,作为分隔符
- 行头使用缩进表示层级关系
- 缩进时不允许使用
Tab
键,只允许使用空格(各个系统 Tab对应的 空格数目可能不同,导致层次混乱)。 - 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
#
表示单行注释,从这个字符一直到行尾,都会被解析器忽略。
这是一行注释
lesson: SpringBoot
server:
port: 80
- 数组:一组按次序排列的值,使用 ” – “表示数组每个元素
lesson: SpringBoot
server:
port: 80
enterprise:
name: itcast
age: 16
tel: 4006184000
subject:
- Java
- 前端
- 大数据
- 获取方式 首先在当前类中注入
Environment
对象,然后通过该对象的getProperty(“key”)
方法获取对应的值 -
关系图示
-
Yaml
文件
lesson: SpringBoot
server:
port: 80
enterprise:
name: itcast
age: 16
tel: 4006184000
subject:
- Java
- 前端
- 大数据
- 读取代码
@RestController
@RequestMapping("/books")
public class BookController {
//使用Environment封装全配置数据
@Autowired
private Environment environment;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println(environment.getProperty("lesson"));
System.out.println(environment.getProperty("server.port"));
System.out.println(environment.getProperty("enterprise.age"));
System.out.println(environment.getProperty("enterprise.subject[1]"));
return "hello , spring boot!";
}
}
-
映射图示
-
Yaml
文件
lesson: SpringBoot
server:
port: 80
enterprise:
name: itcast
age: 16
tel: 4006184000
subject:
- Java
- 前端
- 大数据
- 准备实体类
@Data
// 装配进Spring容器
@Component
// 添加注解,并指定访问前缀。前缀与配置文件中对应的一级属性名一致
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
// 实体类中成员变量名和二级属性名一致
private String name;
private Integer age;
private String tel;
private String[] subject;
}
- 读取代码
@RestController
@RequestMapping("/books")
public class BookController {
// 注入Enterprise,该对象就读取了配置文件,并封装配置文件属性到该对象中。
@Autowired
private Enterprise enterprise;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println(enterprise);
return "hello , spring boot!";
}
}
-
*自定义对象封装数据警告且无提示的问题
-
解决方案(配置依赖)
org.springframework.boot
spring-boot-configuration-processor
true
在实际开发中,项目的 开发环境、 测试环境、 生产环境的配置信息不一致。 SpringBoot
支持快速切换。
-
yaml
单文件多文件 -
演示配置文件(单位件多文档)
#设置启用的环境
spring:
profiles:
active: dev
#生产
spring:
profiles: pro
server:
port: 81
注意:
- 为文档命名的方式,在不同的
SpringBoot
版本中会稍有差异 低版本中只支持:spring.profiles:文档名
高版本还支持并推荐使用:spring.config.activate.on=profile:文档名
- 默认激活方式保持不变。
不要记,配置的时候,最终要的是结果;只要结果符合预期,怎么配置都可以。
想了解配置细节规则,可以查看官网文档。
4. 整合其他技术
最终目的是为了能够完成SSM的整合
SpringBoot
整合Spring
(不需要)SpringBoot
整合SpringMVC
(导入web
起步依赖)-
SpringBoot
整合MyBatis
(主要) -
配置一对依赖
SpringConfig
配置类JdbcConfig
配置类- 定义数据源(加载properties配置项:driver、url、username、password)
MyBatisConfig
配置类- 定义
SqlSessionFactoryBean
- 定义
MapperScannerConfigurer
相同的步骤:
-
编写
dao
层接口和SQL
语句 -
新建
SpringBoot
项目 - 配置起步依赖,及MySQL数据库驱动和Druid数据源
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.2
mysql
mysql-connector-java
5.1.48
com.alibaba
druid
1.1.16
org.projectlombok
lombok
- 配置数据源参数(主要是连接四要素)要注意:用户和密码要填自己的库
server:
port: 80
spring:
# 配置连接池参数
datasource:
# 连接四要素
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///ssm_spring
username: root
password: root
# 数据源类型,值为对应的数据源全类名
type: com.alibaba.druid.pool.DruidDataSource
- 编写Dao接口和SQL语句
package com.andan.dao;
import com.andan.entity.Book;
import org.apache.ibatis.annotations.Select;
/**
* Book持久层接口
*
* @Author Vsunks.v
* @Blog blog.sunxiaowei.net/996.mba
* @Description: Book持久层接口
*/
// @Mapper // 把该接口的代理对象装配进Spring(Boot)容器 和@MapperScan二选一即可
public interface BookDao {
@Select("select * from ssm_spring.t_book where id= #{id}")
public Book selectById(Integer id);
}
- 装配
Dao
进Spring(Boot)
容器(启动类上开启Mapper扫描)
package com.andan;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 整合其他技术的启动类
*
* @Author Vsunks.v
* @Blog blog.sunxiaowei.net/996.mba
* @Description: 整合其他技术的启动类
*/
@SpringBootApplication
@MapperScan("com.andan.dao") // 扫描指定位置的mapper接口,一劳永逸
public class IntegrateApplication {
public static void main(String[] args) {
SpringApplication.run(IntegrateApplication.class,args);
}
}
- 新建SpringBoot模块
- 导入起步依赖(脚手架方式创建的模块会自动添加)
org.springframework.boot
spring-boot-starter-test
test
- 在主包下编写测试入口类(脚手架方式创建的模块会自动添加)
package com.andan;
import com.andan.dao.BookDao;
import com.andan.entity.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* BookDao测试类
*
* @Author Vsunks.v
* @Date 2022/8/5 16:48
* @Blog blog.sunxiaowei.net/996.mba
* @Description: BookDao测试类
*/
// @RunWith(SpringRunner.class) // 使用junit4 需要配置该运行器
// 标注该类是一个SpringBoot中的测试类,该类要放在主包下
// 如果不在主包下,需要通过其classes属性指定启动类的字节码对象
@SpringBootTest
class BookDaoTest {
// 测谁就注入谁
@Autowired
BookDao bookDao;
@Test
public void testSelectById() {
Book book = bookDao.selectById(14);
System.out.println("book = " + book);
}
}
不需要整合,按照 Spring
的要求编码即可。
- 新建service接口和实现类
- 装配实现类进Spring容器,并注入Dao
-
定义方法调用Dao中方法
-
接口
BookService
package com.andan.service;
import com.andan.entity.Book;
/**
* @Name BookService
* @Author Administrator
* @Date 2022-08-09 11:02
* Book业务层接口
*/
public interface BookService {
Book getById(Integer id);
}
- 实现类
BookServiceImpl
package com.andan.service.impl;
import com.andan.dao.BookDao;
import com.andan.entity.Book;
import com.andan.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @Name BookServiceImpl
* @Author Administrator
* @Date 2022-08-09 11:03
*/
@Service
public class BookServiceImpl implements BookService {
@Autowired
BookDao bookDao;
@Override
public Book getById(Integer id) {
return bookDao.selectById(id);
}
}
配置起步依赖即可。
- 配置Web异步依赖
-
编写Controller
-
BookController
package com.andan.web.controller;
import com.andan.entity.Book;
import com.andan.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Name BookController
* @Author Administrator
* @Date 2022-08-09 11:01
*/
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
BookService bookService;
// 根据id查询
@GetMapping("/{id}")
public Book getById(@PathVariable Integer id) {
return bookService.getById(id);
}
}
静态页面,放在 resources/static
文件夹下。
也可以放在其他位置,使用 SpringMVC
中的放行静态资源;SpringBoot支持读取配置类,方式和之前一致。
Original: https://www.cnblogs.com/andanlife/p/16559206.html
Author: 黯淡钦凉
Title: SpringBoot快速入门
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/643747/
转载文章受原作者版权保护。转载请注明原作者出处!