SpringBoot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化 Spring 应用的初始搭建以及开发过程。
使用了 Spring 框架后已经简化了我们的开发。而 SpringBoot 又是对 Spring 开发进行简化的,可想而知 SpringBoot 使用的简单及广泛性。
SpringBoot 开发起来特别简单,分为如下几步:


经过以上步骤后就创建了如下结构的模块,它会帮我们自动生成一个 Application 类,而该类一会在启动服务器时会用到,其实它就是服务器,springboot自身包含服务器,不需要我们导入服务器了。

== 注意:==
在创建好的工程中不需要创建配置类
创建好的项目会自动生成其他的一些文件,而这些文件目前对我们来说没有任何作用,所以可以将这些文件删除。
可以删除的目录和文件如下:
.mvn.gitignoreHELP.mdmvnwmvnw.cmd在 com.example.controller 包下创建 BookController ,代码如下:
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("id==>"+id);
return "hello,springboot!";
}
}

运行 SpringBoot 工程不需要使用本地的 Tomcat 和 插件,只运行项目 com.example 包下的 Application 类,我们就可以在控制台看出如下信息

使用apifox工具进行测试


至此入门案例已经开发完成,我们写的只有controller层的一些内容,复杂的配置都省略了。

坐标
Spring 程序中的坐标需要自己编写,而且坐标非常多
SpringBoot 程序中的坐标是我们在创建工程时进行勾选自动生成的
web3.0配置类
Spring 程序需要自己编写这个配置类。这个配置类大家之前编写过,肯定感觉很复杂
SpringBoot 程序不需要我们自己书写
配置类
Spring/SpringMVC 程序的配置类需要自己书写。而 SpringBoot 程序则不需要书写。
注意:基于Idea的
Spring Initializr快速构建SpringBoot工程时需要联网。
以后我们和前端开发人员协同开发,而前端开发人员需要测试前端程序就需要后端开启服务器,这就受制于后端开发人员。为了摆脱这个受制,前端开发人员尝试着在自己电脑上安装 Tomcat 和 Idea ,在自己电脑上启动后端程序,这显然不现实。
我们后端可以将 SpringBoot 工程打成 jar 包,该 jar 包运行不依赖于 Tomcat 和 Idea 这些工具也可以正常运行,只是这个 jar 包在运行过程中连接和我们自己程序相同的 Mysql 数据库即可。这样就可以解决这个问题,如下图

在打包之前确认自己是否导入打包插件,在我们创建springboot工程时默认是给我们导入好的,这里只是提一下,如果没有这个插件,打包好的jar包是有问题的。
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>2.3.7.RELEASEversion>
plugin>



输入命令java -jar springboot_01-0.0.1-SNAPSHOT.jar启动工程,启动完成后我们依然可以通过apifox进行测试

现在我们启动工程使用的是 tomcat 服务器,那能不能不使用 tomcat 而使用 jetty 服务器,而要切换 web 服务器就需要将默认的 tomcat 服务器给排除掉,怎么排除呢?使用 exclusion 标签


这样服务器就切换成功了,测试方法不变。
在这里以端口号的编写举例,主要有三种格式
application.properties
server.port=80
application.yml
server:
port: 81
application.yaml
server:
port: 82
注意:
SpringBoot程序的配置文件名必须是application,只是后缀名不同而已。
在这里简单配置了一下应用名称和端口号
# 应用名称
spring.application.name=springboot_01
# 应用服务 WEB 访问端口
server.port=8080
YAML(YAML Ain’t Markup Language),一种数据序列化格式。
最开始我们使用的是xml,格式如下:
<enterprise>
<name>muname>
<age>18age>
<tel>111111tel>
enterprise>
properties 类型的配置文件如下
enterprise.name=mu
enterprise.age=18
enterprise.tel=111111
yaml 类型的配置文件内容如下
enterprise:
name: mu
age: 18
tel: 111111
优点:
容易阅读
yaml 类型的配置文件比 xml 类型的配置文件更容易阅读,结构更加清晰
容易与脚本语言交互
以数据为核心,重数据轻格式
yaml 更注重数据,而 xml 更注重格式
YAML 文件扩展名:
.yml (主流).yaml上面两种后缀名都可以,以后使用更多的还是 yml 的。
空格的个数并不重要,只要保证同层级的左侧对齐即可。属性名与属性值之间使用冒号+空格作为分隔)核心规则:数据前面要加空格与冒号隔开
数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔,例如
enterprise:
name: mu
age: 18
tel: 111111
subject:
- Java
- 前端
- 大数据
注解中用于读取属性名引用方式是:${一级属性名.二级属性名……}
我们可以在 BookController 中使用 @Value 注解读取配合文件数据,如下
@RestController
@RequestMapping("/books")
public class BookController {
@Value("${server.port}")
private Integer port;
@Value("${enterprise.name}")
private String name;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println(port);
System.out.println(name);
return "hello,springboot!";
}
}
我们创建一个enterprise实体类来封装数据
public class Enterprise {
private String name;
private int age;
private String tel;
private String[] subject;
//setter and getter
//toString
}
写yaml配置文件
server:
port: 81
enterprise:
name: mu
age: 18
tel: 11111
subject:
- Java
- 前端
- 大数据
测试


