Java椤圭洰寮€鍙戝涔犲叏闈㈡寚鍗?/p>
寮曡█
浠嶫ava鐨勫熀纭€鐭ヨ瘑鍏ラ棬锛屽埌瀹炴垬妗堜緥鐨勫簲鐢紝鏈瘒鎸囧崡灏嗗紩棰嗕綘閫愭鎺屾彙Java椤圭洰鍏ㄦ祦绋嬨€傛繁鍏ヤ簡瑙ava璇█鐨勬牳蹇冪壒鎬э紝鍊熷姪Eclipse鎴朓ntelliJ IDEA绛夐泦鎴愬紑鍙戠幆澧冿紝杩涜浠g爜缂栬緫涓庤皟璇曪紝鐔熸倝Java API搴撲笌甯哥敤妗嗘灦濡係pring銆丮yBatis锛屼互鏋勫缓骞剁鐞哤eb搴旂敤銆傛帉鎻′娇鐢∕aven杩涜椤圭洰绠$悊涓庢瀯寤虹殑鎶€宸с€?/p>
Java鍩虹鐭ヨ瘑鍏ラ棬
Java璇█鍩虹浠嬬粛
Java鏄竴闂ㄩ潰鍚戝璞$殑銆佽法骞冲彴鐨勩€佸己绫诲瀷鐨勭紪绋嬭瑷€銆傚紑濮嬪涔燡ava锛岄鍏堥渶瑕佷簡瑙e叾鍩烘湰璇硶鍜岀壒鐐广€?/p>
浠g爜绀轰緥锛?/p>
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
鍙橀噺涓庣被鍨嬩娇鐢?/p>
鍦↗ava涓紝鍙橀噺鏄瓨鍌ㄦ暟鎹殑瀹瑰櫒銆傛瘡涓彉閲忛兘鏈夌壒瀹氱殑绫诲瀷锛屽鏁村瀷銆佹诞鐐瑰瀷銆佸瓧绗︿覆绛夈€?/p>
浠g爜绀轰緥锛?/p>
```java
int age = 25; // 鏁村瀷鍙橀噺
double height = 1.75; // 鍙岀簿搴︽诞鐐瑰瀷鍙橀噺
String name = "Alice"; // 瀛楃涓插彉閲?/p>
```
鎺у埗缁撴瀯涓庢祦绋嬬鐞?/p>
Java鎻愪緵浜嗗绉嶆帶鍒剁粨鏋勶紝濡俰f-else銆乻witch銆乫or銆亀hile绛夊惊鐜粨鏋勶紝浠ュ強鍑芥暟鎴栨柟娉曠殑缂栧啓瀹炶返锛岀敤浜庢帶鍒剁▼搴忕殑鎵ц娴佺▼銆?/p>
浠g爜绀轰緥锛?/p>
```java
public int add(int a, int b) {
return a + b; // 杩欐槸涓€涓畝鍗曠殑鍔犳硶鍑芥暟
}
```
杩樻湁闈㈠悜瀵硅薄缂栫▼姒傚康鐨勬繁鍏ユ帰绱€?/p>
闈㈠悜瀵硅薄缂栫▼姒傚康
绫讳笌瀵硅薄鍩虹
绫绘槸灞炴€у拰鏂规硶鐨勬ā鏉匡紝鑰屽璞℃槸绫荤殑瀹炰緥銆備簡瑙g被涓庡璞$殑鍏崇郴鏄帉鎻¢潰鍚戝璞$紪绋嬬殑鍩虹銆?/p>
浠g爜绀轰緥锛氬睍绀轰簡涓€涓畝鍗曠殑Circle绫讳笌鍏跺疄渚嬪寲鐨勮繃绋嬨€?/p>
```java
class Circle {
double radius;
void setRadius(double r) { radius = r; } double getArea() { return Math.PI radius radius; }}public class Main { public static void main(String[] args) { Circle circle = new Circle(); circle.setRadius(5); System.out.println("Area: " + circle.getArea()); }}
``` 鎺ヤ笅鏉ヤ粙缁嶅皝瑁呫€佺户鎵夸笌澶氭€佺殑姒傚康浠ュ強绫荤殑鏋勯€犱笌瀹炰緥鍖栫瓑闈㈠悜瀵硅薄缂栫▼鐨勬牳蹇冭绱犮€傚皝瑁呴殣钘忓璞$殑瀹炵幇缁嗚妭锛涚户鎵垮厑璁稿瓙绫荤户鎵跨埗绫荤殑灞炴€у拰鏂规硶锛涘鎬佸厑璁镐娇鐢ㄥ熀绫诲紩鐢ㄨ皟鐢ㄥ瓙绫荤殑鍏蜂綋鏂规硶銆傚悓鏃舵繁鍏ユ帰璁ㄧ被鐨勬瀯閫犳柟娉曚笌瀹炰緥鍖栬繃绋嬨€備緥濡傦細Person绫荤殑鏋勯€犲櫒鐢ㄤ簬鍒涘缓瀵硅薄鏃跺垵濮嬪寲灞炴€с€傛渶鍚庝粙缁岼ava寮€鍙戝伐鍏风殑浣跨敤銆?Java寮€鍙戝伐鍏蜂粙缁?Eclipse涓嶪ntelliJ IDEA鐨勪娇鐢‥clipse鍜孖ntelliJ IDEA鏄祦琛岀殑Java闆嗘垚寮€鍙戠幆澧冿紙IDE锛夛紝鎻愪緵浠g爜缂栬緫銆佽皟璇曘€佺増鏈帶鍒剁瓑鍔熻兘銆?浠g爜缂栬緫涓庤皟璇曞湪IDE涓紪鍐欎唬鐮佸苟鍒╃敤鍐呯疆鐨勮皟璇曞伐鍏疯繘琛岃皟璇曞彲浠ュぇ澶ф彁楂樺紑鍙戞晥鐜囧拰浠g爜璐ㄩ噺銆傝繖浜涘伐鍏峰彲浠ュ府鍔╁紑鍙戣€呮洿鏂逛究鍦扮紪鍐欎唬鐮併€佹煡鎵鹃敊璇苟杩涜璋冭瘯浠庤€屾洿鍔犻珮鏁堝湴缂栧啓鍑洪珮璐ㄩ噺鐨凧ava绋嬪簭銆傞€氳繃鏈寚鍗楃殑瀛︿範浣犲皢鎺屾彙Java椤圭洰寮€鍙戠殑鍏ㄩ潰鐭ヨ瘑浠庡熀纭€璇硶鍒伴珮绾х紪绋嬫妧宸у啀鍒板疄鐢ㄧ殑寮€鍙戝伐鍏风殑浣跨敤涓轰綘鍦↗ava寮€鍙戦鍩熺殑鍙戝睍鎵撲笅鍧氬疄鐨勫熀纭€銆傛帉鎺х増鏈紝绮剧粏鍏ュ井锛欸it鍦ㄧ増鏈帶鍒朵腑鐨勫簲鐢?/p>
鍒╃敤Git杩涜鐗堟湰鎺у埗锛岀‘淇濅唬鐮佺殑姣忎竴姝ュ彉杩侀兘鏈夎抗鍙惊锛屽洟闃熷崗浣滄洿鍔犻珮鏁堟棤璇€傞€氳繃绠€鍗曠殑鍛戒护锛屽鈥済it add .鈥濓紝鈥済it commit -m 'initial commit'鈥濆拰鈥済it push origin master鈥濓紝鎴戜滑鍙互杞绘澗绠$悊浠g爜鐨勭増鏈€?/p>
娣卞叆Java鐨勪笘鐣岋細API搴撱€丼pring銆丮yBatis绛夋牳蹇冩妧鏈殑鎺㈢储
涓€銆丣ava API搴撳簲鐢?/h3>
Java API搴撲负鎴戜滑鎻愪緵浜嗕赴瀵岀殑绫诲拰鎺ュ彛锛屽嚑涔庡彲浠ュ鐞嗕换浣曚换鍔°€傛棤璁烘槸鏁版嵁澶勭悊銆佺綉缁滈€氫俊杩樻槸骞跺彂缂栫▼锛孞ava API搴撻兘鑳芥弧瓒虫垜浠殑闇€姹傘€?/p>
浜屻€丼pring妗嗘灦鍏ラ棬
Spring鏄竴涓交閲忕骇鐨勬帶鍒跺弽杞紙IoC锛夊拰渚濊禆娉ㄥ叆锛圖I锛夊鍣ㄦ鏋躲€傚畠鏀寔闈㈠悜鍒囬潰缂栫▼锛圓OP锛夊拰澹版槑寮忎簨鍔★紝鍙互甯姪鎴戜滑鏋勫缓澶嶆潅鐨勪紒涓氱骇搴旂敤銆傚湪Spring涓紝鎴戜滑鍙互杞绘澗鍦板疄鐜颁緷璧栨敞鍏ュ拰MVC鏋舵瀯锛屼娇寰椾唬鐮佺粨鏋勬洿鍔犳竻鏅般€?/p>
涓? MyBatis涓嶫DBC鏁版嵁搴撴搷浣滃姣?/p>
MyBatis鏄竴涓紭绉€鐨凧ava ORM妗嗘灦锛屽畠绠€鍖栦簡鏁版嵁搴撴搷浣滐紝鎻愰珮浜嗗紑鍙戞晥鐜囥€傜浉瀵逛簬鍘熷鐨凧DBC锛孧yBatis鏇村姞绠€娲併€佹槗鐢ㄣ€傞€氳繃绠€鍗曠殑閰嶇疆锛屾垜浠氨鍙互灏哠QL璇彞涓嶫ava浠g爜鍏宠仈璧锋潵锛屽疄鐜版暟鎹殑鎸佷箙鍖栨搷浣溿€備緥濡傦細
Configuration configuration = new Configuration();
configuration.addMapper(UserMapper.class);
SqlSession session = configuration.buildSessionFactory().openSession();
User user = session.selectOne("com.example.mapper.UserMapper.getUserById", 1);
session.close();
瀹炴垬Java椤圭洰锛氫粠Web搴旂敤鍒扮敤鎴锋敞鍐岀櫥褰曠殑瀹夊叏淇濋殰
鍥涖€佹瀯寤轰竴涓畝鍗曠殑Web搴旂敤
浣跨敤Spring Boot锛屾垜浠彲浠ュ揩閫熸瀯寤轰竴涓熀鏈殑Web搴旂敤銆傞€氳繃绠€鍗曠殑娉ㄨВ锛屽@SpringBootApplication鍜孈RestController锛屾垜浠氨鍙互杞绘澗瀹炵幇璺敱銆佹帶鍒跺櫒鍜岃鍥俱€備緥濡傦細
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class WebApp {
public static void main(String[] args) {SpringApplication.run(WebApp.class, args);}
@GetMapping("/hello")
public String hello() {return "Hello, World!";}
}
浜斻€佸疄鐜扮敤鎴锋敞鍐屼笌鐧诲綍鍔熻兘
涓轰簡纭繚Web搴旂敤鐨勫畨鍏ㄦ€э紝鎴戜滑闇€瑕佸疄鐜扮敤鎴锋敞鍐屼笌鐧诲綍鍔熻兘銆係pring Security鏄竴涓己澶х殑韬唤楠岃瘉鍜屾巿鏉冩鏋讹紝瀹冨彲浠ュ府鍔╂垜浠疄鐜拌繖涓€鍔熻兘銆傞€氳繃Spring Security锛屾垜浠彲浠ヨ交鏉惧湴瀵筗eb搴旂敤杩涜韬唤楠岃瘉鍜屾巿鏉冿紝淇濋殰鐢ㄦ埛鏁版嵁鐨勫畨鍏ㄣ€侻aven椤圭洰绠$悊涓庢瀯寤虹殑榄呭姏
Maven锛岃繖娆捐嚜鍔ㄥ寲鏋勫缓銆佹祴璇曞拰閮ㄧ讲鐨勫埄鍣紝宸叉垚涓哄紑鍙戣€呬滑鐨勫績澶村ソ銆傚畠甯姪鎴戜滑灏嗛」鐩鐞嗙殑绻佹潅浜嬪姟绠€鍖栵紝璁╂垜浠笓娉ㄤ簬缂栫▼鏈韩銆?/p>
浠g爜绀轰緥锛圥OM鏂囦欢绮惧僵鍛堢幇锛夛細
```xml
4.0.0
com.example
example-webapp
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
1.8
1.8
org.springframework.boot
spring-boot-maven-plugin
```
---
Java寮€鍙戞渶浣冲疄璺典笌璋冭瘯鎶€宸т箣鏃?馃殌 鍚▼锛佽笍涓婁綘鐨勭紪鐮佷箣鏃咃紝甯︿笂杩欎簺瀹炵敤鎸囧崡锛岃浣犵殑浠g爜鏇翠紭闆呫€佹洿鏈夋晥鐜囷紒鉁?馃専 馃挭 馃殌 馃敡 馃敩 馃摝 馃摎 馃挕 馃敟 馃敆绛夛紒鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪锛佲湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁紒锛佲湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁紒锛佲湪鉁紒锛佲湪鉁紒锛佲湪锛佲湪锛佲湪锛佲湪锛佲湪锛佲湪锛佲湪锛佲湪锛佲湪锛佲湪锛佲湪锛佺瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑绛夌瓑锛侊紒锛侌煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婐煒婏紒锛侊紒锛侊紒锛侊紒锛佺瓑浣犱滑鍝︼紒锛佲湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁紒锛侊紒鉁紒锛侌煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燂紒锛佷竴鐫归閲囷紒绮惧僵鏃犻檺锛侊紒锛侊紒锛?绛夌潃浣犱滑鏉ヤ綋楠屽摝锛侊紒锛侌煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燂紒锛佷笅闈㈣繘鍏ヨ鎴戜滑鏉ョ湅鐪嬩竴浜汮ava寮€鍙戠殑鏈€浣冲疄璺碉紝杩樻湁閭d簺鍏抽敭鐨勮皟璇曟妧宸у惂銆傛垜浠皢閫愪竴鎻檽閭d簺璁╀綘鐨勪唬鐮佽繍琛屾洿娴佺晠鐨勭瀵嗘鍣ㄣ€傚噯澶囧ソ浜嗗悧锛熻鎴戜滑涓€璧峰嚭鍙戝惂锛侌煔€馃殌馃殌馃殌馃殌馃殌馃殌馃殌馃殌锛?浠g爜浼樺寲涓庢€ц兘鍒嗘瀽鎻 鍦ㄧ紪鍐欎唬鐮佺殑杩囩▼涓紝鎴戜滑涓嶄粎瑕佹敞閲嶅姛鑳界殑瀹炵幇锛屾洿瑕佸叧娉ㄤ唬鐮佺殑浼樺寲鍜屾€ц兘鍒嗘瀽銆備紭鍖栫殑鍏抽敭鍦ㄤ簬閲嶆瀯浣犵殑浠g爜缁撴瀯锛屼娇鍏舵洿涓烘竻鏅板拰楂樻晥銆傝繕闇€瑕佹敞閲嶅唴瀛樼殑浣跨敤鍜岀畻娉曠殑鏁堢巼銆備簡瑙d綘鐨勪唬鐮佸湪杩愯鏃剁殑鎯呭喌鏄潪甯搁噸瑕佺殑锛岃繖鏍峰彲浠ユ壘鍑哄彲鑳界殑鐡堕骞跺鍏惰繘琛屼紭鍖栥€傛帴涓嬫潵鎴戜滑浼氬垎浜竴浜涘叿浣撶殑浠g爜浼樺寲鏂规硶鍜屾€ц兘鍒嗘瀽鐨勬妧宸с€傝缁х画闃呰涓嬮潰鐨勫唴瀹广€傚噯澶囧ソ浜嗗悧锛熻鎴戜滑涓€鍚岃繘鍏ヨ繖涓簿褰╃殑涓栫晫鍚э紒寮傚父澶勭悊涓庨敊璇皟璇曟柟娉曞垎浜?寮傚父澶勭悊鏄瘡涓紑鍙戣€呴兘浼氶亣鍒扮殑甯歌闂銆傝壇濂界殑寮傚父澶勭悊涓嶄粎鑳芥彁楂樹唬鐮佺殑鍋ュ.鎬э紝杩樿兘甯姪鎴戜滑鏇村ソ鍦扮悊瑙d唬鐮佽繍琛屾椂鍙兘閬囧埌鐨勯棶棰樸€傛垜浠細鍒嗕韩濡備綍浣跨敤try-catch璇彞鏉ユ崟鑾峰拰澶勭悊寮傚父锛屽苟浣跨敤log4j绛夋棩蹇楀伐鍏锋潵璁板綍绋嬪簭杩愯淇℃伅銆傝繖瀵逛簬鍚庣画鐨勮皟璇曞拰闂杩借釜鏄潪甯告湁甯姪鐨勩€傛帴涓嬫潵鎴戜滑鏉ョ湅涓€涓叿浣撶殑渚嬪瓙锛氬亣璁炬垜浠湁涓€娈电畝鍗曠殑闄ゆ硶浠g爜锛屾垜浠渶瑕佺‘淇濆綋闄ゆ暟涓洪浂鏃惰兘澶熸崟鑾峰紓甯稿苟杈撳嚭鐩稿簲鐨勯敊璇俊鎭€備笅闈㈡槸涓€涓畝鍗曠殑绀轰緥浠g爜锛?```java try { int result = a / b; System.out.println("Result: " + result); } catch (ArithmeticException e) { System.out.println("Division by zero is not allowed."); } finally { System.out.println("Finally block executed."); } ```鍦ㄨ繖涓緥瀛愪腑锛屾垜浠娇鐢╰ry璇彞鏉ユ墽琛岄櫎娉曟搷浣滐紝濡傛灉闄ゆ暟涓洪浂鍒欐姏鍑篈rithmeticException寮傚父銆傛垜浠娇鐢╟atch璇彞鏉ユ崟鑾疯繖涓紓甯稿苟杈撳嚭閿欒淇℃伅銆俧inally鍧椾腑鐨勪唬鐮佹棤璁烘槸鍚﹀彂鐢熷紓甯搁兘浼氳鎵ц锛岃繖鏄竴涓緢濂界殑鍦版柟鏉ユ墽琛屾竻鐞嗘搷浣滄垨璧勬簮閲婃斁绛変换鍔°€備唬鐮侀噸鏋勪笌鐗堟湰鎺у埗绛栫暐鐨勫疄鏂?浠g爜閲嶆瀯鏄竴涓寔缁殑杩囩▼锛屽畠瑕佹眰鎴戜滑瀹氭湡瀹℃煡浠g爜骞惰繘琛岄噸鏋勩€備负浜嗕繚鎸佷唬鐮佺殑鏁存磥鍜屽彲杩借釜鎬э紝鎴戜滑杩橀渶瑕佷娇鐢ㄧ増鏈帶鍒跺伐鍏峰Git绛夋潵杩涜绠$悊銆傚湪杩欎釜杩囩▼涓紝鎴戜滑闇€瑕佹敞鎰忎唬鐮佺殑娓呮櫚鎬с€佸彲缁存姢鎬у拰鍙墿灞曟€с€傛垜浠繕闇€瑕佸叧娉ㄤ唬鐮佺殑娴嬭瘯瑕嗙洊鐜囦互纭繚閲嶆瀯鍚庣殑浠g爜浠嶇劧鑳藉姝e父宸ヤ綔銆傞€氳繃瀹炴柦杩欎簺绛栫暐锛屾垜浠彲浠ユ洿濂藉湴绠$悊鎴戜滑鐨勪唬鐮佸苟鎻愰珮鍏惰川閲忋€傚笇鏈涜繖绡囨枃绔犺兘澶熷府鍔╀綘鏇村ソ鍦扮悊瑙ava寮€鍙戠殑鏈€浣冲疄璺靛拰璋冭瘯鎶€宸с€傚湪鎺ヤ笅鏉ョ殑鏃ュ瓙閲岋紝鎴戜滑浼氱户缁垎浜洿澶氬叧浜嶫ava寮€鍙戠殑鐭ヨ瘑鍜屾妧宸с€傝缁х画鍏虫敞鎴戜滑鐨勫崥瀹㈡垨绀句氦濯掍綋璐﹀彿浠ヨ幏鍙栨洿澶氫俊鎭€傛劅璋綘浠殑闃呰鍜屾敮鎸侊紒璁╂垜浠竴鍚屽紑鍚疛ava寮€鍙戠殑鏂扮瘒绔犲惂锛佷唬鐮侀噸鏋勪箣鏃咃紙Git鎻愪氦淇℃伅绀轰緥锛夛細
git commit -m "浼樺寲涓诲嚱鏁帮紝鎻愬崌鍙鎬т笌鎬ц兘"
鍦ㄦ棩甯哥殑Java椤圭洰鐮斿彂杩囩▼涓紝寮€鍙戣€呬滑閫氳繃涓嶆柇鍦板疄璺靛拰鍒涙柊锛屾帰绱㈠嚭璁稿瀹炵敤鐨勬妧宸у拰鏂规硶銆傝繖浜涙妧宸т笉浠呰兘澶熷府鍔╁紑鍙戣€呬滑鎻愰珮浠g爜鐨勫彲璇绘€э紝杩樿兘鏄捐憲鎻愬崌浠g爜鐨勬€ц兘锛屼粠鑰屾洿鍔犻珮鏁堝湴鎺ㄨ繘椤圭洰鐨勫紑鍙戣繘绋嬨€?/p>
杩欎簺浼樺寲鎶€宸х殑搴旂敤锛屽鍚屽浠g爜鐨勭簿缁嗛洉鐞€傚畠浠浠g爜鍍忎竴鍧楃粡杩囨墦纾ㄧ殑鐜夌煶锛岄€愭笎鏁e彂鍑鸿糠浜虹殑鍏夋辰銆傞€氳繃閲嶆瀯鍜屾敼鑹牳蹇冧唬鐮侊紝鎴戜滑涓嶄粎鍙互璁╀唬鐮佹洿鍔犵畝娲佹槗鎳傦紝杩樺彲浠ヤ娇绋嬪簭杩愯鏇村姞娴佺晠锛屼粠鑰屾彁楂樻暣涓」鐩殑璐ㄩ噺鍜屾晥鐜囥€傝繖涓嶄粎鏄竴绉嶆妧鑳界殑灞曠幇锛屾洿鏄竴绉嶅浠g爜鍜岀▼搴忕儹鐖变箣鎯呯殑娴侀湶銆傚湪Java鐨勫箍闃斿ぉ鍦伴噷锛屽紑鍙戣€呬滑涓嶆柇鎺㈢储锛屼笉鏂繘姝ワ紝涓哄垱寤烘洿浼樼鐨勯」鐩€屽姫鍔涖€?/p> |