如何搭建 SSM 框架集成环境?

SsM 框架集成环境搭建方式

jar 包依赖添加

web.xml 文件配置

Springmvc 配置文件 servlet-context.xml 添加

Spring.xml 配置

案例实操

1.jar 包依赖添加(原有基础上继续添加 springmvc 相关依赖 jar 包及对应 jetty 插件) 修改 pom.xml 文件

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

http://maven.apache.org/maven-v4_0_0.xsd"> 

<modelVersion>4.0.0</modelVersion> 

<groupId>com.xxx</groupId> 

<artifactId>spring_mybatis</artifactId> 

<packaging>war</packaging> 

<version>0.0.1-SNAPSHOT</version> 

<name>spring_mybatis Maven Webapp</name> 

<url>http://maven.apache.org</url> 

<dependencies> 


<dependency> 

<groupId>junit</groupId> 

<artifactId>junit</artifactId> 

<version>4.12</version> 

<scope>test</scope> 

</dependency> 


<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-context</artifactId> 

<version>4.3.2.RELEASE</version> 

</dependency> 


<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-test</artifactId> 

<version>4.3.2.RELEASE</version> 

</dependency> 


<dependency>

<groupId>org.springframework</groupId> 

<artifactId>spring-jdbc</artifactId> 

<version>4.3.2.RELEASE</version> 

</dependency> 


<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-tx</artifactId> 

<version>4.3.2.RELEASE</version> 

</dependency> 


<dependency> 

<groupId>org.aspectj</groupId> 

<artifactId>aspectjweaver</artifactId> 

<version>1.8.9</version> 

</dependency> 


<dependency> 

<groupId>c3p0</groupId> 

<artifactId>c3p0</artifactId> 

<version>0.9.1.2</version> 

</dependency> 


<dependency> 

<groupId>org.mybatis</groupId> 

<artifactId>mybatis</artifactId> 

<version>3.4.1</version> 

</dependency> 


<dependency> 

<groupId>org.mybatis</groupId> 

<artifactId>mybatis-spring</artifactId> 

<version>1.3.0</version> 

</dependency> 


<dependency> 

<groupId>mysql</groupId> 

<artifactId>mysql-connector-java</artifactId> 

<version>5.1.39</version> 

</dependency> 


<dependency> 

<groupId>org.slf4j</groupId> 

<artifactId>slf4j-log4j12</artifactId> 

<version>1.7.2</version> 

</dependency> 

<dependency> 

<groupId>org.slf4j</groupId> 

<artifactId>slf4j-api</artifactId> 

<version>1.7.2</version> 

</dependency> 


<dependency> 

<groupId>com.github.pagehelper</groupId> 

<artifactId>pagehelper</artifactId> 

<version>4.1.0</version> 

</dependency> 


<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-web</artifactId> 

<version>4.3.2.RELEASE</version> 

</dependency> 


<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-webmvc</artifactId> 

<version>4.3.2.RELEASE</version> 

</dependency> 


<dependency> 

<groupId>javax.servlet</groupId> 

<artifactId>javax.servlet-api</artifactId> 

<version>3.0.1</version> 

</dependency> 


<dependency> 

<groupId>com.fasterxml.jackson.core</groupId> 

<artifactId>jackson-core</artifactId> 

<version>2.7.0</version> 

</dependency> 

<dependency> 

<groupId>com.fasterxml.jackson.core</groupId> 

<artifactId>jackson-databind</artifactId> 

<version>2.7.0</version> 

</dependency> 

<dependency> 

<groupId>com.fasterxml.jackson.core</groupId> 

<artifactId>jackson-annotations</artifactId> 

<version>2.7.0</version> 

</dependency> 


<dependency> 

<groupId>commons-fileupload</groupId> 

<artifactId>commons-fileupload</artifactId> 

<version>1.3.2</version> 

</dependency> 

</dependencies> 

<build> 

 <finalName>ssM</finalName> 

 <resources> 

<resource> 

 <directory>**src/main/resources**</directory> 

 </resource> 

<resource> 

 <directory>**src/main/java**</directory> 

 <includes> 

<include>***\*/\*.xml**</include> 

<include>***\*/\*.properties**</include> 

<include>***\*/\*.tld**</include> 

 </includes> 

 <filtering>**false**</filtering> 

</resource> 

</resources> 

<plugins> 

<plugin> 

<groupId>org.mybatis.generator</groupId> 

<artifactId>mybatis-generator-maven-plugin</artifactId> 

<version>1.3.2</version> 

<configuration> 

 <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> 

<verbose>true</verbose> 

<overwrite>true</overwrite> 

</configuration> 

<dependencies> 

<dependency> 

<groupId>org.mybatis.generator</groupId> 

<artifactId>mybatis-generator-core</artifactId> 

<version>1.3.2</version> 

</dependency> 

</dependencies> 

</plugin> 


<plugin> 

<groupId>org.apache.maven.plugins</groupId> 

<artifactId>maven-compiler-plugin</artifactId> 

<version>2.3.2</version> 

<configuration> 

<source>1.7</source> 

<target>1.7</target> 

<encoding>UTF-8</encoding> 

<compilerArguments> 

<bootclasspath>${project.basedir}/lib/rt.jar</bootclasspath> 

</compilerArguments> 

</configuration> 

</plugin> 


<plugin> 

<groupId>org.mortbay.jetty</groupId> 

<artifactId>maven-jetty-plugin</artifactId> 

<version>6.1.21</version> 

<configuration> 

<scanIntervalSeconds>10</scanIntervalSeconds> 

<contextPath>/ssM</contextPath> 

</configuration> 

</plugin> 