上面方式读取到的数据特别零散,SpringBoot 还可以使用 @Autowired 注解注入 Environment 对象的方式读取数据。这种方式 SpringBoot 会将配置文件中所有的数据封装到 Environment 对象中,如果需要使用哪个数据只需要通过调用 Environment 对象的 getProperty(String name) 方法获取。具体代码如下:
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private Environment env;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println(env.getProperty("server.port"));
System.out.println(env.getProperty("enterprise.name"));
return "hello,springboot!";
}
}
注意:这种方式,框架内容大量数据,而在开发中我们很少使用。
SpringBoot 还提供了将配置文件中的数据封装到我们自定义的实体类对象中的方式。具体操作如下:
将实体类 bean 的创建交给 Spring 管理。
在类上添加 @Component 注解
使用 @ConfigurationProperties 注解表示加载配置文件
在该注解中也可以使用 prefix 属性指定只加载指定前缀的数据
在 BookController 中进行注入
Enterprise 实体类内容如下:
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
private String name;
private int age;
private String tel;
private String[] subject;
//setter and getter
//toString
}
BookController 内容如下:
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private Enterprise ent;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println(ent.getName());
System.out.println(ent.getSubject()[0]);
return "hello,springboot!";
}
}
注意:
使用第三种方式,在实体类上有如下警告提示

这个警告提示解决是在 pom.xml 中添加如下依赖即可
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
运行测试


步骤:
SpringBootTest 注解@Autowired 注入要测试的资源创建一个全新springboot项目,创建时导入junit测试依赖


在 com.example.service 下创建 BookService 接口,内容如下
public interface BookService {
public void save();
}
在 com.example.service.impl 包写创建一个 BookServiceImpl 类,使其实现 BookService 接口,内容如下
@Service
public class BookServiceImpl implements BookService {
public void save() {
System.out.println("book service is running ...");
}
}
编写测试类
@SpringBootTest
class SpringbootJunitApplicationTests {
@Autowired
private BookService bookService;
@Test
void save() {
bookService.save();
}
}
运行服务器

==注意:==这里的引导类所在包必须是测试类所在包及其子包。
例如:
com.examplecom.example如果不满足这个要求的话,就需要在使用 @SpringBootTest 注解时,使用 classes 属性指定引导类的字节码对象。如 @SpringBootTest(classes = Springboot07TestApplication.class)
Spring 整合 Mybatis 需要定义很多配置类
SpringConfig 配置类
导入 JdbcConfig 配置类
导入 MybatisConfig 配置类
@Configuration
@ComponentScan("com.example")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MyBatisConfig.class})
public class SpringConfig {
}
JdbcConfig 配置类
定义数据源(加载properties配置项:driver、url、username、password)
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 MapperScannerConfigurer getMapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.example.dao");
return msc;
}
@Bean
public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
ssfb.setTypeAliasesPackage("com.example.domain");
ssfb.setDataSource(dataSource);
return ssfb;
}


在 com.example.domain 包下定义实体类 User,内容如下
public class User {
private Integer id;
private String name;
//setter and getter
//toString
}
@Mapper
public interface UserDao {
@Select("select * from tbl_user where id = #{id}")
public User getById(Integer id);
}
@SpringBootTest
class SpringbootMybatisApplicationTests {
@Autowired
private UserDao userDao;
@Test
void testGetById() {
User user = userDao.getById(3);
System.out.println(user);
}
}
我们代码中并没有指定连接哪儿个数据库,用户名是什么,密码是什么。所以这部分需要在 SpringBoot 的配置文件中进行配合。
使用druid数据源
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.16version>
dependency>
这里我使用的是创建springboot项目后自带的properties配置文件,个人感觉还挺好用的,我们对里面的一些配置进行修改
# 应用名称
spring.application.name=springboot_mybatis
#下面这些内容是为了让MyBatis映射
#指定Mybatis的Mapper文件
mybatis.mapper-locations=classpath:mappers/*xml
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.example.mybatis.entity
# 数据库驱动:
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC
# 数据库用户名&密码:
spring.datasource.username=****
spring.datasource.password=****
Mybatis 会扫描接口并创建接口的代码对象交给 Spring 管理,但是现在并没有告诉 Mybatis 哪个是 dao 接口。而我们要解决这个问题需要在BookDao 接口上使用 @Mapper
注意:
SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区jdbc:mysql://localhost:3306/blue?serverTimezone=UTC,或在MySQL数据库端配置时区解决此问题


至此springboot整合mybatis完成,主要就是在创建springboot工程时导入依赖,然后编写接口,在测试类中注入接口,在接口上使用**@mapper注解让其被spring管理**并加载为一个bean
感谢各位大佬的浏览,有什么问题大家可以指出,私信或者评论都是可以的,互相学习~