1.spring boot 特点

2. 创建工程

最后的文件结构如下图

3. Gril.java
package girl;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* Created by Think on 2017/10/3.
*/
@Entity
public class Girl
{
@Id
@GeneratedValue
private Integer id;
private String cupSize;
private Integer age;
public Girl(){}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
4. ComApplication
package girl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ComApplication {
public static void main(String[] args) {
SpringApplication.run(ComApplication.class, args);
}
}
5. 创建接口
package girl;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Created by Think on 2017/10/3.
*/
public interface GirlRepository extends JpaRepository<Girl, Integer>{
}
5. GirlProperties.java
package girl;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Created by Think on 2017/10/3.
*/
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
private String cupSize;
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
private Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
6. 配置文件
使用的数据库为MySql
application.yml
spring:
profiles:
active: prod
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dbgirl
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update
show-sql: true
application-dev.yml
server: port: 8080 girl: cupSize: B age: 18
application-prod.yml
server: port: 8082 girl: cupSize: F age: 18
7. Controller文件
package girl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by Think on 2017/10/3.
*/
@RestController
public class GirlController {
@Autowired
private GirlRepository girlRepository;
@Autowired
private GirlService girlService;
/**
* 查詢所以女生列表
* @return
*/
@GetMapping(value = "/girls")
public List<Girl> girlList(){
return girlRepository.findAll();
}
/**
* 添加一個女生
* @param cupSize
* @param age
* @return
*/
@PostMapping(value = "/girls")
public Girl girlAdd(@RequestParam("cupSize") String cupSize,
@RequestParam("age") Integer age){
Girl girl = new Girl();
girl.setCupSize(cupSize);
girl.setAge(age);
return girlRepository.save(girl);
}
@PostMapping(value = "/girls/two")
public void girlTwo(){
girlService.insertTow();
}
}
原文:http://www.cnblogs.com/linlf03/p/7629418.html