您当前的位置:首页 > 百宝箱

SSM框架入门指南:从零开始构建高效Web应用

2024-11-08 12:11:52 作者:石家庄人才网
涓€銆丼SM妗嗘灦姒傝

SSM妗嗘灦锛屽嵆Spring銆丮yBatis鍜孲pring MVC鐨勯泦鎴愭鏋讹紝鏄幇浠ava Web搴旂敤寮€鍙戜腑鐨勪富娴侀€夋嫨銆傚叾妯″潡鍖栫殑鐗规€с€佷赴瀵岀殑鍔熻兘浠ュ強寮哄ぇ鐨勯泦鎴愯兘鍔涳紝浣垮緱瀹冭兘澶熷湪鏋勫缓澶嶆潅搴旂敤鏃惰〃鐜板嚭鑹层€?/p>

SSM妗嗘灦鏍稿績缁勪欢:

Spring: 鎻愪緵渚濊禆娉ㄥ叆銆侀潰鍚戝垏闈㈢紪绋嬨€佷簨鍔$鐞嗙瓑鍩虹鍔熻兘锛屽姪鍔涙瀯寤虹伒娲讳笖鏄撲簬缁存姢鐨勫簲鐢ㄣ€?/p>

MyBatis: 浣滀负浼樼鐨勬寔涔呭眰妗嗘灦锛屾敮鎸佽嚜瀹氫箟SQL鏄犲皠鍜孹ML鏄犲皠鏂囦欢锛屼娇寰楁暟鎹簱鎿嶄綔鏇翠负鐏垫椿銆?/p>

Spring MVC: 閲囩敤MVC璁捐妯″紡鐨刉eb寮€鍙戞鏋讹紝绠€鍖栦簡鎺у埗鍣ㄣ€佽鍥惧拰妯″瀷涔嬮棿鐨勪氦浜掋€?/p>

SSM妗嗘灦鐨勪富瑕佷紭鍔?

鐏垫椿鎬? 妯″潡鍖栬璁′娇寰楀彲浠ョ嫭绔嬮€夋嫨鎴栨浛鎹㈢粍浠讹紝婊¤冻鍚勭椤圭洰闇€姹傘€?/p>

鍙墿灞曟€? 妗嗘灦璁捐鑰冭檻浜嗕笌鍏朵粬宸ュ叿鍜屾妧鏈泦鎴愶紝鏄撲簬鎵╁睍銆?/p>

鎬ц兘: 浣跨敤Java搴曞眰瀹炵幇锛屾€ц兘鍗撹秺锛岄€傜敤浜庡ぇ瑙勬ā搴旂敤銆?/p>

瀹夊叏鎬? 鎻愪緵鍏ㄩ潰鐨勫畨鍏ㄧ鐞嗭紝濡傛潈闄愭帶鍒躲€佹棩蹇楃鐞嗙瓑銆?/p>浜屻€丼pring鍏ラ棬鎸囧崡

Spring鐨勬牳蹇冩蹇?

渚濊禆娉ㄥ叆(DI): 閫氳繃閰嶇疆鏂囦欢鎴栨敞瑙o紝涓哄璞℃敞鍏ユ墍闇€鐨勬湇鍔★紝瀹炵幇缁勪欢闂磋В鑰︺€?/p>

闈㈠悜鍒囬潰缂栫▼(AOP): 灏嗗鏃ュ織銆佷簨鍔$鐞嗙瓑妯垏鍏虫敞鐐逛粠涓氬姟閫昏緫涓垎绂伙紝瀹炵幇浠g爜妯″潡鍖栥€?/p>

Spring閰嶇疆鏂囦欢璇﹁В:

閫氬父浣跨敤applicationContext.xml鎴栭€氳繃娉ㄨВ鏂瑰紡杩涜閰嶇疆銆備緥濡傦細

```xml

```

鍩烘湰Bean绠$悊涓庨厤缃?

鍙互浣跨敤@Component銆丂Service銆丂Controller绛夋敞瑙f潵鏍囪Bean锛屽苟閫氳繃閰嶇疆鏂囦欢鎴栨敞瑙e疄鐜颁緷璧栨敞鍏ャ€備緥濡傦細

```java

package com.example;

@Service

public class MyService {

private final MyBean myBean;

@Autowired

public MyService(MyBean myBean) {

this.myBean = myBean;

}

```

鎴栬€呬娇鐢ㄦ瀯閫犲嚱鏁版敞鍏ャ€?/p>涓夈€丮yBatis鍩虹

