前言:上一節我們說了如何創建、配置、啓動springboot項目,本節我們就談談如何編寫springboot代碼。
一、屬性配置
在spring中,我們經常要配置很多東西,在springboot中我們也需要配置一些東西,首先我們先配置如何連接上一個數據庫,這裏我們使用mysql作爲數據庫。
在resources目錄下創建一個application.yml,內容如下圖
上圖中的spring:profiles:active:dev是切換不同配置環境使用的,在開發中,我們不免會有不同的開發環境,如測試環境、生產環境、開發環境。
在本節中,我們做了兩個環境,一個開發環境,一個生產環境。
通過給文件加一個後綴,實現不同的配置環境,在主配置文件中,使用active屬性進行程序配置環境的切換。
當我們在配置文件中配置一些固定的值,我們可以再java代碼中使用註解獲取到
如:
girl:
age: 18
cupSize: B
@Value("${age}")
private Integer age;
也可以使用ConfigurationProperties註解獲取,然後構成一個對象
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
private Integer age;
private String cupSize;
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;
}
}
二:操作數據庫
在springboot中,我們使用jpa的方式操作數據庫,jpa中有非常好用的api,十分方便我們隊數據庫的操作。
首先我們需要先添加兩個依賴。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
刷新pom.xml後
我們創建一個實體類,它對應數據庫中的一個表
@Entity
public class Girl {
@Id
@GeneratedValue
private Integer id;
private String name;
private String cupSize;
private Integer age;
public Girl() {
}
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 getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Girl{" +
"id=" + id +
", name='" + name + '\'' +
", cupSize='" + cupSize + '\'' +
", age=" + age +
'}';
}
}
首先在數據庫中新建一個我們配置文件中對應的數據庫,然後執行程序,此時會在數據庫中自動創建一個表,也即是girl表,注意,第一次創建的時候ddl-auto:一定要是create
spring:
profiles:
active: dev
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
三、創建Dao接口
springboot會自動將接口注入到容器中,不需要做任何配置,只需要繼承“JpaRepository”即可
public interface GirlRepository extends JpaRepository<Girl, Integer>{
List<Girl> getGirlByAge(Integer age);
}
創建好dao層的接口後,我們就直接創建controller控制器了,因爲程序簡單,我們就不在創建service層了。
創建GirlController,寫一個獲取所有girl的api和添加girl的api ,自己跑一下就可以了:
@RestController
public class GirlController {
@Autowired
private GirlRep girlRep;
/**
* 查詢所有女生列表
* @return
*/
@RequestMapping(value = "/girls",method = RequestMethod.GET)
public List<Girl> getGirlList(){
return girlRep.findAll();
} /**
* 添加一個女生
* @param cupSize
* @param age
* @return
*/
@RequestMapping(value = "/girls",method = RequestMethod.POST)
public Girl addGirl(@RequestParam("cupSize") String cupSize,
@RequestParam("age") Integer age){
Girl girl = new Girl();
girl.setAge(age);
girl.setCupSize(cupSize);
return girlRep.save(girl);
}
}
如果需要事務的話,在service層加@Transaction註解即可。
@RestController註解其實是@Controller與@ResponseBody的結合。
四、小結
如此,我們就完成了一個很簡單的,springboot操作數據庫的程序,應該很簡單的,其實除了一些配置和註解,跟springmvc沒什麼太大的區別。
當然一些更深的東西,我也還沒學到,哈哈。
over,期待下次的分享。
代碼下載