Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss.
Most Spring Boot applications need very little Spring configuration.
化繁为简,简化配置。
SpringBoot Hello World
环境准备:jdk1.8, maven:3.5.3
开发工具:[IntelliJ IDEA] (https://www.jetbrains.com/idea/)
maven的settings.xml中最好配置阿里的mirror。
1 2 3 4 5 6
| <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
|
创建spring项目:



这里勾选web一个选项即可。

注意路径不要有中文。

生成maven项目。
启动项目
不用做其他任何配置,先直接启动项目,爽一把。(或者命令行:mvn spring-boot run)

启动后,web访问:http://127.0.0.1:8080/

OK,说明项目启动成功了。
对比之前的springmvc项目,这个实在是太简单了吧。
编写一个helloworld类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.tony.girl;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
@RestController public class HelloController {
@RequestMapping(value = "hello", method = RequestMethod.GET) public String sayHello() {
return "hello world Spring Boot";
}
}
|
重新启动项目,web访问http://127.0.0.1:8080/hello

自定义属性配置
默认提供的配置文件为application.properties,建议使用yml文件格式,更简单便捷。

将服务端口改成8081. http://127.0.0.1:8081/hello
使用属性值
在application.yml 中配置:
1 2 3 4 5 6 7 8
| server: port: 8081
name: "zhangsan"
age: 18
userinfo: "name:${name},age:${age}"
|
HelloController中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.tony.girl;
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
@RestController public class HelloController {
@Value("${userinfo}") private String userinfo;
@RequestMapping(value = "hello", method = RequestMethod.GET) public String sayHello() {
return userinfo;
}
}
|
访问:http://127.0.0.1:8081/hello

将属性配置映射为bean
1 2 3
| user: name: "123" age: 20
|
新建model类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package com.tony.girl;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;
@Component @ConfigurationProperties(prefix = "user") public class UserProperties {
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; }
}
|
使用的时候直接注入就行了。
1 2
| @Autowired private UserProperties userProperties;
|
Spring-data-jpa
Spring-data-jpa使用2.0.6版本:
1 2 3 4 5
| <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>2.0.6.RELEASE</version> </dependency>
|
在application.yml文件中配置datasource
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| spring: datasource: url: jdbc:mysql: username: root password: root jpa: database: MYSQL show-sql: true #Hibernate ddl auto hibernate: ddl-auto: create naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy properties: hibernate: dialect: org.hibernate.dialect.MySQL5Dialect
|
创建一个Person类,来映射数据库表。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.tony.girl;
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id;
@Entity public class Person {
@Id @GeneratedValue private Integer id;
private String name;
private String age;
public Person() { }
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getAge() { return age; }
public void setAge(String age) { this.age = age; } }
|
运行,到mysql中看到表自动创建出来。
编写一则查询demo
先创建一个Person的repository接口,继承JpaRepository。
1 2 3 4 5 6 7
| package com.tony.girl;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
public interface PersonRepository extends JpaRepository<Person,Integer>{ }
|
编写PersonController,为了方便省去service层,直接调用dao接口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.tony.girl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController public class PersonController {
@Autowired private PersonRepository personRepository;
@GetMapping(value="/persons") public List<Person> personList(){
return personRepository.findAll();
}
}
|
运行起来,看下效果。http://192.168.56.1:8080/persons

事务管理
加入事物很简单,只需要在进行事物管理的方法上加入@Transactional注解即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.tony.girl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import java.util.List;
@Service public class PersonService {
@Autowired private PersonRepository personRepository;
@Transactional public void insertPerson(){
Person p = new Person(); p.setAge("20"); p.setName("zhangsan");
personRepository.save(p);
}
}
|