![2小時學會springboot](http://p2.ttnews.xyz/loading.gif)
一.什麼是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}
a文件中,獲取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
1
2
3
4
四.通過jpa方式操作數據庫
導入jar ,在pom.xml中添加依賴:
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-data-jpa/<artifactid>
<dependency>
<groupid>mysql/<groupid>
<artifactid>mysql-connector-java/<artifactid>
在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<girl>{/<girl>
}
創建一個GirlController,寫一個獲取所有girl的api和添加girl的api ,自己跑一下就可以了:
@RestController
public class GirlController {
@Autowired
private GirlRep girlRep;
/**
* 查詢所有女生列表
* @return
*/
@RequestMapping(value = "/girls",method = RequestMethod.GET)
public List<girl> getGirlList(){/<girl>
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);
}
}
---------------------
原文:https://blog.csdn.net/forezp/article/details/61472783
閱讀更多 Java小優秀 的文章