ssm 搭建

SM框架——Spring+SpringMVC+Mybatis的搭建教程

一:概述

SSM框架在項(xiàng)目開發(fā)中經(jīng)常使用到,相比于SSH框架,它在僅幾年的開發(fā)中運(yùn)用的更加廣泛。

Spring作為一個(gè)輕量級(jí)的框架,有很多的拓展功能,最主要的我們一般項(xiàng)目使用的就是IOC和AOP。

SpringMVC是Spring實(shí)現(xiàn)的一個(gè)Web層,相當(dāng)于Struts的框架,但是比Struts更加靈活和強(qiáng)大!

Mybatis是 一個(gè)持久層的框架,在使用上相比Hibernate更加靈活,可以控制sql的編寫,使用 XML或注解進(jìn)行相關(guān)的配置!

根據(jù)上面的描述,學(xué)習(xí)SSM框架就非常的重要了!

二:搭建一個(gè)SSM的過程

使用Maven管理項(xiàng)目

使用Maven在Eclipse中創(chuàng)建一個(gè)webapp的項(xiàng)目 ,具體的創(chuàng)建過程不做演示,如有不會(huì)創(chuàng)建的[創(chuàng)建項(xiàng)目]

也可以使用Maven命令進(jìn)行創(chuàng)建,在Dos窗口進(jìn)入指定的目錄,執(zhí)行下面命令:

mvnarchetype:create -DgroupId=org.ssm.dufy -DartifactId=ssm-demo -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

使用命令要注意,系統(tǒng)安裝了Maven,并配置好了環(huán)境變量![Maven的安裝和環(huán)境變量配置]

導(dǎo)入項(xiàng)目(命名創(chuàng)建),添加依賴

導(dǎo)入項(xiàng)目是IDE中,或者直接在IDE創(chuàng)建,一般默認(rèn)有【src/main/java】,手動(dòng)創(chuàng)建【src/test/resources】、【src/test/java】文件夾。

如下項(xiàng)目結(jié)構(gòu):

然后直接配置 pom.xml文件中的包依賴!

4.0.0org.dufyssmwar0.0.1-SNAPSHOTssmDemohttp://maven.apache.org4.0.5.RELEASE3.2.11.6.61.2.125.1.35org.springframeworkspring-core${spring.version}org.springframeworkspring-context${spring.version}org.springframeworkspring-context-support${spring.version}org.springframeworkspring-aop${spring.version}org.springframeworkspring-aspects${spring.version}org.springframeworkspring-tx${spring.version}org.springframeworkspring-jdbc${spring.version}org.springframeworkspring-web${spring.version}org.springframeworkspring-test${spring.version}testorg.springframeworkspring-webmvc${spring.version}org.springframeworkspring-web${spring.version}mysqlmysql-connector-java${mysql.version}com.alibabadruid0.2.23com.alibabafastjson1.1.41log4jlog4j${log4j.version}org.slf4jslf4j-api${slf4j.version}ch.qos.logbacklogback-classic1.1.2ch.qos.logbacklogback-core1.1.2org.logback-extensionslogback-ext-spring0.1.1org.mybatismybatis${mybatis.version}org.mybatismybatis-spring1.2.0javax.servletjavax.servlet-api3.0.1javax.servlet.jspjavax.servlet.jsp-api2.3.2-b01javax.servletjstl1.2junitjunit3.8.1testssmDemo

創(chuàng)建數(shù)據(jù)庫和表,生成代碼

創(chuàng)建數(shù)據(jù)庫我參考別人的博客數(shù)據(jù)庫設(shè)計(jì),這塊沒有自己去書寫,直接添上代碼

DROPTABLEIFEXISTS`user_t`;CREATETABLE`user_t`(`id`int(11)NOTNULLAUTO_INCREMENT,`user_name`varchar(40)NOTNULL,`password`varchar(255)NOTNULL,`age`int(4)NOTNULL,? ? PRIMARYKEY(`id`)? )ENGINE=InnoDBAUTO_INCREMENT=2DEFAULTCHARSET=utf8;/*Data for the table `user_t` */insertinto`user_t`(`id`,`user_name`,`password`,`age`)values(1,'測試','sfasgfaf',24)

生成代碼請查看:

[Mybatis自動(dòng)生成代碼]

生成的代碼導(dǎo)入圖片解釋:

Spring 和 mybatis整合,連接數(shù)據(jù)庫,進(jìn)行Junit測試

將生成的代碼拷貝到項(xiàng)目中,然后進(jìn)行Spring和Mybatis的整合,添加配置文件!

主要有

applicationContent.xml :Spring的相關(guān)配置!

Spring-mhbatis.xml : Spring和Mybatis集成配置!

jdbc.properties : 數(shù)據(jù)庫信息配置!

logback.xml : 日志輸出信息配置?。?b>不做介紹,詳細(xì)信息查看源碼)

主要介紹applicationContext.xml、Spring-mhbatis.xml、jdbc.properties。主要內(nèi)容如下:

jdbc.properties

jdbc_driverClassName=com.mysql.jdbc.Driverjdbc_url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8jdbc_username=rootjdbc_password=root

applicationContext.xml


http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

"> 可以不在配置 -->

spring-mybatis.xml


? http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

? http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

? http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

測試代碼,兩種方式:

測試1:

package org.ssm.dufy.service;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;importorg.ssm.dufy.entity.User;/**

* 配置spring和junit整合,junit啟動(dòng)時(shí)加載springIOC容器 spring-test,junit

*/@RunWith(SpringJUnit4ClassRunner.class)// 告訴junit spring配置文件@ContextConfiguration({"classpath:applicationContext.xml"})publicclassIUserServiceTest{? ? @AutowiredpublicIUserServiceuserService;? ? ? ? @Testpublicvoid getUserByIdTest(){Useruser = userService.getUserById(1);System.out.println(user.getUserName());? ? }? ? }

測試2:

package org.ssm.dufy.service;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importorg.ssm.dufy.entity.User;publicclassIUserServiceTest2{publicstaticvoid main(String[] args) {ApplicationContextapplication = newClassPathXmlApplicationContext("applicationContext.xml");IUserServiceuService = (IUserService) application.getBean("userService");Useruser = uService.getUserById(1);System.out.println(user.getUserName());? ? }}

5:整合SpringMVC,添加配置,創(chuàng)建jsp

SpringMVC需要的依賴在pom.xml中已經(jīng)加上了,現(xiàn)在需在Web項(xiàng)目中的web.xml中添加啟動(dòng)SpringMVC啟動(dòng)配置和Spring整合SpringMVC的配置了。

新增如下兩個(gè)文件:

spring-mvc.xml


? ? http://www.springframework.org/schema/beans

? ? http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

? ? http://www.springframework.org/schema/context

? ? http://www.springframework.org/schema/context/spring-context-3.2.xsd

? ? http://www.springframework.org/schema/mvc

? ? http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> application/json -->json=application/json? ? ? ? ? ? ? ? xml=application/xml? ? ? ? ? ? ? ? html=text/html/ 映射時(shí),能映射靜態(tài)資源 -->

web.xml

SSM-DEMOcontextConfigLocationclasspath:applicationContext.xml


? ? ? ? webAppRootKey

? ? ? ? springmvc.root


? ? -->SpringEncodingFilterorg.springframework.web.filter.CharacterEncodingFilterencodingUTF-8forceEncodingtrueSpringEncodingFilter/*logbackConfigLocationclasspath:logback.xmlch.qos.logback.ext.spring.web.LogbackConfigListenerorg.springframework.web.context.ContextLoaderListenerdispatcherServletorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:spring-mvc.xml1dispatcherServlet/index.jsp

新增index.jsp文件

<%@pagecontentType="text/html; charset=utf-8"%>

Hello World!

6.啟動(dòng)web服務(wù),測試

將上面的項(xiàng)目添加到Tomcat中,啟動(dòng),控制臺(tái)沒有報(bào)錯(cuò),并在地址欄訪問,http://localhost:8080/ssm。頁面顯示?Hello World! 項(xiàng)目配置ok!

7:編寫Controller,和對(duì)應(yīng)得業(yè)務(wù)界面

新增UserController?,通過參數(shù)傳遞uid獲取用戶,若用戶存在,跳轉(zhuǎn)到showName.jsp ,若用戶不存在,則跳轉(zhuǎn)到error.jsp,并返回提示信息!

package org.ssm.dufy.web;importjavax.servlet.http.HttpServletRequest;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.servlet.ModelAndView;importorg.ssm.dufy.entity.User;importorg.ssm.dufy.service.IUserService;@ControllerpublicclassUserController{? ? @AutowiredprivateIUserServiceuserService;? ? ? ? @RequestMapping(value="/showname",method=RequestMethod.GET)publicStringshowUserName(@RequestParam("uid") int uid,HttpServletRequestrequest,Modelmodel){System.out.println("showname");Useruser = userService.getUserById(uid);if(user != null){? ? ? ? ? ? request.setAttribute("name", user.getUserName());? ? ? ? ? ? model.addAttribute("mame", user.getUserName());return"showName";? ? ? ? }? ? ? ? request.setAttribute("error","沒有找到該用戶!");return"error";? ? }}

Jsp內(nèi)容如下:

showName.jsp

<%@pagecontentType="text/html; charset=utf-8"%>show name

Welcome

訪問此頁面

error.jsp

<%@pagecontentType="text/html; charset=utf-8"%>error page

${error }

三:遇到的問題

1:找不到UserService,報(bào)錯(cuò)

可能是包掃描路徑的問題,檢查一下Service是否在掃描的范圍內(nèi)

2:jsp接收不到request.setAttribute("","");內(nèi)容

查資料說是因?yàn)?JSP的版本問題,可以在Jsp 上面添加 <%@ page isELIgnored="false" %>

或者修改web.xml ,添加version版本!


注意:

? ? 1.沒有l(wèi)ogback.xml 文件。導(dǎo)入的jsp 基礎(chǔ)的運(yùn)行的包容易報(bào)錯(cuò)


四:心得總結(jié)

或許這些都是站在別人的基礎(chǔ)的搭建的,但是看一篇SSM的搭建文章可能很快,看完覺得自己懂了,但是我建議一定要自己去搭建一次,看一遍,和動(dòng)手做一遍完全是兩個(gè)概念!

本文為博主-阿飛(dufy)-原創(chuàng)文章,未經(jīng)博主允許可轉(zhuǎn)載,但請標(biāo)明出處,謝謝!https://www.cnblogs.com/aflyun/p/6421441.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容