</plugins> 

</build> 

</project>

2.web.xml 文件配置


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 

id="WebApp_ID" version="3.0"> 

<context-param> 

<param-name>contextConfigLocation</param-name> 

<param-value>classpath:spring.xml</param-value> 

</context-param> 

<listener> 

<listener-

class>org.springframework.web.context.ContextLoaderListener</listener-class> 

</listener> 

<filter> 

 <description>char encoding filter</description> 

<filter-name>encodingFilter</filter-name> 

<filter

class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 

<init-param> 

<param-name>encoding</param-name> 

<param-value>UTF-8</param-value> 

</init-param> 

</filter> 

<filter-mapping> 

<filter-name>encodingFilter</filter-name> 

<url-pattern>/*</url-pattern> 

</filter-mapping> 

<servlet> 

<servlet-name>springMvc</servlet-name> 

<servlet

class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 

<init-param> 

<param-name>contextConfigLocation</param-name> 

<param-value>classpath:servlet-context.xml</param-value> 

</init-param> 

<load-on-startup>1</load-on-startup> 

</servlet> 

<servlet-mapping> 

<servlet-name>springMvc</servlet-name> 

<url-pattern>*.do</url-pattern> 

</servlet-mapping> 

</web-app>

3.Springmvc 配置文件 servlet-context.xml 添加


<beans xmlns="http://www.springframework.org/schema/beans" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:mvc="http://www.springframework.org/schema/mvc" 

xmlns:context="http://www.springframework.org/schema/context" 

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx" 

xsi:schemaLocation="

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

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

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

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

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-2.5.xsd 

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 


<context:component-scan base-package="com.xxx.controller" /> 


<mvc:annotation-driven /> 


<bean id="defaultViewResolver" 

class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 

<property name="viewClass" 

value="org.springframework.web.servlet.view.JstlView" /> 

<property name="contentType" value="text/html" /> 

<property name="prefix" value="/WEB-INF/jsp/" /> 

<property name="suffix" value=".jsp" /> 

</bean> 


<bean 

class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHand 

lerMapping"> 

</bean> 

<bean 

class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHand 

lerAdapter"> 

 <property name="messageConverters"> 

<list> 

<bean 

class="org.springframework.http.converter.json.MappingJackson2HttpMessageConver 

ter" /> 

</list> 

</property> 

</bean> 


<bean id="multipartResolver" 

class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 

<property name="maxUploadSize"> 

<value>104857600</value> 

</property> 

<property name="maxInMemorySize"> 

<value>4096</value> 

</property> 

</bean> 

</beans>

4.Spring.xml 配置


<beans xmlns="http://www.springframework.org/schema/beans" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context" 

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx" 

xsi:schemaLocation="http://www.springframework.org/schema/beans 

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

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

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

http://www.springframework.org/schema/aop

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

http://www.springframework.org/schema/tx

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


<context:component-scan base-package="com.xxx" > 

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 

</context:component-scan> 


<context:property-placeholder location="classpath:db.properties" /> 

<aop:aspectj-autoproxy /> 


<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 

<property name="driverClass" value="${driver}"></property> 

<property name="jdbcUrl" value="${url}"></property> 

<property name="user" value="${user}"></property> 

<property name="password" value="${password}"></property> 

</bean> 


<bean id="txManager" 

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 

 <property name="dataSource" ref="dataSource"></property> 

</bean> 


<tx:advice id="txAdvice" transaction-manager="txManager"> 

<tx:attributes> 

<tx:method name="get*" read-only="true" /> 

<tx:method name="find*" read-only="true" /> 

<tx:method name="query*" read-only="true" /> 

<tx:method name="load*" read-only="true" /> 

<tx:method name="add*" propagation="REQUIRED" /> 

<tx:method name="insert*" propagation="REQUIRED" /> 

<tx:method name="update*" propagation="REQUIRED" /> 

<tx:method name="delete*" propagation="REQUIRED" /> 

</tx:attributes> 

</tx:advice> 


<aop:config> 

<aop:pointcut id="servicePointcut" 

expression="execution(* com.xxx.service..*.*(..))" /> 

<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut" /> 

</aop:config> 


<bean id="sqlSessionFactory"

class="org.mybatis.spring.SqlSessionFactoryBean"> 

<property name="dataSource" ref="dataSource"></property> 

<property name="configLocation" value="classpath:mybatis.xml" /> 

<property name="mapperLocations"

value="classpath:com/xxx/mapper/*.xml" /> 

</bean> 


<bean id="mapperScanner"

class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 


<property name="basePackage" value="com.xxx.dao" /> 

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 

</bean> 

</beans>

扩展

Helloworld 编写

搭建好SsM集成框架,Hellocontroller 的编写就变得非常简单

@Controller 

public class HelloController { 

/** 

* 注入 service 层 userService 接口 

*/ 

@Autowired 

private UserService userService; 

@RequestMapping("/hello") 

public ModelAndView hello(){ 

ModelAndView mv=new ModelAndView(); 

/** 

* 调用 service 层查询方法 

*/ 

User user=userService.queryUserById(7); 

System.out.println(user); 

mv.addObject("user", user); 

mv.setViewName("hello"); 

return mv; 

}

视图页面部分代码接收结果显示:

<body> 

欢迎你,${user.userName} 

</body>

启动 jetty 服务器,访问地址: http://localhost:8080/ssM/hello.do

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,254评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,875评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,682评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,896评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,015评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,152评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,208评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,962评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,388评论 1 304
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,700评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,867评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,551评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,186评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,901评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,142评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,689评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,757评论 2 351

推荐阅读更多精彩内容