SSM框架入门指南:从零开始构建高效Web应用
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>
- 上一篇:Java高并发直播资料:入门到实践的全方位指南
- 下一篇:返回列表
版权声明:《SSM框架入门指南:从零开始构建高效Web应用》来自【石家庄人才网】收集整理于网络,不代表本站立场,所有图片文章版权属于原作者,如有侵略,联系删除。
https://www.ymil.cn/baibaoxiang/27514.html