Spring Boot是Spring提供的一个子项目,用于快速构建Spring应用程序
导入依赖繁琐
项目配置繁琐
为了简化如此繁琐的配置流程,SpringBoot这一子项目提供了如下特性
本质上就是一个Maven坐标,整合了完成一个功能所需要的所有坐标
遵循约定大于配置的原则,再boot程序启动后,一些Bean对象会自动注入到IOC容器,不需要手动声明,简化开发
内嵌的Tomcat、Jetty(无需部署WAR文件)
外部化配置
不需要XML配置(properties/yml)
org.springframework.boot
spring-boot-starter-web
以我用于测试的hellocontroller为例
@RestController
public class helloController {
@RequestMapping("/hello")
public String hello(){
System.out.println("hello");
return "hello";
}
}
@SpringBootApplication
public class SpringBootQsDemo1Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootQsDemo1Application.class, args);
}
}
继承parent工程
org.springframework.boot
spring-boot-starter-parent
3.2.2
引入相应起步依赖
org.springframework.boot
spring-boot-starter-web
@SpringBootApplication
public class SpringBootQsDemo1Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootQsDemo1Application.class, args);
}
}
application.properties
以配置端口和访问路径为例
server.port=8080
server.servlet.context-path=/start
application.yml / application.yaml
同上例
server:
servlet:
context-path: /start2
port: 8282
在实际开发中,更常用的是yaml配置文件
yaml层级表示更加明显
例如,如果我们在类中要调用email.username,email.code,email.host这三个值
在配置文件中就应该进行如下书写
application.properties
email.user=Cary_cacb
email.code=dawdawdadawd
email.host=smtp.qq.com
application.yml / application.yaml
email:
user: Cary_cacb
code: awdawdawd
host: smtp.qq.com
yml书写注意事项:
值前边必须有空格,作为分隔符
使用空格作为缩进表示层级关系,相同的层级左对齐
以上面配置的三个键为例
在实体类中进行如下书写
@Value("${email.user}")
private String user;
@Value("${email.code}")
private String code;
@Value("${email.host}")
private String host;
还可以使用@ConfigurationProperties()注解来完成
沿用上例
在实体类中进行如下书写
@ConfigurationProperties(prefix = "email")
@Component
public class EmailController {
private String user;
private String code;
private String host;
}
注意事项:该注解中的perfix必须与配置未见中的前缀名一致
类内部的成员变量名与配置文件中的键名也要保持一致
com.mysql
mysql-connector-j
org.mybatis.spring.boot
mybatis-spring-boot-starter
3.0.3
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cary_cacb
username: root
password: psw
以之前用过的tbl_book表为例
package com.cacb.springboot_mybatis.pojo;
public class Book {
private Integer id ;
private String type;
private String name;
private String description;
public Book(Integer id, String type, String name, String description) {
this.id = id;
this.type = type;
this.name = name;
this.description = description;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", type='" + type + '\'' +
", name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}
}
@Mapper
public interface BookMapper {
@Select("select * from tbl_book where id = #{id}")
public Book getByID(Integer id);
}
public interface BookService {
public Book getByID(Integer id);
}
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
@Override
public Book getByID(Integer id) {
return bookMapper.getByID(id);
}
}
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("getByID")
public Book getByID(Integer id){
return bookService.getByID(id);
}
}
使用测试软件测试结果如下: