關注公眾號:平凡的程式猿
回覆:送書
送45本《Spring 5企業級開發實戰》包郵免費送
《Spring 5企業級開發實戰》
一。什麼是spring boot
Takes an opinionated view of building production-ready Spring applications。 Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible。——摘自官網
翻譯:採納了建立生產就緒Spring應用程式的觀點。 Spring Boot優先於配置的慣例,旨在讓您儘快啟動和執行。
spring boot 致力於簡潔,讓開發者寫更少的配置,程式能夠更快的執行和啟動。它是下一代javaweb框架,並且它是spring cloud(微服務)的基礎。
二、搭建第一個sping boot 程式
可以在start。spring。io上建專案,也可以用idea構建。本案列採用idea。
具體步驟:
new prpject -> spring initializr ->{name :firstspringboot , type: mavenproject,packaging:jar ,。。} ->{spring version :1。5。2 web: web } -> 。。。。
應用建立成功後,會生成相應的目錄和檔案。
其中有一個Application類,它是程式的入口:
@SpringBootApplication
public class FirstspringbootApplication {
public static void main(String[] args) {
SpringApplication。run(FirstspringbootApplication。class, args);
}
}
在resources檔案下下又一個application。yml檔案,它是程式的配置檔案。預設為空,寫點配置 ,程式的埠為8080,context-path為 /springboot:
server:
port: 8080
context-path: /springboot
寫一個HelloController:
@RestController //等同於同時加上了@Controller和@ResponseBody
public class HelloController {
//訪問/hello或者/hi任何一個地址,都會返回一樣的結果
@RequestMapping(value = {“/hello”,“/hi”},method = RequestMethod。GET)
public String say(){
return “hi you!!!”;
}
}
執行 Application的main(),呈現會啟動,由於springboot自動內建了servlet容器,所以不需要類似傳統的方式,先部署到容器再啟動容器。只需要執行main()即可,這時開啟瀏覽器輸入網址:localhost:8080/springboot/hi ,就可以在瀏覽器上看到: hi you!!!
三。屬性配置
在appliction。yml檔案新增屬性:
server:
port: 8080
context-path: /springboot
girl:
name: B
age: 18
content: content:${name},age:${age}
在java檔案中,獲取name屬性,如下:
@Value(“${name}”)
private String name;
也可以透過ConfigurationProperties註解,將屬性注入到bean中,透過Component註解將bean註解到spring容器中:
@ConfigurationProperties(prefix=“girl”)
@Component
public class GirlProperties {
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方式操作資料庫
匯入jar ,在pom。xml中新增依賴:
在appilication。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
這些都是資料庫常見的一些配置沒什麼可說的,其中ddl_auto: create 代表在資料庫建立表,update 代表更新,首次啟動需要create ,如果你想透過hibernate 註解的方式建立資料庫的表的話,之後需要改為 update。
建立一個實體girl,這是基於hibernate的:
@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 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介面, springboot 將介面類會自動註解到spring容器中,不需要我嗎做任何配置,只需要繼承JpaRepository 即可:
//其中第二個引數為Id的型別
public interface GirlRep extends JpaRepository
}
建立一個GirlController,寫一個獲取所有girl的api和新增girl的api ,自己跑一下就可以了:
@RestController
public class GirlController {
@Autowired
private GirlRep girlRep;
/**
* 查詢所有女生列表
* @return
*/
@RequestMapping(value = “/girls”,method = RequestMethod。GET)
public List
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註解即可。
——————————-
作者:方誌朋
原文:https://blog。csdn。net/forezp/article/details/61472783
——————————-