日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2 小时学会 springboot ( 附实例讲解 )

發(fā)布時間:2023/12/18 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2 小时学会 springboot ( 附实例讲解 ) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前些天發(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:

@RestController ? ? //等同于同時加上了@Controller和@ResponseBody public class HelloController {//訪問/hello或者/hi任何一個地址,都會返回一樣的結(jié)果@RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)public String say(){return "hi you!!!";} }

運行 Application的main(),呈現(xiàn)會啟動,由于springboot自動內(nèi)置了servlet容器,所以不需要類似傳統(tǒng)的方式,先部署到容器再啟動容器。只需要運行main()即可,這時打開瀏覽器輸入網(wǎng)址:localhost:8080/springboot/hi ,就可以在瀏覽器上看到: hi you!!!

三.屬性配置
在appliction.yml文件添加屬性:

server:port: 8080context-path: /springbootgirl:name: Bage: 18content: 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;} }

另外可以通過配置文件制定不同環(huán)境的配置文,具體見源碼:

spring:profiles:active: prod

四.通過jpa方式操作數(shù)據(jù)庫
導(dǎo)入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中添加數(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。