MyBatis鐨勫伐浣滃師鐞?

閫氳繃閰嶇疆鏂囦欢鍜屾槧灏勬枃浠跺疄鐜版暟鎹簱鎿嶄綔銆傞厤缃枃浠跺畾涔変簡鐜淇℃伅銆佹暟鎹簱璇彞鏄犲皠绛夈€傛槧灏勬枃浠跺畾涔変簡SQL璇彞涓嶫ava瀵硅薄鐨勬槧灏勫叧绯汇€?/p>

MyBatis鏍稿績閰嶇疆:

渚嬪锛?/p>

```xml

```

SQL鏄犲皠鏂囦欢璇﹁В:

渚嬪锛屼竴涓畝鍗曠殑鏌ヨ鏄犲皠锛?/p>

```xml

SELECT FROM user WHERE id = {id}

```

鍩烘湰CRUD鎿嶄綔瀹炶返:

閫氳繃MyBatis鎻愪緵鐨勬槧灏勬枃浠朵笌閰嶇疆鏂囦欢锛岃交鏉惧疄鐜版暟鎹簱鐨勫鍒犳敼鏌ユ搷浣溿€傚洓銆丼pring涓嶮yBatis鐨勫崕涓借瀺鍚?/p>

馃崁 MyBatis涓嶴pring鐨勯泦鎴愪氦鍝嶄箰 馃崁

鍦ㄦ瀯寤虹幇浠eb搴旂敤鏃讹紝鎴戜滑甯稿父灏哠pring涓嶮yBatis缁撳悎浣跨敤锛屼袱鑰呴兘鏄紑婧愰鍩熺殑浣间郊鑰咃紝涓€涓礋璐d笟鍔¢€昏緫锛屼竴涓搮闀挎暟鎹簱鎿嶄綔銆傚浣曚紭闆呭湴灏嗚繖涓よ€呰瀺鍚堝湪涓€璧峰憿锛?/p>

馃尡 閰嶇疆涔嬭垶锛歋pring涓嶮yBatis鐨勯泦鎴愰厤缃?馃尡

浣犲彲浠ラ€夋嫨閫氳繃閰嶇疆MybatisConfiguration鎴栦娇鐢⊿pring鎻愪緵鐨凷qlSessionFactoryBean鏉ュ惎鍔ㄩ泦鎴愩€傝繖涓ょ鏂瑰紡閮芥湁鍏剁嫭鐗圭殑榄呭姏銆?/p>

鉁?鑷姩閰嶇疆涓庢墜鍔ㄩ厤缃細鍝鏇撮€傚悎浣狅紵 鉁?/p>

鑷姩閰嶇疆锛屼緷璧朣pring鐨凙utoConfiguration鏈哄埗锛岃兘澶熷ぇ澶у噺灏戜綘鐨勯厤缃枃浠堕噺锛岃浣犵殑閰嶇疆涔嬭矾鏇磋交鏉俱€備絾鎵嬪姩閰嶇疆鍒欐彁渚涗簡鏇村鐨勮嚜瀹氫箟閫夐」锛岄€傚悎閭d簺瀵圭粏鑺傛湁鏋佽嚧杩芥眰鐨勫紑鍙戣€呫€?/p>

浜斻€丼pring MVC锛氭帰绱eb寮€鍙戠殑濂ョ

馃帹 Spring MVC宸ヤ綔鍘熺悊鎻 馃帹

Spring MVC鏄竴涓熀浜嶮VC璁捐妯″紡鐨刉eb搴旂敤寮€鍙戞鏋躲€傚畠涓昏鐢辨帶鍒跺櫒銆佹ā鍨嬪拰瑙嗗浘涓夊ぇ缁勪欢鏋勬垚锛岃兘澶熻交鏉惧疄鐜拌姹傚鐞嗗拰鍝嶅簲娓叉煋銆?/p>

馃敡 鎺у埗鍣ㄥ熀纭€锛氬垱寤轰綘鐨勭涓€涓猄pring MVC鎺у埗鍣?馃敡

涓嬮潰鏄竴涓畝鍗曠殑渚嬪瓙锛屽睍绀轰簡濡備綍鍒涘缓涓€涓熀鏈殑Spring MVC鎺у埗鍣細

`@Controller`

public class MyController {

`@RequestMapping("/hello")`

public String hello(Model model) {

model.addAttribute("message", "Hello, Spring MVC!");

return "hello";

}

}

