基于SpringBoot框架的程序开发步骤
熟练使用SpringBoot配置信息修改服务器配置
基于SpringBoot的完成SSM整合项目开发
SpringMVC的HelloWord程序大家还记得吗?
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
原生开发SpringMVC程序过程
环境准备
@RestController
public class Controller01 {
@RequestMapping("/sayHi")
public String sayHi(){
System.out.println("hi...");
return "hi ... springboot...";
}
}
④:运行自动生成的Application类
访问页面
重新启动
最简SpringBoot程序所包含的基础文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.1
com.itheima
demo1_helloworld
0.0.1-SNAPSHOT
demo1_helloworld
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
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*/*
@SpringBootApplication:
1. 表示这个类是一个springboot应用程序的入口类。
2. 要想让程序启动,只需要在main方法里面写上这样的一句话:
SpringApplication.run(当前类的字节码对象, args);
3. 拓展:
3.1 springboot项目启动的时候,默认会扫描启动类所在的位置,以及它后续的所有子包。
3.2 查找到类里面打的注解 @Controller , @Service , @RequestMapping.
3.3 springApplication.run 传递进去当前类的字节码对象,也是可以确定当前
这个启动器它的包是哪个!
*/
@SpringBootApplication
public class Demo1HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(Demo1HelloworldApplication.class, args);
}
}
Spring程序与SpringBoot程序对比
注意事项:
基于idea开发SpringBoot程序需要确保联网且能够加载到程序框架结构
保存到桌面
解压完之后 用idea打开他
因为用到了Mysql 所以得配置数据库
已经启动
① 对SpringBoot项目打包(执行Maven构建指令package)
② 执行启动指令
java -jar ava -jar demo1_helloworld-0.0.1-SNAPSHOT.jar # 项目的名称根据实际情况修改
例子
注意事项:
jar支持命令行启动需要依赖maven插件支持,请确认打包时是否具有SpringBoot对应的maven插件。
org.springframework.boot
spring-boot-maven-plugin
如果没有这个依赖maven插件 打包就只有4k左右
这个时候跟本运行不了
学习了SpringBoot入门案例之后,感觉对比SpringMVC哪一个更加方便简洁?
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
Spring程序缺点
配置繁琐
依赖设置繁琐
SpringBoot程序优点
自动配置
起步依赖(简化依赖配置)
辅助功能(内置服务器,……)
28行到225行
226行 到2737行
starter
SpringBoot中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的
可以认为这个起步依赖相当于一个开关 我们主要用了这个东西 就相当用了他的全套功能
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
parent
所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的
spring-boot-starter-parent(2.5.0)与 spring-boot-starter-parent(2.4.6)共计57处坐标版本不同
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.1
com.itheima
demo1_helloworld
0.0.1-SNAPSHOT
demo1_helloworld
Demo project for Spring Boot
4.0.0
org.springframework.boot
spring-boot-dependencies
2.5.0
spring-boot-starter-parent
pom
...
实际开发
使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供
如发生坐标错误,再指定version(要小心版本冲突)
如我们要用到Mysql
mysql
mysql-connector-java
当然也不是什么都有管理
比如druid就不被管理
com.alibaba
druid
1.2.8
如果不写版本号的话 就会出现unknown 这个时候 我们就知道在springboot的父亲的父亲里面并没有管理这个依赖 这个时候 我们就能自己添加版本号
自己添加版本号
SpringBoot程序启动
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*/*
@SpringBootApplication:
1. 表示这个类是一个springboot应用程序的入口类。
2. 要想让程序启动,只需要在main方法里面写上这样的一句话:
SpringApplication.run(当前类的字节码对象, args);
3. 拓展:
3.1 springboot项目启动的时候,默认会扫描启动类所在的位置,以及它后续的所有子包。
3.2 查找到类里面打的注解 @Controller , @Service , @RequestMapping.
3.3 springApplication.run 传递进去当前类的字节码对象,也是可以确定当前
这个启动器它的包是哪个!
*/
@SpringBootApplication
public class Demo1HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(Demo1HelloworldApplication.class, args);
}
}
SpringBoot在创建项目时,采用jar的打包方式
SpringBoot的引导类是项目的入口,运行main方法就可以启动项目
使用maven依赖管理变更起步依赖项
Jetty比Tomcat更轻量级,可扩展性更强(相较于Tomcat),谷歌应用引擎(GAE)已经全面切换为Jetty
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-jetty
mysql
mysql-connector-java
com.alibaba
druid
1.2.8
org.springframework.boot
spring-boot-starter-test
test
全部pom.xml
`
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.1
com.itheima
demo1_helloworld
0.0.1-SNAPSHOT
demo1_helloworld
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-jetty
mysql
mysql-connector-java
com.alibaba
druid
1.2.8
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
环境准备 demo2_properties
前面的创建操作和demo1类似 不在展示
框架常见的配置文件有哪几种形式?
SpringBoot提供了多种属性配置方式
application.properties
server.port=8081
重启
小备注:如果发现启动报错
解决办法
查看进程 看看有没的被人家使用了
发现被14452使用了
打开任务管理器
找到14452
在重新启动就可以
application.yml
server:
port: 8082
重新启动
application.yaml
server:
port: 8083
发现不提示 解决办法 1.2 自动提示功能消失解决方案
解决完小叶子问题之 后 就会有提示了
重启
操作步骤:
第一种:
如果发现上面的ok点不了 可能是没有application.properties 我们要先把这个给创建出来
第二种
application.properties > application.yml > application.yaml
注意事项:
SpringBoot核心配置文件名为application
SpringBoot内置属性过多,且所有属性集中在一起修改,在使用时,通过提示键+关键字修改属性
什么是yaml,和properties有什么区别?
YAML(YAML Ain't Markup Language),一种数据序列化格式
优点:
容易阅读
容易与脚本语言交互
以数据为核心,重数据轻格式
YAML文件扩展名
.yml(主流)
.yaml
大小写敏感
属性层级关系使用多行描述,每行结尾使用冒号结束
使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
#表示注释
核心规则:数据前面要加空格与冒号隔开
数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔
数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔
package com.itheima.web;
/*
在这个类里面,使用@Value来读取yml文件中的数据。
*/
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller01 {
@Value("${lesson}")
private String lesson;
//@Value("${第一层.第二层.第三层.属性名}")
@Value("${server.port}")
private String port;
@Value("${enterprise.name}")
private String name;
@Value("${enterprise.subject[0]}")
private String subject;
@RequestMapping("/readYml")
public String readYml(){
System.out.println("lesson = " + lesson);
System.out.println("port = " + port);
System.out.println("name = " + name);
System.out.println("subject = " + subject);
return "hi springboot...";
}
}
lesson: SpringBoot
server:
port: 8082
enterprise:
name: 传智播客
age: 16
tel: 4006184000
subject:
- Java
- 前端
- 大数据
注:如果报错的话 修改一下编码
如果还报错的话 就clean一下
如果还是报错的话
如果还是报错 我们就把前面没有用到的application.properties和application.yaml的端口号给注释了或把这二个文件删除
端口号给注释了
或删除
package com.itheima.web;
/*
在这个类里面,使用Environment来读取yml文件中的数据。
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller02 {
@Autowired
private Environment env;
@RequestMapping("/readYml02")
public String readYml(){
System.out.println("lesson = " + env.getProperty("lesson"));
System.out.println("port = " + env.getProperty("server.port"));
System.out.println("name = " + env.getProperty("enterprise.name"));
System.out.println("subject = " + env.getProperty("enterprise.subject[0]"));
return "hi springboot.0222..";
}
}
lesson: SpringBoot
server:
port: 8082
enterprise:
name: 传智播客
age: 16
tel: 4006184000
subject:
- Java
- 前端
- 大数据
因为封装 要得到属性 用到@Data注解 所以要加入以下依赖
org.projectlombok
lombok
package com.itheima.bean;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
private String name;
private int age ;
private String tel;
private String [] subject;
}
package com.itheima.web;
/*
在这个类里面,使用Environment来读取yml文件中的数据。
*/
import com.itheima.bean.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
@RestController
public class Controller03 {
@Autowired
private Enterprise enterprise;
@RequestMapping("/readYml03")
public String readYml(){
System.out.println("enterprise = " + enterprise);
return "hi springboot.0333..";
}
}
lesson: SpringBoot
server:
port: 8082
enterprise:
name: 传智播客
age: 16
tel: 4006184000
subject:
- Java
- 前端
- 大数据
自定义对象封装数据警告解决方案
问题:
解决
加入依赖
org.springframework.boot
spring-boot-configuration-processor
true
环境准备
在实际开发中,项目的开发环境、测试环境、生产环境的配置信息是否会一致?如何快速切换?
yaml文件多环境启动
# 激活具体的环境
spring:
profiles:
active: dev
---
# 定义开发环境
server:
port: 80
spring:
config:
activate:
on-profile: dev
---
# 定义生产环境
server:
port: 81
spring:
config:
activate:
on-profile: prod
---
# 定义测试环境
server:
port: 82
spring:
config:
activate:
on-profile: test
application.properties
# 使用properties的文件格式来定义多环境
# 1. 一套环境就一个properties文件,命名格式: application-环境的id.properries
# 2. 在application.properties 里面激活具体的环境。
spring.profiles.active=dev
application-dev.properties
server.port=8081
application-prod.properties
server.port=8082
application-test.properties
server.port=8083
启动
把application.properties放到properties-bak
带参数启动SpringBoot
打包
重新cmd 修改端口
参数加载优先顺序
参看文档:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config
环境准备
dev_mvn
dev
true
prod_mvn
prod
test_mvn
test
# 激活具体的环境
spring:
profiles:
active: ${a} # 这里的值是动态变化的,由maven来控制它的变化
---
# 声明开发环境
server:
port: 80
spring:
config:
activate:
on-profile: dev
---
# 声明生产环境
server:
port: 81
spring:
config:
activate:
on-profile: prod
---
# 声明测试环境
server:
port: 82
spring:
config:
activate:
on-profile: test
③:执行Maven编译指令
Maven指令执行完毕后,生成了对应的包,其中类参与编译,但是配置文件并没有编译,而是复制到包中
解决思路:对于源码中非java类的操作要求加载Maven对应的属性,解析${}占位符
maven-resources-plugin
utf-8
true
重新编译
Maven编译加载到属性,编译顺利通过
如果想要换其他的环境
也可以用命令的方式
打包
SpringBoot的配置文件可以放在项目的哪些地方?
java –jar springboot.jar --spring.profiles.active=test --server.port=85 --server.servlet.context-path=/heima --server.tomcat.connection-timeout=-1 ... ...
1级: file :config/application.yml 【最高】
2级: file :application.yml
3级:classpath:config/application.yml
4级:classpath:application.yml 【最低】
1级与2级留做系统打包后设置通用属性
3级与4级用于系统开发阶段设置通用属性
优先使用8888端口 因为config/application.yml的优先级高于application.yml 【最低】
Ctrl+C 结束前面的
重新启动
环境准备
有web
org.springframework.boot
spring-boot-starter-web
无web
org.springframework.boot
spring-boot-starter
不导入web也是可以自动扫描包 也是可以自动创建工程
回忆一下Spring整合JUnit的步骤?
先跑一下自己写的测试类
@SpringBootTest
public class MyTest {
@Test
public void test01(){
System.out.println("test01....");
}
}
在MyTest里面测试service的方法
测试类
package com.itheima;
import com.itheima.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class MyTest {
@Autowired
private UserService us;
@Test
public void test01(){
System.out.println("test01....");
us.add();
}
}
接口
package com.itheima.service;
public interface UserService {
void add();
}
实现类
package com.itheima.service.impl;
import com.itheima.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Override
public void add() {
System.out.println("调用了UserServiceImpl的add方法~!!~");
}
}
@SpringBootTest解释
@SpringBootTest(classes = Demo5JunitApplication.class)
什么时候加classes
1. 如果测试类所在的包 是 位于 启动类所在的包或者它的子包里面, 或者大家的包名一致,那么可以省略启动类不写!
启动类: com.itheima
测试: com.itheima
2. 如果测试类所在的包,并不是位于启动类所在包的下面,那么一定要指定启动类是谁, 否则有可能出错!
比如我们把test包放到com下面 不写classes就会出错
如果写上classes
回忆一下Spring整合MyBatis的核心思想?
SpringConfig
导入JdbcConfig
导入MyBatisConfig
`@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class, MyBatisConfig.class})
public class SpringConfig {
}
JDBCConfig
定义数据源(加载properties配置项:driver、url、username、password)
#jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db
jdbc.username=root
jdbc.password=itheima
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String userName;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource getDataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(userName);
ds.setPassword(password);
return ds;
}
}
MyBatisConfig
定义SqlSessionFactoryBean
定义映射配置
@Bean
public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource) {
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
ssfb.setTypeAliasesPackage("com.itheima.domain");
ssfb.setDataSource(dataSource);
return ssfb;
}
@Bean
public MapperScannerConfigurer getMapperScannerConfigurer() {
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.itheima.dao");
return msc;
}
SpringBoot整合Spring(不存在)
SpringBoot整合SpringMVC(不存在)
SpringBoot整合MyBatis(主要)
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot_test
username: root
password: 123456
注意事项:
SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区,或在MySQL数据库端配置时区解决此问题
jdbc:mysql://localhost:3306/springboot_test?serverTimezone=UTC
加入druid连接池 和lombok依赖
com.alibaba
druid
1.2.8
org.projectlombok
lombok
实体类bean
package com.itheima.bean;
import lombok.Data;
@Data
public class Book {
private int id;
private String type;
private String name;
private String description;
}
package com.itheima.dao;
import com.itheima.bean.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/*
要想创建出来dao的代理对象,那么需要告诉springboot,这个dao在哪里?
1. 第一种做法: 就是直接在dao的接口上打上注解 @Mapper
2. 第二种做法: 在启动类上打上注解: @MapperScan("com.itheima.dao")
*/
@Mapper
public interface BookDao {
@Select("select * from tbl_book")
List findAll();
}
package com.itheima;
import com.itheima.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Demo6MybatisApplicationTests {
@Autowired
private BookDao dao;
@Test
void testFindAll() {
System.out.println("dao.findAll() = " + dao.findAll());
}
}
测试结果:
如果不用druid连接池 就用光连接池
spring:
datasource:
#type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot_test
username: root
password: 123456
/*
要想创建出来dao的代理对象,那么需要告诉springboot,这个dao在哪里?
1. 第一种做法: 就是直接在dao的接口上打上注解 @Mapper
2. 第二种做法: 在启动类上打上注解: @MapperScan("com.itheima.dao")
*/
第二种做法
package com.itheima;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.itheima.dao")
@SpringBootApplication
public class Demo6MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(Demo6MybatisApplication.class, args);
}
}
如果二种都不打 会报以下错误
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.itheima.dao.BookDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot_test
username: root
password: 123456
启动
访问
http://localhost:8080/pages/books.html
methods: {
//列表
getAll() {
axios.get("/books").then(resp=>{
console.log(resp);
//this.dataList = resp.data.data;
});
`package com.itheima.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/*
这个类是用来包装返回给前端的数据
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Result {
private int code; //代表这次请求的代号: 表示成功或者表示失败
private String msg; //代表这次请求的代号的一个简短说明: 添加品牌成功 或者 添加品牌失败
private Object data; //代表这次请求要返回给客户端的数据,一般是针对查询操作。
}
package com.itheima.dao;
import com.itheima.bean.Book;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface BookDao {
@Select("select * from tbl_book")
List findAll();
}
`package com.itheima.service;
import com.itheima.bean.Book;
import java.util.List;
public interface BookService {
List findAll();
}
`package com.itheima.service.impl;
import com.itheima.bean.Book;
import com.itheima.dao.BookDao;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class BookServiceImpl implements BookService {
@Autowired
private BookDao dao;
@Override
public List findAll() {
return dao.findAll();
}
}
`package com.itheima.web;
import com.itheima.bean.Book;
import com.itheima.bean.Result;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RequestMapping("/books")
@RestController
public class BookController {
@Autowired
private BookService bs;
@GetMapping
public Result findAll(){
List list = bs.findAll();
return new Result(1, "查询成功" , list);
}
}
启动类打上@MapperScan("com.itheima.dao")
package com.itheima;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.itheima.dao")
@SpringBootApplication
public class Demo7SsmApplication {
public static void main(String[] args) {
SpringApplication.run(Demo7SsmApplication.class, args);
}
}
启动
methods: {
//列表
getAll() {
axios.get("/books").then(resp=>{
console.log(resp);
this.dataList = resp.data.data;
});
重启