SSM(Spring+SpringMVC+Mybatis)框架整合配置方法

本文将介绍SSM三大框架的配置以及整合的方法。


目录

    • 目录
    • 1框架所需JAR包文件的引入
      • 1 对于maven项目
      • 2 对于javaweb项目在WebRootWEB-INFlib文件中加入下列JAR包
    • 2webxml文件的配置
    • 3Spring配置以及Spring与Mybatis的整合
      • 1 Spring的配置springxml文件
      • 2 数据源配置dbconfigproperties文件
      • 3 日志管理的配置log4jproperties文件
      • 4 Mybatis的配置mybatis-configxml文件
    • 4配置SpringMVC


1、框架所需JAR包文件的引入

框架所需的JAR包文件如下:

1.1 对于maven项目:

pom.xml

<dependencies>  
      
    <dependency>  
        <groupId>org.springframeworkgroupId>  
        <artifactId>spring-coreartifactId>  
        <version>4.3.7version>  
    dependency>  
    <dependency>  
        <groupId>org.springframeworkgroupId>  
        <artifactId>spring-webartifactId>  
        <version>4.3.7version>  
    dependency>  
    <dependency>  
        <groupId>org.springframeworkgroupId>  
        <artifactId>spring-oxmartifactId>  
        <version>4.3.7version>  
    dependency>  
    <dependency>  
        <groupId>org.springframeworkgroupId>  
        <artifactId>spring-txartifactId>  
        <version>4.3.7version>  
    dependency>  
    <dependency>  
        <groupId>org.springframeworkgroupId>  
        <artifactId>spring-jdbcartifactId>  
        <version>4.3.7version>  
    dependency>  
    <dependency>  
        <groupId>org.springframeworkgroupId>  
        <artifactId>spring-webmvcartifactId>  
        <version>4.3.7version>  
    dependency>  
    <dependency>  
        <groupId>org.springframeworkgroupId>  
        <artifactId>spring-aopartifactId>  
        <version>4.3.7version>  
    dependency>  
    <dependency>  
        <groupId>org.springframeworkgroupId>  
        <artifactId>spring-context-supportartifactId>  
        <version>4.3.7version>  
    dependency>  
    <dependency>  
        <groupId>org.springframeworkgroupId>  
        <artifactId>spring-testartifactId>  
        <version>4.3.7version>  
    dependency>  
      
    <dependency>  
        <groupId>org.mybatisgroupId>  
        <artifactId>mybatisartifactId>  
        <version>3.4.2version>  
    dependency>  
      
    <dependency>  
        <groupId>org.mybatisgroupId>  
        <artifactId>mybatis-springartifactId>  
        <version>1.3.1version>  
    dependency>  
      
    <dependency>  
        <groupId>mysqlgroupId>  
        <artifactId>mysql-connector-javaartifactId>  
        <version>5.1.41version>  
    dependency>  
    
    <dependency>
        <groupId>c3p0groupId>
        <artifactId>c3p0artifactId>
        <version>0.9.1version>
    dependency>
      
    <dependency>  
        <groupId>jstlgroupId>  
        <artifactId>jstlartifactId>  
        <version>1.2version>  
    dependency>  
      
    <dependency>  
        <groupId>log4jgroupId>  
        <artifactId>log4jartifactId>  
        <version>1.2.17version>  
    dependency>  
    
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
    dependency>  
      
    <dependency>
        <groupId>com.fasterxml.jackson.coregroupId>
        <artifactId>jackson-databindartifactId>
        <version>2.8.8version>
    dependency>  
dependencies>  

1.2 对于javaweb项目,在WebRoot/WEB-INF/lib文件中加入下列JAR包:

c3p0-0.9.1.jar
jackson-databind-2.8.8.jar
jstl-1.2.jar
junit-4.12.jar
log4j-1.2.17.jar
mybatis-3.4.2.jar
mybatis-spring-1.3.1.jar
mysql-connector-java-5.1.41.jar
spring-aop-4.3.7.RELEASE.jar
spring-aspects-4.3.7.RELEASE.jar
spring-beans-4.3.7.RELEASE.jar
spring-context-4.3.7.RELEASE.jar
spring-core-4.3.7.RELEASE.jar
spring-expression-4.3.7.RELEASE.jar
spring-jdbc-4.3.7.RELEASE.jar
spring-test-4.3.7.RELEASE.jar
spring-tx-4.3.7.RELEASE.jar
spring-web-4.3.7.RELEASE.jar
spring-webmvc-4.3.7.RELEASE.jar


2、web.xml文件的配置

这里我们将Spring以及SpringMVC的基本配置信息加入到web.xml文件中,主要分为Spring、SpringMVC、字符编码配置三个部分。

注意:这里将Spring、Mybatis以及SpringMVC的具体配置文件放到了src目录项的config中。

/WebRoot/WEB-INF/web.xml文件的内容如下:
SSM(Spring+SpringMVC+Mybatis)框架整合配置方法_第1张图片


<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:config/spring.xmlparam-value>
    context-param>
    
    <servlet>
        <servlet-name>springMVCservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:config/spring-mvc.xmlparam-value>
        init-param>
        
        <load-on-startup>1load-on-startup>
    servlet>
    
    <servlet-mapping>
        <servlet-name>springMVCservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>
    
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
    listener>
    
    <filter>
        <filter-name>CharacterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
        <init-param>
            <param-name>forceRequestEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
        <init-param>
            <param-name>forceResponseEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
web-app>

3、Spring配置以及Spring与Mybatis的整合

整个Spring的配置以及与Mybatis的整合需要在src/config目录下配置spring.xml、dbconfig.properties、log4j.properties、mybatis-config.xml四个文件。
SSM(Spring+SpringMVC+Mybatis)框架整合配置方法_第2张图片

3.1 Spring的配置(spring.xml文件)

Spring配置的核心主要是三点:数据源的配置、与Mybatis的整合以及事务控制。
在src目录下新建config包,新建spring.xml文件。
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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    
    <context:component-scan base-package="com.lims">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    context:component-scan>
    
    
    <context:property-placeholder location="classpath:config/dbconfig.properties" />
    <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}">property>
        <property name="driverClass" value="${jdbc.driverClass}">property>
        <property name="user" value="${jdbc.user}">property>
        <property name="password" value="${jdbc.password}">property>
    bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="configLocation" value="classpath:config/mybatis-config.xml">property>
        <property name="dataSource" ref="pooledDataSource">property>
        
        <property name="mapperLocations" value="classpath:com/lims/mapper/*.xml">property>
    bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="basePackage" value="com.lims.dao">property>
    bean>
    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory">constructor-arg>
        <constructor-arg name="executorType" value="BATCH">constructor-arg>
    bean>
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
        <property name="dataSource" ref="pooledDataSource">property>
    bean>
    
    <aop:config>
        
        <aop:pointcut expression="execution(* com.lims.service..*(..))" id="txPoint"/>
        
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    aop:config>
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            
            <tx:method name="*"/>
            
            <tx:method name="get*" read-only="true"/>
        tx:attributes>
    tx:advice>        
    
    <bean id="" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
         <property name="locations">
            <list>
                <value>classpath:config/log4j.propertiesvalue>
            list>
         property>
    bean>
beans>

3.2 数据源配置(dbconfig.properties文件)

在config目录中新建dbconfig.properties文件,进行数据源的配置:

注意:这里的数据库名称、用户名以及密码都应该是你自己项目中具体使用的

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/1708_open-lims?useSSL=false&useUnicode=true&characterEncoding=utf-8
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=

3.3 日志管理的配置(log4j.properties文件)

在config目录中新建log4j.properties文件,对日志管理进行配置:

log4j.rootLogger=DEBUG,CONSOLE,FILEOUT 
log4j.addivity.org.apache=true

# CONSOLE  \u6253\u5370\u5230\u63A7\u5236\u53F0
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=ALL
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} \:%m%n

#
# FILEOUT \u6253\u5370\u5230 D\:\\report.log
log4j.appender.FILEOUT=org.apache.log4j.RollingFileAppender
log4j.appender.FILEOUT.File=D\:\\report.log
log4j.appender.FILEOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.MaxFileSize=100000KB

log4j.appender.FILEOUT.Append=true
#log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d \u2013 %c -%-4r [%t] %-5p %c %x \u2013 %m%n
log4j.appender.FILEOUT.layout.ConversionPattern=[%-5p]_%d{yyyy-MM-dd HH\:mm\:ss} \:%m%n

3.4 Mybatis的配置(mybatis-config.xml文件)

在config目录下新建mybatis-config.xml文件,对mybatis进行配置,内容如下:



<configuration>
    
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    settings>
    
    <typeAliases>
        <package name="com.lims.bean"/>
    typeAliases>  
configuration>

至此Spring以及对Mybatis的整合就结束了,接下来是对SpringMVC进行配置。


4、配置SpringMVC

在config目录下新建spring-mvc.xml文件,内容如下:
SSM(Spring+SpringMVC+Mybatis)框架整合配置方法_第3张图片


<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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
                        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
                        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-4.3.xsd">

    
    
    <context:component-scan base-package="com.lims.controller" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
        <property name="prefix" value="/WEB-INF/views/">property>
        <property name="suffix" value=".jsp">property>
    bean>  
    
    
    <mvc:default-servlet-handler/>
    
    <mvc:annotation-driven/>

beans>

这样,SSM三大框架的配置整合就完成了。

你可能感兴趣的:(系统开发)