Spring Boot锛氫竴涓交閲忕骇鐨凧ava寮€鍙戞鏋舵瑙?/p>
Spring Boot鏄竴涓负蹇€熸瀯寤篠pring搴旂敤鑰岀敓鐨勭畝鍖栨鏋讹紝瀹冩棬鍦ㄩ€氳繃鑷姩閰嶇疆鍜屼緷璧栨敞鍏ユ彁楂樺紑鍙戞晥鐜囷紝瑙e喅浼犵粺Spring寮€鍙戜腑鐨勯厤缃箒鐞愰棶棰樸€傝繖涓鏋跺熀浜嶴pring妗嗘灦锛屼负寮€鍙戣€呮彁渚涘揩閫熴€佺伒娲汇€侀珮鏁堢殑寮€鍙戞敮鎸併€傞€氳繃閰嶇疆鍜岀幆澧冨噯澶囥€佸熀纭€浠g爜鍒涘缓銆佹牳蹇冪粍浠舵暣鍚堜互鍙婂疄鎴樻渚嬪睍绀猴紝寮€鍙戣€呰兘澶熷埄鐢⊿pring Boot鏋勫缓楂樿川閲忕殑Java寰湇鍔″簲鐢ㄣ€?/p>
瀹夎涓庣幆澧冨噯澶?/p>
涓轰簡鍚姩Spring Boot椤圭洰锛屼綘闇€瑕佺‘淇濅綘鐨勫紑鍙戠幆澧冩弧瓒充互涓嬭姹傦細
涓€銆丣ava寮€鍙戝伐鍏凤細浣犻渶瑕佸畨瑁匤DK锛圝ava Development Kit锛夌増鏈?鎴栨洿楂樸€?/p>
浜屻€両DE锛氶€夋嫨涓€涓己澶х殑IDE锛屽IntelliJ IDEA鎴朎clipse锛屽畠浠兘鎻愪緵浜嗚壇濂界殑Spring Boot鏀寔銆?/p>
涓夈€佹瀯寤哄伐鍏凤細Spring Boot椤圭洰閫氬父閫氳繃Maven鎴朑radle鏉ョ鐞嗕緷璧栥€?/p>
鍒涘缓棣栦釜Spring Boot椤圭洰
浣跨敤IntelliJ IDEA鍒涘缓涓€涓狹aven椤圭洰锛屾祦绋嬪涓嬶細
1. 鎵撳紑IntelliJ IDEA锛岄€夋嫨鈥淐reate New Project鈥濄€?/p>
2. 閫夋嫨鈥淪pring Initializr鈥濇ā鏉匡紝鐐瑰嚮鈥淣ext鈥濄€?/p>
3. 閰嶇疆椤圭洰淇℃伅锛屽缁処D銆侀」鐩悕绉般€佸寘鍚嶇瓑銆?/p>
4. 閫夋嫨椤圭洰渚濊禆锛屼緥濡俿pring-boot-starter-web鐢ㄤ簬Web搴旂敤鍜宻pring-boot-devtools鐢ㄤ簬寮€鍙戞椂鐨勮嚜鍔ㄩ厤缃拰閲嶅惎鍔熻兘銆?/p>
5. 纭繚宸插嬀閫夆€淪pring Web鈥濄€佲€淎uto-configuration鈥濄€佲€淟ombok鈥濄€佲€淲eb鈥濇鏋堕€夐」锛岀劧鍚庣偣鍑烩€淔inish鈥濄€?/p>
椤圭洰鍒涘缓瀹屾垚鍚庯紝鎸夌収浠ヤ笅姝ラ杩愯椤圭洰锛?/p>
1. 鍙抽敭鐐瑰嚮椤圭洰锛岄€夋嫨鈥淥pen Module Settings鈥濄€?/p>
2. 鍦ㄢ€淢odules鈥濇爣绛句笅锛屾壘鍒板苟鍙屽嚮鈥淪pringApplication.run鈥濇柟娉曘€傞」鐩皢鑷姩杩愯銆?/p>
3. 鍦ㄦ祻瑙堝櫒涓闂甽ocalhost:8080锛屼綘搴旇鑳界湅鍒伴粯璁ょ殑娆㈣繋椤甸潰銆?/p>
鍩虹閰嶇疆涓庨」鐩粨鏋?/p>
Spring Boot椤圭洰鐨勫叧閿洰褰曢€氬父鍖呮嫭锛?/p>
涓€銆乻rc/main/java锛氬瓨鏀惧簲鐢ㄧ殑婧愪唬鐮併€?/p>
浜屻€乻rc/main/resources锛氬瓨鏀惧簲鐢ㄧ殑璧勬簮鏂囦欢锛屽閰嶇疆鏂囦欢銆?/p>
鍦╯rc/main/resources鐩綍涓嬪垱寤篴pplication.properties鏂囦欢锛岀敤浜庨厤缃簲鐢ㄧ殑鍩烘湰鍙傛暟銆備綘杩樺彲浠ュ湪Java浠g爜涓€氳繃娉ㄨВ杩涜閰嶇疆銆備緥濡傦紝浣跨敤@SpringBootApplication娉ㄨВ鏉ユ爣璇嗕富搴旂敤绫汇€?/p>
---
閰嶇疆鏂囦欢娣卞害瑙f瀽
鍦╝pplication.properties鏂囦欢涓紝姣忎竴琛岄兘鏄竴涓绉樼殑瀵嗙爜鐩掞紝閲岄潰瑁呯潃閰嶇疆涓栫晫鐨勭瀵嗐€傞偅浜涗互spring.寮€澶寸殑閿€煎锛屽鍚屾墦寮€鏌愪釜鍔熻兘澶ч棬鐨勯挜鍖欙紝璁㏒pring Boot鍘荤鐞嗛偅浜涜嚦鍏抽噸瑕佺殑璁剧疆銆備綘鎯崇煡閬撴暟鎹簱濡備綍鍛煎惛锛熸湇鍔″櫒搴旇鍦ㄥ摢鎵囬棬鍚庡€惧惉锛熸垨鏄棩蹇楃殑杞诲0缁嗚锛熻繖閲岄兘鑳芥壘鍒扮瓟妗堛€?/p>
鏍稿績涓殑渚濊禆绠″
Spring Boot鐨勯瓟娉曡儗鍚庯紝鏄疢aven鎴朑radle杩欎袱浣嶆瀯寤哄ぇ甯堢殑榛橀粯鏀寔銆傚湪椤圭洰鐨勬牳蹇冩枃浠秔om.xml鎴朾uild.gradle涓紝浣犲彲浠ラ€氳繃鐗瑰畾鐨勮妭鐐规潵鎷涘嫙浣犳墍闇€瑕佺殑甯墜銆備緥濡傦紝涓€澹板彫鍞わ紝spring-boot-starter-web渚垮墠鏉ユ晥鍔炽€?/p>
鎻鑷姩閰嶇疆鐨勭濂囧姏閲?/p>
璇村埌Spring Boot鐨勮嚜鍔ㄩ厤缃紝閭e氨鍍忔槸涓€鍦洪瓟娉曠銆傚熀浜庝竴绯诲垪閰嶇疆绫汇€佸睘鎬у拰娉ㄨВ锛屽綋鐗瑰畾鐨勪緷璧栬鍙敜鏃讹紝Spring Boot浼氱珛鍒诲彉韬垚涓哄垱寤哄拰閰嶇疆鐩稿叧bean鐨勭濂囧伐鍖犮€傛瘮濡傞偅涓狜EnableWebMvc娉ㄨВ锛屽畠灏卞儚涓€涓殣褰㈠紑鍏筹紝鍦ㄥ叧閿椂鍒昏嚜鍔ㄥ惎鍔ㄥ叾鍔熻兘銆?/p>
鏁版嵁搴撶殑鏃犵紳闆嗘垚浣撻獙
Spring Boot闆嗘垚浜哠pring Data JPA杩欎竴鍒╁櫒锛岃涓庢暟鎹簱鐨勪氦浜掑彉寰楄交鏉炬剦鎮︺€傚畠灏卞儚涓€浣嶈缁冩湁绱犵殑绠″锛岀畝鍖栦簡鎵€鏈夌箒鐞愭搷浣滐紝璁╀綘鍦ㄧ紪绋嬬殑涓栫晫閲岋紝濡傞奔寰楁按銆?/p>
---
---
瀹炰綋绫伙細濉戦€犳暟鎹箣榄傦紙渚嬪锛歎ser锛?/p>
鍦↗ava鐨勪笘鐣屼腑锛屽疄浣撶被鏄垜浠瀯寤烘暟鎹ā鍨嬬殑閲嶈鍩虹煶銆備互`User`涓轰緥锛屾垜浠潵浜嗚В涓€涓嬪浣曞畾涔変竴涓疄浣撶被銆傚€熷姪JPA锛圝ava Persistence API锛夌殑娉ㄨВ锛屾垜浠彲浠ヨ交鏉惧湴灏嗘暟鎹簱琛ㄦ槧灏勪负Java瀵硅薄銆傜湅涓嬮潰杩欐浠g爜锛?/p>
```java
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
// 鏋勯€犳柟娉曘€乬etter鍜宻etter鏂规硶鐪佺暐...
}
```
杩欓噷鐨刞@Entity`娉ㄨВ琛ㄦ槑杩欐槸涓€涓疄浣撶被锛岃€宍@Id`鍜宍@GeneratedValue`鍒欑敤浜庢爣璇嗗疄浣撶殑涓婚敭灞炴€с€傜畝鍗曟槑浜嗭紝鏁版嵁妯″瀷灏辫繖涔堣交鏉炬悶瀹氾紒
鎺ュ彛锛氬畾涔夋暟鎹搷浣滅殑瑙勮寖锛堜緥濡傦細UserRepository锛?/p>
鍦⊿pring Data JPA涓紝鎺ュ彛鎵紨浜嗗叧閿鑹层€傚畠浠畾涔変簡鏁版嵁鎿嶄綔鐨勬爣鍑嗘柟寮忋€備緥濡傦紝鎴戜滑鍙互瀹氫箟涓€涓猔UserRepository`鎺ュ彛鏉ユ搷浣滅敤鎴锋暟鎹細
```java
public interface UserRepository extends JpaRepository
User findByName(String name);
}
```
杩欎釜绠€鍗曠殑鎺ュ彛缁ф壙浜哷JpaRepository`锛屽苟瀹氫箟浜嗕竴涓柟娉曠敤浜庢牴鎹悕瀛楁煡鎵剧敤鎴枫€傝繖鏍凤紝鎴戜滑灏卞彲浠ヨ交鏉惧湴鍦ㄥ簲鐢ㄧ▼搴忎腑鎿嶄綔鏁版嵁搴撲簡銆?/p>
閰嶇疆绫伙細閰嶇疆搴旂敤鐨凞NA
閰嶇疆绫绘槸Spring Boot搴旂敤鐨勯噸瑕佺粍鎴愰儴鍒嗭紝瀹冧滑璐熻矗閰嶇疆搴旂敤鐨勫悇涓柟闈€備緥濡傦紝鎴戜滑鍙互浣跨敤`DataConfig`绫绘潵閰嶇疆Spring Data JPA鐨勪粨搴擄細
```java
@Configuration
@EnableJpaRepositories(basePackages = "com.example.repository")
public class DataConfig { }
```
杩欎釜閰嶇疆绫诲惎鐢ㄤ簡JPA浠撳簱锛屽苟鎸囧畾浜嗕粨搴撶殑鍖呰矾寰勩€傜畝鍗曠殑鍑犺浠g爜锛屽氨瀹屾垚浜嗗鏉傞厤缃殑娲诲効锛?/p>
瀹炴垬妗堜緥锛氭墦閫?RESTful API 鏈嶅姟
鎺ヤ笅鏉ワ紝璁╂垜浠疄鎴樻紨缁冧竴涓嬶紝濡備綍蹇€熸瀯寤轰竴涓猂ESTful API鏈嶅姟銆傛垜浠渶瑕佸垱寤轰竴涓帶鍒跺櫒鏉ュ鐞咹TTP璇锋眰锛?/p>
```java
@RestController
@RequestMapping("/api")
public class UserController {
@GetMapping("/users")
public Iterable
return userRepository.findAll();
}
}
```
杩欐浠g爜瀹氫箟浜嗕竴涓畝鍗曠殑鐢ㄦ埛鎺у埗鍣紝閫氳繃璁块棶`localhost:8080/api/users`锛屼綘灏卞彲浠ユ祴璇曡繖涓猂ESTful API浜嗐€傛柟渚垮揩鎹凤紝浣犵殑搴旂敤灏辫繖鏍封€滄椿鈥濊捣鏉ヤ簡锛?/p>
甯歌闂涓庢渶浣冲疄璺?甯︿綘璧板悜楂樻晥寮€鍙戜箣璺紒 馃殌 馃敟 馃幆 馃憣 馃挭馃徏 馃挕 ...绛夌瓑锛佽鎴戜滑鏉ョ湅鐪嬩竴浜涙渶浣冲疄璺靛惂锛佷粠浠g爜浼樺寲鍒版€ц兘璋冧紭銆佹晠闅滄帓鏌ヤ笌鏃ュ織绠$悊銆侀泦鎴愭祴璇曚笌鎸佺画闆嗘垚绛夋柟闈负浣犱竴涓€鎻檽銆傝繖浜涙寚鍗楀拰鏈€浣冲疄璺靛皢甯姪寮€鍙戣€呮洿楂樻晥鍦颁娇鐢⊿pring Boot鏋勫缓楂樿川閲忕殑Java寰湇鍔″簲鐢ㄣ€傝浣忥紝Spring Boot妗嗘灦鐨勯瓟鍔涘湪浜庡叾绠€鍖栦簡寮€鍙戞祦绋嬶紝璁╁紑鍙戣€呰兘澶熶笓娉ㄤ簬涓氬姟閫昏緫鐨勫垱鏂帮紝鏃犻渶杩囧鍏虫敞搴曞眰缁嗚妭銆傝鎴戜滑涓€璧疯繄鍚戦珮鏁堝紑鍙戠殑鏃呯▼鍚э紒
文章来自《钓虾网小编|www.jnqjk.cn》整理于网络,文章内容不代表本站立场,转载请注明出处。