SpringMVC+mybatis的配置(依赖maven)

SpringMVC+mybatis的配置(依赖maven)

pom.xml配置:

<dependencies>

            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>1.6.1version>
        dependency> 
            <dependency>
        <groupId>org.slf4jgroupId>  
        <artifactId>slf4j-log4j12artifactId>  
        <version>1.7.2version>    

        <dependency> 
            <groupId>org.slf4jgroupId>
            <artifactId>log4j-over-slf4jartifactId>
            <version>1.6.1version>
        dependency> 

        <dependency>
            <groupId>org.apache.httpcomponentsgroupId>
            <artifactId>httpclientartifactId>
            <version>4.3.3version>
        dependency>
        <dependency>
            <groupId>org.apache.httpcomponentsgroupId>
            <artifactId>httpmimeartifactId>
            <version>4.3.3version>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
            <version>3.3.2version>
        dependency>
        <dependency>
            <groupId>commons-iogroupId>
            <artifactId>commons-ioartifactId>
            <version>2.4version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.7version>
            <scope>testscope>
        dependency> 
        <dependency> 
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>4.1.6.RELEASEversion> 
        dependency>    
        <dependency>
            <groupId>commons-codecgroupId>  
            <artifactId>commons-codecartifactId>
            <version>1.8version> 
        dependency>   
            <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>3.0-alpha-1version>
        dependency>
        <dependency>
            <groupId>commons-logginggroupId>
            <artifactId>commons-loggingartifactId>
            <version>1.1version>
        dependency>
        <dependency>
            <groupId>net.sf.json-libgroupId>
            <artifactId>json-libartifactId>
            <classifier>jdk15classifier>
            <version>2.3version>
        dependency>
        <dependency>
            <groupId>org.codehaus.jacksongroupId>
            <artifactId>jackson-core-aslartifactId>
            <version>1.9.13version>
        dependency>
        <dependency>
            <groupId>org.codehaus.jacksongroupId>
            <artifactId>jackson-mapper-aslartifactId>
            <version>1.9.13version>
        dependency>
            <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>4.1.6.RELEASEversion>
        dependency>
        <dependency>
            <groupId>jstlgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.3.1version>
        dependency>
            <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>4.1.6.RELEASEversion>
        dependency> 
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.6.9version> 
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>0.2.9version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.2.3version>
        dependency>
            <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.25version>
        dependency>
        <dependency>
            <groupId>cglibgroupId>
            <artifactId>cglibartifactId>
            <version>2.2.2version>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poiartifactId>
            <version>3.12version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.6version>
        dependency>  
        <dependency>
            <groupId>commons-fileuploadgroupId>
            <artifactId>commons-fileuploadartifactId>
            <version>1.3version>
        dependency>
        <dependency>
            <groupId>org.quartz-schedulergroupId>
            <artifactId>quartzartifactId>
            <version>2.2.1version>
        dependency>
        <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>4.1.6.RELEASEversion>
    dependency>
 <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-coreartifactId>
    <version>4.1.6.RELEASEversion>
dependency>
        <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jdbcartifactId>
    <version>4.1.6.RELEASEversion>
dependency>
  dependencies>

web.xml配置:


<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>
           classpath:applicationContext.xml;
           classpath:spring-mybatis.xml;
           classpath:applicationContext-servlet.xml;
       param-value>
    context-param>

<servlet-mapping>  
    <servlet-name>defaultservlet-name> 
    <url-pattern>*.htmlurl-pattern>    
