SSM整合(配置文件)

Spring配置文件:applicationContext.xml

<pre style="background-color:#101020;color:#d0d0d0;font-family:'Consolas';font-size:15.0pt;"><?xml version="1.0" encoding="UTF-8"?>
<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">

<!-- 加载db.properties文件中的内容,dbconfig.properties文件中key命名要有一定的特殊规则 -->
  <context:property-placeholder location="classpath:dbconfig.properties"/>
    <!--配置连接池-->
 <!-- 数据源 org.apache.commons.dbcp.BasicDataSource com.alibaba.druid.pool.DruidDataSource -->  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!-- 初始化连接大小 -->
  <property name="initialSize" value="0" />
        <!-- 连接池最大使用连接数量 -->
  <property name="maxActive" value="30" />
        <!-- 连接池最小空闲 -->
  <property name="minIdle" value="0" />
        <!-- 获取连接最大等待时间 -->
  <property name="maxWait" value="60000" />
    </bean>

    <!--================== 配置和MyBatis的整合=============== -->
 <!-- sqlSessionFactory -->  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
  <property name="dataSource" ref="dataSource" />
        <!-- 加载mybatis的全局配置文件 -->
  <property name="configLocation" value="classpath:/mybatis-config.xml" />
        <!-- 指定mybatis,mapper文件的位置 -->
  <property name="mapperLocations" value="classpath:mappers/*.xml"></property>
        <!--对应实体类别名-->
  <property name="typeAliasesPackage" value="com.yuangh.recycle.bean"/>
    </bean>

    <!-- mapper扫描器 -->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
  <property name="basePackage" value="com.yuangh.recycle.mappers"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

    <!-- 配置一个可以执行批量的sqlSession -->
  <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="dataSource"></property>
    </bean>

    <!--开启基于注解的事务,使用xml配置形式的事务-->
  <aop:config>
        <!--切入点表达式-->
  <aop:pointcut id="txPoint" expression="execution(* com.yuangh.recycle.service.impl..*(..))"/>
        <!--配置事物增强-->
  <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>

    <!--配置事物增强,事物如何切入-->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 所有方法都是事务方法 -->
  <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

</beans></pre>

Mybatis 配置文件:mybatis-config.xml

<pre style="background-color:#101020;color:#d0d0d0;font-family:'Consolas';font-size:15.0pt;"><?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>
      <setting name="mapUnderscoreToCamelCase" value="false"/>

      <!--延迟加载(lazyLoadingEnabled):在用到时再加载
 按需加载(aggressiveLazyLoading)
  -->
  <setting name="lazyLoadingEnabled" value="true"/>
      <setting name="aggressiveLazyLoading" value="false"/>

      <!--开启二级缓存-->
  <setting name="cacheEnabled" value="true"/>
   </settings>

   <!-- 起别名:为实体类起一个别名,便于在使用实体类全类名的时候替代,防止冗余 -->
  <typeAliases>
      <package name="com.yuangh.recycle.bean"></package>
   </typeAliases>

   <!-- 将写好的sql映射文件配置到全局映射文件中
  -->
  <mappers></mappers>
</configuration></pre>

SpringMVC 配置文件:springmvc.xml

<pre style="background-color:#101020;color:#d0d0d0;font-family:'Consolas';font-size:15.0pt;"><beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!-- 对于注解的Handler 可以单个配置
 实际开发中加你使用组件扫描  -->
  <context:component-scan base-package="com.yuangh.recycle"></context:component-scan>

    <!-- 使用mvc:annotation-driven代替上面两个注解映射器和注解适配的配置
 mvc:annotation-driven默认加载很多的参数绑定方法,
 比如json转换解析器默认加载了,如果使用mvc:annotation-driven则不用配置上面的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
  实际开发时使用mvc:annotation-driven
 -->  <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"
  p:ignoreDefaultModelOnRedirect="true" >
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
            </list>
        </property>
    </bean>

    <!-- 校验器 -->
  <bean id="validator"
  class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <!-- hibernate校验器-->
  <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
        <!-- 指定校验使用的资源文件,在文件中配置校验错误信息,如果不指定则默认使用classpath下的ValidationMessages.properties -->
  <property name="validationMessageSource" ref="messageSource"/>
    </bean>
    <!-- 校验错误信息配置文件 -->
  <bean id="messageSource"
  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <!-- 资源文件编码格式 -->
  <property name="fileEncodings" value="utf-8"/>
        <!-- 对资源文件内容缓存时间,单位秒 -->
  <property name="cacheSeconds" value="120"/>
    </bean>

    <!-- 文件上传 -->
  <bean id="multipartResolver"
  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置上传文件的最大尺寸为5MB -->
  <property name="maxUploadSize">
            <value>5242880</value>
        </property>
    </bean>

    <!-- 配置处理器映射器和处理器适配器 -->
  <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>

    <!-- 自定义参数绑定 -->
  <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <!-- 转换器-->
  <property name="converters">
            <list>
                <!-- 日期格式转换-->
  <bean class="com.yuangh.recycle.converter.StringDateConverter"></bean>
            </list>
        </property>
    </bean>

    <!--拦截器 -->
  <mvc:interceptors>
        <!--多个拦截器,顺序执行 -->
 <!-- 登陆认证拦截器 -->
  <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.yuangh.recycle.interceptor.LoginInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

</beans></pre>

web配置文件:web.xml

<pre style="background-color:#101020;color:#d0d0d0;font-family:'Consolas';font-size:15.0pt;"><?xml version="1.0" encoding="utf-8" ?>
<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">
  <display-name>Archetype Created Web Application</display-name>

  <!--加载Spring容器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:*.xml</param-value>
  </context-param>

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

  <!--springmvc前端控制器-->
  <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:springmvc.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

  <!-- springmvc前端控制器,rest配置 -->
  <servlet>
    <servlet-name>springmvc_rest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等) 如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml) -->
  <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc_rest</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--乱码过滤器-->
  <filter>
    <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>fileupload</servlet-name>
    <servlet-class>com.yuangh.recycle.test.TestFileUpload</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>fileupload</servlet-name>
    <url-pattern>/fileup</url-pattern>
  </servlet-mapping>

</web-app>
</pre>
阅读更多

没有更多推荐了,返回首页