澶勭悊鍣ㄦ柟娉曚笌璇锋眰鍙傛暟鐨勭粦瀹氫篃鏄疭pring MVC鐨勪竴涓己澶у姛鑳姐€備緥濡傦細

`@PostMapping("/save")`

public String save(@RequestParam("name") String name, @RequestParam("age") int age) {

// 澶勭悊淇濆瓨閫昏緫

return "redirect:/success";

}

馃寪 鍓嶅悗绔垎绂荤殑鍒濇帰 馃寪

涓轰簡瀹炵幇鍓嶅悗绔垎绂伙紝浣犲彲浠ヤ娇鐢ㄨ鍥捐В鏋愬櫒鍜屾ā鏉垮紩鎿庯紙濡俆hymeleaf鎴朏reemarker锛夈€傚湪Spring鐨勯厤缃腑锛屼綘鍙互杩欐牱璁剧疆锛?/p>

`@Configuration`

public class WebConfig implements WebMvcConfigurer {

`@Override`

public void addViewControllers(ViewControllerRegistry registry) {

registry.addViewController("/").setViewName("index");

registry.addViewController("login").setViewName("login");

}

}

鍏€佸疄鎴樻渚嬶細鏋勫缓鐢ㄦ埛绠$悊绯荤粺鐨勭編濡欐梾绋?/h3>

馃挕 搴旂敤闇€姹傚垎鏋?馃挕

璁炬兂鎴戜滑鏋勫缓涓€涓敤鎴风鐞嗙郴缁燂紝瀹冨簲璇ュ叿澶囧摢浜涘姛鑳藉憿锛熺敤鎴烽渶瑕佽兘澶熸敞鍐屽拰鐧诲綍绯荤粺銆傜櫥褰曞悗锛屼粬浠彲浠ユ煡鐪嬭嚜宸辩殑璇︾粏淇℃伅锛屽鐢ㄦ埛鍚嶃€侀偖绠辩瓑銆備负浜嗘弧瓒宠繖浜涢渶姹傦紝鎴戜滑闇€瑕佺粨鍚圫pring鍜孧yBatis鐨勫己澶х殑鍔熻兘鏉ュ疄鐜拌繖涓郴缁熴€傚湪杩欎釜杩囩▼涓紝鎴戜滑灏嗗厖鍒嗗埄鐢⊿pring鐨勪緷璧栨敞鍏ャ€丮yBatis鐨勬暟鎹簱鎿嶄綔浠ュ強Spring MVC鐨刉eb寮€鍙戣兘鍔涖€傝繖灏嗕細鏄竴娆″厖婊℃寫鎴樺拰涔愯叮鐨勬梾绋嬶紒閫氳繃SSM妗嗘灦锛屽疄鐜板姛鑳芥ā鍧楃殑杩囩▼璇﹁堪濡備笅锛?/p>涓€銆佹暟鎹簱璁捐涓庤〃缁撴瀯璋冩暣

鎴戜滑闇€瑕佹瀯寤烘垜浠殑鏁版嵁妯″瀷銆傝繖鍖呮嫭鍒涘缓鐢ㄦ埛琛ㄣ€佽鑹茶〃浠ュ強鐢ㄦ埛瑙掕壊鍏宠仈琛ㄣ€傜敤鎴疯〃涓昏鍖呭惈鐢ㄦ埛ID銆佺敤鎴峰悕銆佸瘑鐮併€侀偖绠卞拰娉ㄥ唽鏃堕棿绛夊叧閿俊鎭紱瑙掕壊琛ㄥ寘鍚鑹睮D鍜岃鑹插悕绉帮紱鐢ㄦ埛瑙掕壊鍏宠仈琛ㄥ垯鐢ㄤ簬寤虹珛鐢ㄦ埛涓庤闂寸殑澶氬澶氬叧绯汇€?/p>浜屻€佸疄鐜板姛鑳芥ā鍧?/h3>

鎺ヤ笅鏉ワ紝鎴戜滑灏嗕娇鐢⊿SM妗嗘灦瀹炵幇鍑犱釜鍏抽敭鐨勫姛鑳芥ā鍧楋細

娉ㄥ唽妯″潡锛氱敤鎴峰彲浠ラ€氳繃鍓嶇椤甸潰鎻愪氦娉ㄥ唽淇℃伅锛屽悗绔疷serController鎺ユ敹璇锋眰锛岄€氳繃UserService澶勭悊娉ㄥ唽閫昏緫锛屽皢鐢ㄦ埛淇℃伅瀛樺叆鏁版嵁搴擄紝骞堕噸瀹氬悜鍒扮櫥褰曢〉闈€?/p>

