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(微服務)的基礎。java
能夠在start.spring.io上建項目,也能夠用idea構建。本案列採用idea.mysql
具體步驟:web
new prpject -> spring initializr ->{name :firstspringboot , type: mavenproject,packaging:jar ,..} ->{spring version :1.5.2 web: web } -> ....
應用建立成功後,會生成相應的目錄和文件。spring
其中有一個Application類,它是程序的入口:sql
@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:api
@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文件添加屬性:springboot
server: port: 8080 context-path: /springboot girl: name: B age: 18 content: content:${name},age:${age}
在java文件中,獲取name屬性,以下:app
@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
導入jar ,在pom.xml中添加依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
在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,Integer>{ }
建立一個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註解便可。已經凌晨了,我要睡了.