2 小时学会 springboot ( 附实例讲解 )
前些天發(fā)現(xiàn)了一個巨牛的人工智能學(xué)習(xí)網(wǎng)站,通俗易懂,風(fēng)趣幽默,忍不住分享一下給大家。點擊跳轉(zhuǎn)到教程。
一.什么是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.
摘自官網(wǎng)
翻譯:采納了建立生產(chǎn)就緒Spring應(yīng)用程序的觀點。 Spring Boot優(yōu)先于配置的慣例,旨在讓您盡快啟動和運行。
spring boot 致力于簡潔,讓開發(fā)者寫更少的配置,程序能夠更快的運行和啟動。它是下一代javaweb框架,并且它是spring cloud(微服務(wù))的基礎(chǔ)。
?
二、搭建第一個sping boot 程序
可以在start.spring.io上建項目,也可以用idea構(gòu)建。本案列采用idea.
具體步驟:
new prpject -> spring initializr ->{name :firstspringboot , type: mavenproject,packaging:jar ,..} ?->{spring version :1.5.2 ?web: web } -> ...
應(yīng)用創(chuàng)建成功后,會生成相應(yīng)的目錄和文件。
其中有一個Application類,它是程序的入口:
@SpringBootApplication public class FirstspringbootApplication {public static void main(String[] args) {SpringApplication.run(FirstspringbootApplication.class, args);} }在resources文件下下又一個application.yml文件,它是程序的配置文件。默認(rèn)為空,寫點配置 ,程序的端口為8080,context-path為 /springboot:
server:port: 8080context-path: /springboot
寫一個HelloController:
運行 Application的main(),呈現(xiàn)會啟動,由于springboot自動內(nèi)置了servlet容器,所以不需要類似傳統(tǒng)的方式,先部署到容器再啟動容器。只需要運行main()即可,這時打開瀏覽器輸入網(wǎng)址:localhost:8080/springboot/hi ,就可以在瀏覽器上看到: hi you!!!
三.屬性配置
在appliction.yml文件添加屬性:
??在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;} }另外可以通過配置文件制定不同環(huán)境的配置文,具體見源碼:
spring:profiles:active: prod四.通過jpa方式操作數(shù)據(jù)庫
導(dǎo)入jar ,在pom.xml中添加依賴:
在appilication.yml中添加數(shù)據(jù)庫配置:
spring:profiles:active: proddatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/dbgirl?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8username: rootpassword: 123jpa:hibernate:ddl-auto: createshow-sql: true這些都是數(shù)據(jù)庫常見的一些配置沒什么可說的,其中ddl_auto: create 代表在數(shù)據(jù)庫創(chuàng)建表,update 代表更新,首次啟動需要create ,如果你想通過hibernate 注解的方式創(chuàng)建數(shù)據(jù)庫的表的話,之后需要改為 update.
創(chuàng)建一個實體girl,這是基于hibernate的:
@Entity public class Girl {@Id@GeneratedValueprivate 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;} }創(chuàng)建Dao接口, springboot 將接口類會自動注解到spring容器中,不需要我嗎做任何配置,只需要繼承JpaRepository 即可:
//其中第二個參數(shù)為Id的類型
public interface GirlRep extends JpaRepository<Girl,Integer>{}創(chuàng)建一個GirlController,寫一個獲取所有g(shù)irl的api和添加girl的api ,自己跑一下就可以了:
@RestController public class GirlController {@Autowiredprivate 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);}}?如果需要事務(wù)的話,在service層加@Transaction注解即可。已經(jīng)凌晨了,我要睡了.
源碼;http://download.csdn.net/detail/forezp/9778235
參考資料
說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com @廖師兄
?
轉(zhuǎn)自方志朋的博客:
https://www.fangzhipeng.com/springboot/2017/05/25/sb25-2hour.html
總結(jié)
以上是生活随笔為你收集整理的2 小时学会 springboot ( 附实例讲解 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通过Windows API 创建窗口的过
- 下一篇: 解决:Exception in thre