servlet-mapping>   
<servlet-mapping>      
    <servlet-name>defaultservlet-name>   
    <url-pattern>/js/*url-pattern>   
servlet-mapping>   
<servlet-mapping>       
    <servlet-name>defaultservlet-name>      
    <url-pattern>/css/*url-pattern>     
servlet-mapping>
<servlet-mapping>       
    <servlet-name>defaultservlet-name>      
    <url-pattern>/headpic/*url-pattern>     
servlet-mapping>
<servlet-mapping>       
    <servlet-name>defaultservlet-name>      
    <url-pattern>/image/*url-pattern>     
servlet-mapping>

    <servlet>
        <servlet-name>mvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:applicationContext-servlet.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>mvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>

    
        <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>forceEncodingparam-name>  
                <param-value>trueparam-value>  
            init-param>  
        filter>  
        <filter-mapping>  
            <filter-name>characterEncodingFilterfilter-name>  
            <url-pattern>/*url-pattern>  
        filter-mapping>  
web-app>

配置applicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="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.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
        
    <context:component-scan base-package="com.qcit.service*">context:component-scan>
    
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:imageconfig.propertiesvalue>
                <value>classpath:db.propertiesvalue>
                <value>classpath:upload.propertiesvalue>
            list>
        property>
        
    bean> 
    <bean id="imageCodeUtil" class="com.qcit.util.ImageCodeUtil" name="imageCodeUtil" />
    <bean id="fileUtil" class="com.qcit.util.FileUtil" name="fileUtil"/>
beans>

配置applicationContext-servlet.xml


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
        
    <context:component-scan base-package="com.qcit.controller*">context:component-scan>
    
    <mvc:default-servlet-handler/>
    
    <mvc:annotation-driven> 
    <mvc:message-converters register-defaults="false">
    <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
    <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/>
    <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
    <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
    
    <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
    <property name="supportedMediaTypes">
    <list>
    <value>text/html;charset=UTF-8value>
    <value>text/plain;charset=UTF-8value>
    <value>application/json;charset=UTF-8value>
     <value>application/xml;charset=UTF-8value>
    list>
    property> 
    bean>
    mvc:message-converters>
    mvc:annotation-driven>


    
    <bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="104857600" />
        <property name="maxInMemorySize" value="4096" />
    bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/">property>
        <property name="suffix" value=".jsp">property>
    bean>


    
    <mvc:resources location="/js" mapping="/**/*.js" />
    <mvc:resources location="/css/" mapping="/**/*.css" />
    <mvc:resources location="/image/" mapping="/images/*"
        cache-period="360000" />
    
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="com.qcit.interceptor.LoginInterceptor">
            
                <property name="list">

                    <list>
                        <value>loginvalue>
                        <value>getcodevalue>
                        <value>registervalue>
                        <value>htmlvalue>
                        <value>jsvalue>
                        <value>imagevalue>
                        <value>cssvalue>
                    list>

                property>

            bean>
        mvc:interceptor>

    mvc:interceptors>



beans>

配置spring-mybatis.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">


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

    
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <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="50" />
        
        
        
        <property name="minIdle" value="0" />
        
        <property name="maxWait" value="60000" />

        

        <property name="validationQuery" value="${validationQuery}" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />

        
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        
        <property name="minEvictableIdleTimeMillis" value="25200000" />

        
        <property name="removeAbandoned" value="true" />
        
        <property name="removeAbandonedTimeout" value="1800" />
        
        <property name="logAbandoned" value="true" />

        
        
        <property name="filters" value="mergeStat" />
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        
        <property name="configLocation" value="classpath:myBatisConfig.xml">property>
        
        <property name="mapperLocations" value="classpath:com/qcit/mapping/*.xml" />
    bean>


    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.qcit.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    bean>

     
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    bean>

    
    <tx:annotation-driven transaction-manager="transactionManager" />

    
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="append*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" propagation="REQUIRED" />
            <tx:method name="delAndRepair" propagation="REQUIRED" />
            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
            <tx:method name="datagrid*" propagation="SUPPORTS" />

            <tx:method name="*" propagation="SUPPORTS" />
        tx:attributes>
    tx:advice>

    <aop:config> 
    <aop:pointcut id="transactionPointcut" expression="execution(* com.qcit.service..*Impl.*(..))" /> 
    <aop:advisor pointcut-ref="transactionPointcut" 
    advice-ref="transactionAdvice" />
     aop:config> 
    
    <bean id="druid-stat-interceptor"
        class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
    bean>
    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
        scope="prototype">
        <property name="patterns">
            <list>
                <value>com.qcit.service.*value>
            list>
        property>
    bean>
    <aop:config>
        <aop:advisor advice-ref="druid-stat-interceptor"
            pointcut-ref="druid-stat-pointcut" />
    aop:config>
beans>

配置myBatisConfig.xml



<configuration>
    <typeAliases>
        <typeAlias type="com.qcit.model.User" alias="user" />
        <typeAlias type="com.qcit.model.ItemsListVo" alias="itemslistvo" />
        <typeAlias type="com.qcit.model.ItemsPo" alias="itemspo" />
        <typeAlias type="com.qcit.model.UserLoginSimple" alias="userloginsimple" />
        <typeAlias type="com.qcit.model.ProblemCount" alias="problemcount" />
    typeAliases>

configuration>    

db.properties

validationQuery=SELECT 1
jdbc_url=jdbc\:mysql\://127.0.0.1\:3306/bugmanage?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull&allowMultiQueries\=true 
jdbc_username=root
jdbc_password=123456

log4j.properties

log4j.rootCategory=DEBUG, stdout  

log4j.appender.stdout=org.apache.log4j.ConsoleAppender   
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout   
log4j.appender.stdout.layout.ConversionPattern=[QCIT] %p [%t] %C.%M(%L) | %m%n   


XXXmapping.xml



<mapper namespace="com.qcit.dao.ItemsDao">
mapper>

你可能感兴趣的:(学习总结)