SSM三大框架整合配置(Spring+SpringMVC+MyBatis)

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">
  <!-- 定义Spring MVC的前端控制器 -->
  <servlet>
    <servlet-name>UserManage</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/config/UserManage-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- 让Spring MVC的前端控制器拦截所有请求 -->
  <servlet-mapping>
    <servlet-name>UserManage</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- ================Spring配置开始================ -->
  <!-- 设置Spring容器加载配置文件的路径 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/config/spring-config.xml</param-value>
    <!-- <param-value>classpath:/spring-*.xml</param-value> -->
  </context-param>

  <!-- 配置Spring监听器,可以在容器启动时,加载contextConfigLocation的context-param节点的配置文件 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- ================Spring配置结束================ -->

  <!-- 防止Spring内存溢出监听器 -->
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>

  <!-- 指定首页 -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!-- 配置SESSION超时,单位是分钟 -->
  <session-config>
    <session-timeout>10</session-timeout>
  </session-config>


  <!--如下此段编码设置必须在所有filter的前面,否则过滤器有可能不起作用。
      设置编码方式为UTF-8,解决中文乱码问题。-->
  <filter>
    <filter-name>CharacterEncoding</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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

UserManage-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:context="http://www.springframework.org/schema/context"
 6        xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 9         http://www.springframework.org/schema/mvc
10         http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
11         http://www.springframework.org/schema/context
12         http://www.springframework.org/schema/context/spring-context-4.2.xsd">
13 
14     <!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
15         如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean -->
16     <context:component-scan base-package="org.usermanage.controller"/>
17 
18     <!-- 配置处理映射器 -->
19     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
20 
21     <!-- 配置处理器适配器-->
22     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
23 
24     <!-- 视图解析器 -->
25     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
26         <!--指定默认前缀,controller中直接写文件名即可-->
27         <property name="prefix" value="/WEB-INF/content/jsp/"></property>
28     </bean>
29 
30     <!--指定单独处理的资源,不经过DispatcherServlet-->
31     <mvc:annotation-driven/>
32 
33     <!--所有以html结尾的请求全部到指定目录下查询-->
34     <mvc:resources mapping="/*.html" location="WEB-INF/content/html/"></mvc:resources>
35     <mvc:resources mapping="/*.js" location="WEB-INF/content/js/"></mvc:resources>
36     <mvc:resources mapping="/*.css" location="WEB-INF/content/css/"></mvc:resources>
37     <mvc:resources mapping="/login/*.css" location="WEB-INF/content/css/login/"></mvc:resources>
38 
39 
40 </beans>

spring-config.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.xsd">

    <!-- 加载配置jdbc属性文件 -->
    <!--MapperScannerConigurer实际是在解析加载bean定义阶段的,这个时候要是设置sqlSessionFactory的话,
        PropertyPlaceholderConfigurer还没来得及替换定义中的变量,导致把表达式当作字符串复制了
        修改如下:-->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>WEB-INF/config/jdbc.properties</value>
                <!--要是有多个配置文件,只需在这里继续添加即可 -->
            </list>
        </property>
    </bean>

    <!--配置c3p0连接池-->
    <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="${username}"></property>
        <property name="password" value="${password}"></property>
        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>

    <!--SqlSessionFactoryBean配置-->
    <bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--Mybatis配置文件,非必须-->
        <property name="configLocation" value="WEB-INF/config/mybatis-config.xml"/>
        <property name="dataSource" ref="dataSource"/>
        <!--扫描mapper.xml文件的位置-->
        <property name="mapperLocations" value="classpath:org/usermanage/mapper/*.xml"/>
    </bean>

    <!-- 配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory,单个dataSource时可省略 -->
        <!--<property name="sqlSessionFactoryBeanName" value="sqlsessionFactory"/>-->
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="org.usermanage.mapper"/>
    </bean>

    <!--扫描所有使用注解的类型,使用Annotation自动注册Bean-->
    <context:component-scan base-package="org.usermanage"/>

    <!-- 事务配置 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 使用annotation注解方式配置事务 -->
    <!--<tx:annotation-driven transaction-manager="transactionManager"/>-->

</beans>

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>
        <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
        <setting name="useGeneratedKeys" value="true"/>

        <!-- 使用列别名替换列名 默认:true -->
        <setting name="useColumnLabel" value="true"/>

        <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <!--从外部配置文件导入jdbc信息-->
    <!--<properties resource="config/jdbc.properties"></properties>-->

    <!--<environments default="development">-->
    <!--<environment id="development">-->
    <!--<transactionManager type="JDBC"/>-->
    <!--<dataSource type="POOLED">-->
    <!--<property name="driver" value="${driver}"/>-->
    <!--<property name="url" value="${url}"/>-->
    <!--<property name="username" value="${username}"/>-->
    <!--<property name="password" value="${password}"/>-->
    <!--</dataSource>-->
    <!--</environment>-->
    <!--</environments>-->

    <!--指定映射资源文件-->
    <!--<mappers>-->
    <!--<mapper resource="classpath:org/usermanage/mapper/UserMapper.xml"/>-->
    <!--</mappers>-->

</configuration>

jdbc.properties

# jdbc连接信息
driver=com.mysql.jdbc.Driver
#url=jdbc:mysql://192.168.184.130:3306/gxrdb
url=jdbc:mysql://10.15.1.200:3306/gxrdb?useUnicode=true&characterEncoding=utf8
username=root
password=root

 

posted @ 2018-02-26 19:13  星瑞  阅读(219)  评论(0编辑  收藏  举报