鐧诲綍妯″潡锛氱敤鎴疯緭鍏ョ敤鎴峰悕鍜屽瘑鐮佸悗锛岄€氳繃UserController澶勭悊鐧诲綍璇锋眰锛岄獙璇佺敤鎴蜂俊鎭紝鑻ラ獙璇佹垚鍔熷垯鍏佽鐢ㄦ埛璁块棶搴旂敤锛屽苟閲嶅畾鍚戝埌涓婚〉銆?/p>

鏌ヨ鐢ㄦ埛淇℃伅妯″潡锛氶€氳繃UserController鎺ユ敹璇锋眰锛岃皟鐢║serService鏍规嵁鐢ㄦ埛ID鏌ヨ鐢ㄦ埛淇℃伅锛屽苟灏嗕俊鎭坊鍔犲埌妯″瀷涓紝鐒跺悗杩斿洖鐢ㄦ埛淇℃伅椤甸潰銆?/p>

涓? 浠g爜缂栧啓涓庤皟璇?/p>

鍦ㄥ紑鍙戣繃绋嬩腑锛屾垜浠皢浣跨敤IDEA浣滀负闆嗘垚寮€鍙戠幆澧冭繘琛屼唬鐮佺紪鍐欍€傞厤缃瀯寤哄伐鍏峰Maven鎴朑radle鏉ョ鐞嗛」鐩緷璧栧拰鏋勫缓杩囩▼銆傚埄鐢↖DEA鐨勮皟璇曞姛鑳借繘琛屼唬鐮佽皟璇曪紝纭繚浠g爜璐ㄩ噺鍜屽姛鑳芥纭€с€?/p>鍥涖€佸簲鐢ㄩ儴缃蹭笌娴嬭瘯

閰嶇疆搴旂敤鏈嶅姟鍣ㄥTomcat鎴朖etty鏉ラ儴缃叉垜浠殑搴旂敤銆備娇鐢≒ostman鎴栨祻瑙堝櫒杩涜API娴嬭瘯锛岀‘淇濆簲鐢ㄥ湪鍚勭鍦烘櫙涓嬬殑绋冲畾鎬у拰鎬ц兘銆?/p>

瀵逛簬SSM妗嗘灦鐨勫涔犲拰瀹炶返锛岃繕闇€瑕佹敞鎰忎竴浜涘父瑙侀棶棰樺拰瑙e喅鎬濊矾銆傚閰嶇疆鏂囦欢娣蜂贡銆佺己灏戝叏灞€渚濊禆绛夐棶棰橈紝鍙互閫氳繃浣跨敤Spring鍛藉悕绌洪棿閰嶇疆銆佹槑纭尯鍒嗕笉鍚岀粍浠跺拰閰嶇疆鍖哄煙銆侀伩鍏嶄緷璧栨贩涔辩瓑鏂瑰紡鏉ヨВ鍐炽€?/p>

涓轰簡鏇存繁鍏ュ湴瀛︿範SSM妗嗘灦鍜孞ava Web寮€鍙戞妧鏈紝鎺ㄨ崘涓€浜涜繘闃跺涔犺祫婧愬鎱曡缃戞彁渚涚殑Java Web寮€鍙戣绋嬨€併€奡pring + MyBatis + Spring MVC瀹炴垬銆嬬瓑涔︾睄銆傚垎浜竴浜涘父鐢ㄥ伐鍏峰拰鏈€浣冲疄璺碉紝濡備娇鐢↖ntelliJ IDEA鎴朎clipse浣滀负闆嗘垚寮€鍙戠幆澧冦€佷娇鐢℅it杩涜浠g爜鐗堟湰绠$悊銆佸埄鐢↗enkins绛夊伐鍏峰疄鐜拌嚜鍔ㄥ寲鏋勫缓鍜岄儴缃茬瓑銆?/p>

閫氳繃鏈寚鍗楃殑瀛︿範鍜屽疄璺碉紝浣犲皢鑳藉鐔熺粌鎺屾彙SSM妗嗘灦鐨勯泦鎴愪笌搴旂敤锛屽苟鑳藉鏋勫缓楂樻晥銆佸彲缁存姢鐨刉eb搴旂敤銆?/p>

版权声明:《SSM框架入门指南:从零开始构建高效Web应用》来自【石家庄人才网】收集整理于网络,不代表本站立场,所有图片文章版权属于原作者,如有侵略,联系删除。
https://www.ymil.cn/baibaoxiang/27514.html