日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

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

前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。

一.什么是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: 8080context-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: 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;} }

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

spring:profiles:active: prod

四.通過jpa方式操作數據庫
導入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: 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

這些都是數據庫常見的一些配置沒什么可說的,其中ddl_auto: create 代表在數據庫創建表,update 代表更新,首次啟動需要create ,如果你想通過hibernate 注解的方式創建數據庫的表的話,之后需要改為 update.

創建一個實體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;} }

創建Dao接口, springboot 將接口類會自動注解到spring容器中,不需要我嗎做任何配置,只需要繼承JpaRepository 即可:

//其中第二個參數為Id的類型

public interface GirlRep extends JpaRepository<Girl,Integer>{}

創建一個GirlController,寫一個獲取所有girl的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);}}?

如果需要事務的話,在service層加@Transaction注解即可。已經凌晨了,我要睡了.

源碼;http://download.csdn.net/detail/forezp/9778235

參考資料
說明:本文部分內容均來自慕課網。@慕課網:http://www.imooc.com @廖師兄
?

轉自方志朋的博客:
https://www.fangzhipeng.com/springboot/2017/05/25/sb25-2hour.html

總結

以上是生活随笔為你收集整理的2 小时学会 springboot ( 附实例讲解 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。