一 、什么是Spring Boot
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible.
Spring Boot使创建独立的、生产级的基于Spring的应用程序变得很容易,您可以“直接运行”这些应用程序。Spring Boot优先于配置的惯例,旨在让您尽快启动和运行。
二 、搭建一个简单的Spring Boot程序
1)创建项目
可以在IDEA上构建,也可以在start.spring.io上构建,本文是在IDEA上构建,应用创建完成之后,会生成相应的目录和文件其中有一个Application类,它是程序的入口:
@SpringBootApplication
public class FirstspringbootApplication {
public static void main(String[] args) {
SpringApplication.run(FirstspringbootApplication.class, args);
}
}
2)创建yml文件
在resources文件下创建一个application.yml文件,它是程序的配置文件。默认为空,写入配置,程序的端口为8080,context-path为 /springboot
server:
port: 8080
context-path: /springboot
3)创建Controller
@RestController
public class HelloController {
@RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)
public String say(){
return "hi you!";
}
}
运行Application的main(),呈现会启动,由于springboot自动内置了Servlet容器,所以不需要类似传统的方式,先部署到容器中在启动容器,只需要运行main()即可,这时打开浏览器输入地址localhost:8080/springboot/hi ,就可以在浏览器上看到: hi you!
三 、属性配置
在application.yml文件添加属性:
server:
port: 8080
context-path: /springboot
body:
name: john
age: 18
content: content:${name},age:${age}
在java文件中获取name属性,如下:
@Value("${name}")
private String name;
也可以通过ConfigurationProperties注解,将属性注入到bean中,通过Component注解将bean注入到spring容器中
@ConfigurationProperties(prefix="body")
@Component
public class BodyProperties {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
另外可以通过配置文件制定不同环境的配置:
spring:
profiles:
active: prod
四 、采用JPA方式操作数据库
在pom.xml中加入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
在application.yml文件中加入配置
spring:
profiles:
active: prod
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/dbgirl?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
username: root
password: 123
jpa:
hibernate:
ddl-auto: create
show-sql: true
创建一个实体body,这是基于hibernate的:
@Entity
public class Body{
@Id
@GeneratedValue
private Integer id;
private String cupSize;
private Integer age;
public Body() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
}
创建Dao接口, Spring Boot 将接口类会自动注解到spring容器中,不需要做任何配置,只需要继承JpaRepository 即可:
public interface GirlRep extends JpaRepository<Girl,Integer>{
}
创建一个BodyController,写一个获取所有body的API和添加body的API
@RestController
public class BodyController {
@Autowired
private BodyRep BodylRep;
/** * 查询所有男生列表 * @return */
@RequestMapping(value = "/bodys",method = RequestMethod.GET)
public List<Girl> getBodyList(){
return bodyRep.findAll();
}
/** * 添加一个男生 * @param cupSize * @param age * @return */
@RequestMapping(value = "/bodys",method = RequestMethod.POST)
public Girl addGirl(@RequestParam("cupSize") String cupSize,
@RequestParam("age") Integer age){
Body body = new Body();
body.setAge(age);
body.setCupSize(cupSize);
return bodyRep.save(body);
}
}