SSM框架整合

Posted alida

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM框架整合相关的知识,希望对你有一定的参考价值。

1.新建Web Project ,导入 jar 包

技术图片

技术图片

 

2.web.xml

  配置Spring MVC的核心控制器DispatcherServlet、字符编码过滤器,指定Spring配置文件 ,配置ContextLoaderListener,日志文件等。

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
 3   <display-name>springMVC</display-name>
 4   <welcome-file-list>
 5     <welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
 6   </welcome-file-list>
 7   <context-param>
 8     <param-name>contextConfigLocation</param-name>
 9     <param-value>classpath:applicationContext-*.xml</param-value>
10   </context-param>
11   <filter>
12     <filter-name>encodingFilter</filter-name>
13     <filter-class>
14           org.springframework.web.filter.CharacterEncodingFilter
15       </filter-class>
16     <init-param>
17       <param-name>encoding</param-name>
18       <param-value>UTF-8</param-value>
19     </init-param>
20     <init-param>
21       <param-name>forceEncoding</param-name>
22       <param-value>true</param-value>
23     </init-param>
24   </filter>
25   <filter-mapping>
26     <filter-name>encodingFilter</filter-name>
27     <url-pattern>/*</url-pattern>
28   </filter-mapping>
29   <servlet>
30     <servlet-name>spring</servlet-name>
31     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
32     <init-param>
33       <param-name>contextConfigLocation</param-name>
34       <param-value>classpath:springmvc-servlet.xml</param-value>
35     </init-param>
36     <load-on-startup>1</load-on-startup>
37   </servlet>
38   <servlet-mapping>
39     <servlet-name>spring</servlet-name>
40     <url-pattern>/</url-pattern>
41   </servlet-mapping>
42   <listener>
43     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
44   </listener>
45   <context-param>
46     <param-name>log4jConfigLocation</param-name>
47     <param-value>classpath:log4j.properties</param-value>
48   </context-param>
49   <context-param>
50     <param-name>webAppRootKey</param-name>
51     <param-value>SMBMS_C12_09.root</param-value>
52   </context-param>
53   <listener>
54     <listener-class>
55             org.springframework.web.util.Log4jConfigListener
56         </listener-class>
57   </listener>
58 </web-app>

 

3.配置文件 (创建resources源文件夹)

  (1)applicationContext-mybatis.xml  

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <beans xmlns="http://www.springframework.org/schema/beans"  
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 4         xmlns:aop="http://www.springframework.org/schema/aop"  
 5         xmlns:p="http://www.springframework.org/schema/p"  
 6         xmlns:tx="http://www.springframework.org/schema/tx"  
 7         xmlns:context="http://www.springframework.org/schema/context"  
 8         xsi:schemaLocation="   
 9             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
10             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
11             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
12             http://www.springframework.org/schema/context 
13             http://www.springframework.org/schema/context/spring-context.xsd">  
14             
15     <context:component-scan base-package="cn.smbms.service"/> 
16     <context:component-scan base-package="cn.smbms.dao"/>  
17     
18     <!-- 读取数据库配置文件 -->
19     <context:property-placeholder location="classpath:database.properties"/>
20     
21     <!-- JNDI获取数据源(使用dbcp连接池) -->  
22     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
23             <property name="driverClassName" value="${driver}" />  
24             <property name="url" value="${url}" />  
25             <property name="username" value="${user}" />  
26             <property name="password" value="${password}" />
27             <property name="initialSize" value="${initialSize}"/>
28             <property name="maxActive" value="${maxActive}"/>
29             <property name="maxIdle" value="${maxIdle}"/>
30             <property name="minIdle" value="${minIdle}"/>
31             <property name="maxWait" value="${maxWait}"/>
32             <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
33             <property name="removeAbandoned" value="${removeAbandoned}"/>
34             <!-- sql 心跳 -->
35             <property name= "testWhileIdle" value="true"/>
36             <property name= "testOnBorrow" value="false"/>
37             <property name= "testOnReturn" value="false"/>
38             <property name= "validationQuery" value="select 1"/>
39             <property name= "timeBetweenEvictionRunsMillis" value="60000"/>
40             <property name= "numTestsPerEvictionRun" value="${maxActive}"/>
41     </bean>
42     
43     <!-- 事务管理 -->
44        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
45            <property name="dataSource" ref="dataSource"/>
46        </bean> 
47        
48        <!-- 配置mybatis SqlSessionFactoryBean-->
49     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
50         <property name="dataSource" ref="dataSource"/>
51         <property name="configLocation" value="classpath:mybatis-config.xml"/>
52     </bean>
53     
54        <!-- AOP 事务处理 开始 -->    
55     <aop:aspectj-autoproxy />
56     <aop:config  proxy-target-class="true">
57         <aop:pointcut expression="execution(* *cn.smbms.service..*(..))" id="transService"/>
58         <aop:advisor pointcut-ref="transService" advice-ref="txAdvice" />
59     </aop:config> 
60     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
61         <tx:attributes>  
62            <tx:method name="smbms*"  propagation="REQUIRED" rollback-for="Exception"  />
63         </tx:attributes>  
64     </tx:advice> 
65     <!-- AOP 事务处理 结束 -->
66     
67     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
68          <property name="basePackage" value="cn.smbms.dao" />  
69     </bean>
70     
71 </beans>

 

(2)database.properties

 1 driver=com.mysql.jdbc.Driver
 2 url=jdbc:mysql://localhost:3306/test1?useGeneral=true&characterEncoding=utf-8
 3 user=root
 4 password=
 5 minIdle=45
 6 maxIdle=50
 7 initialSize=5
 8 maxActive=100
 9 maxWait=100
10 removeAbandonedTimeout=180
11 removeAbandoned=true

 

(3)log4j.properties

 1 log4j.rootLogger=debug,CONSOLE,file
 2 #log4j.rootLogger=ERROR,ROLLING_FILE
 3 
 4 log4j.logger.cn.smbms=debug
 5 log4j.logger.org.apache.ibatis=debug
 6 log4j.logger.org.mybatis.spring=debug
 7 log4j.logger.java.sql.Connection=debug
 8 log4j.logger.java.sql.Statement=debug
 9 log4j.logger.java.sql.PreparedStatement=debug
10 log4j.logger.java.sql.ResultSet=debug
11 
12 ######################################################################################
13 # Console Appender  \\u65e5\\u5fd7\\u5728\\u63a7\\u5236\\u8f93\\u51fa\\u914d\\u7f6e
14 ######################################################################################
15 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
16 log4j.appender.Threshold=debug
17 log4j.appender.CONSOLE.DatePattern=yyyy-MM-dd
18 log4j.appender.CONSOLE.Target=System.out
19 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
20 log4j.appender.CONSOLE.layout.ConversionPattern= - (%r ms) - %d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n
21 
22 
23 
24 ######################################################################################
25 # Rolling File  \\u6587\\u4ef6\\u5927\\u5c0f\\u5230\\u8fbe\\u6307\\u5b9a\\u5c3a\\u5bf8\\u7684\\u65f6\\u5019\\u4ea7\\u751f\\u4e00\\u4e2a\\u65b0\\u7684\\u6587\\u4ef6
26 ######################################################################################
27 #log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
28 #log4j.appender.ROLLING_FILE.Threshold=INFO
29 #log4j.appender.ROLLING_FILE.File=${baojia.root}/logs/log.log
30 #log4j.appender.ROLLING_FILE.Append=true
31 #log4j.appender.ROLLING_FILE.MaxFileSize=5000KB
32 #log4j.appender.ROLLING_FILE.MaxBackupIndex=100
33 #log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
34 #log4j.appender.ROLLING_FILE.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n
35 
36 ######################################################################################
37 # DailyRolling File  \\u6bcf\\u5929\\u4ea7\\u751f\\u4e00\\u4e2a\\u65e5\\u5fd7\\u6587\\u4ef6\\uff0c\\u6587\\u4ef6\\u540d\\u683c\\u5f0f:log2009-09-11
38 ######################################################################################
39 log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
40 log4j.appender.file.DatePattern=yyyy-MM-dd
41 log4j.appender.file.File=${SMBMS_C12_09.root}/logs/log.log
42 log4j.appender.file.Append=true
43 log4j.appender.file.Threshold=debug
44 log4j.appender.file.layout=org.apache.log4j.PatternLayout
45 log4j.appender.file.layout.ConversionPattern= - (%r ms) - %d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n
46 
47 #DWR \\u65e5\\u5fd7
48 #log4j.logger.org.directwebremoting = ERROR
49 
50 #\\u663e\\u793aHibernate\\u5360\\u4f4d\\u7b26\\u7ed1\\u5b9a\\u503c\\u53ca\\u8fd4\\u56de\\u503c
51 #log4j.logger.org.hibernate.type=DEBUG,CONSOLE 
52 
53 #log4j.logger.org.springframework.transaction=DEBUG
54 #log4j.logger.org.hibernate=DEBUG
55 #log4j.logger.org.acegisecurity=DEBUG
56 #log4j.logger.org.apache.myfaces=TRACE
57 #log4j.logger.org.quartz=DEBUG
58 
59 #log4j.logger.com.opensymphony=INFO  
60 #log4j.logger.org.apache.struts2=DEBUG  
61 log4j.logger.com.opensymphony.xwork2=debug

 

(4)mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>  
    <!DOCTYPE configuration   
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"   
        "http://mybatis.org/dtd/mybatis-3-config.dtd">  
<configuration>  
      <settings>  
<!--           changes from the defaults   -->
<!--         所有相关联的实体都被初始化加载 -->
          <setting name="lazyLoadingEnabled" value="false" />  
      </settings>  
     <typeAliases>  
         <!--这里给实体类取别名,方便在mapper配置文件中使用--> 
         <package name="cn.smbms.pojo"/>
     </typeAliases> 
</configuration>  

 

(5)springmvc-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:mvc="http://www.springframework.org/schema/mvc"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans
 9         http://www.springframework.org/schema/beans/spring-beans.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context.xsd
12         http://www.springframework.org/schema/mvc
13         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
14         
15     <context:component-scan base-package="cn.smbms.controller"/>
16     
17     <mvc:annotation-driven>
18          <mvc:message-converters>
19              <bean class="org.springframework.http.converter.StringHttpMessageConverter">
20                  <property name="supportedMediaTypes">
21                      <list>
22                          <value>application/json;charset=UTF-8</value>
23                      </list>
24                  </property>
25              </bean> 
26              <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
27                  <property name="supportedMediaTypes">
28                      <list>
29                          <value>text/html;charset=UTF-8</value>
30                          <value>application/json</value>
31                      </list>
32                  </property>
33                  <property name="features">
34                      <list>
35                       <!--   Date的日期转换器 -->
36                       <value>WriteDateUseDateFormat</value>
37                     </list>
38                 </property>
39             </bean>
40         </mvc:message-converters>
41     </mvc:annotation-driven>
42     
43     <mvc:resources location="/statics/" mapping="/statics/**"></mvc:resources>
44     
45     <!-- 配置多视图解析器:允许同样的内容数据呈现不同的view -->
46     <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
47         <property name="favorParameter" value="true"/>
48         <property name="defaultContentType" value="text/html"/>
49         <property name="mediaTypes">
50             <map>
51                 <entry key="html" value="text/html;charset=UTF-8"/>
52                 <entry key="json" value="application/json;charset=UTF-8"/>
53                 <entry key="xml" value="application/xml;charset=UTF-8"/>
54             </map>
55         </property>
56         <property name="viewResolvers">
57             <list>
58                 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
59                     <property name="prefix" value="/WEB-INF/jsp/"/>
60                     <property name="suffix" value=".jsp"/>
61                 </bean>        
62             </list>
63         </property>
64     </bean>
65       
66     <!-- 配置interceptors -->
67     <mvc:interceptors>
68         <mvc:interceptor>
69             <mvc:mapping path="/sys/**"/>
70             <bean class="cn.smbms.interceptor.SysInterceptor"/>
71         </mvc:interceptor>
72     </mvc:interceptors>
73     
74     
75     <!-- 配置MultipartResolver,用于上传文件,使用spring的CommonsMultipartResolver -->  
76     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
77          <property name="maxUploadSize" value="5000000"/>
78          <property name="defaultEncoding" value="UTF-8"/>
79     </bean>
80 </beans>   

 

如图所示

技术图片

 

ssm框架整合好了

以上是关于SSM框架整合的主要内容,如果未能解决你的问题,请参考以下文章

SSM 项目整合详细解读

520前,我放弃陪女朋友时间,被迫写代码:“SSM框架整合+excel文件上传到数据库+数据更新“

SSM框架整合

Java EE互联网轻量级框架整合开发— SSM框架(中文版带书签)原书代码

手把手整合SSM框架

黑马程序员最全SSM框架用户角色案例(SSM整合版)