码迷,mamicode.com
首页 > 编程语言 > 详细

spring+springmvc+mybatis配置文件

时间:2019-07-12 22:31:07      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:sse   style   cep   scanner   一起   put   int   action   clear   

最近在学习SSM,自己上手了一个crm项目,这两天对底层配置文件做了个总结。

sqlmapconfig.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>
    
    <!-- 全局setting配置,根据需要添加 -->
    
    <!-- 配置别名 -->
    <typeAliases>
        <!-- 批量扫描别名,指定pojo类的位置,该配置中暂时用不到 -->
        <package name="cn.itcast.ssm.po"/>
    </typeAliases>

    <!-- 配置mapper
    由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。
    必须遵循:mapper.xml和mapper.java文件同名且在一个目录 
     -->

    <!-- <mappers>
    
    </mappers> -->
</configuration>

applicationContext.xml

把spring-mybatis.xml文件和spring-service.xml文件整合在一起

<?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:mvc="http://www.springframework.org/schema/mvc" 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/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd ">
    <!-- mybatis datasource 配置 -->
    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close" init-method="init">
        <property name="defaultAutoCommit" value="true" />
        <!-- 按照数据库分配4GB设置,如Oracle内存设置4GB -->
        <!-- maxActive范围(200-5000),参数需调高数据库内存,最大连接数 -->
        <property name="maxActive" value="500" />
        <property name="initialSize" value="5" />
        <property name="maxWait" value="240000" />
        <property name="minIdle" value="5" />
        <property name="timeBetweenEvictionRunsMillis" value="3000" />
        <property name="timeBetweenLogStatsMillis" value="0" />
        <property name="minEvictableIdleTimeMillis" value="300000" />
        <property name="validationQuery" value="SELECT 1" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <!-- poolPreparedStatements在mysql下建议关闭 -->
        <property name="poolPreparedStatements" value="false" />
        <property name="maxPoolPreparedStatementPerConnectionSize"
            value="100" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="60" />
        <property name="logAbandoned" value="false" />
        <property name="connectionProperties"
            value="druid.stat.slowSqlMillis=50;druid.stat.loggerName=log4j;" />
        <property name="driverClassName" value="${dbdriver}" />
        <property name="url" value="${dburl}" />
        <property name="username" value="${dbuser}" />
        <property name="password" value="${dbpwd}" />
        <!-- filter:(stat监控统计,mergeStat监控统计并合并,log4j日志,slf4j日志,wall防御sql注入,encoding编码记录) -->
        <!-- <property name="filters" value="mergeStat,log4j" /> -->
        <property name="proxyFilters">
            <list>
                <ref bean="logfilter" />
                <ref bean="statFilter" />
                <!--SQL防火墙,在阿里云上需要开启 -->
                <ref bean="wallFilter" />
            </list>
        </property>
        <!-- spring下不可用 -->
        <!-- <property name="statementExecutableSqlLogEnable" value="true" /> -->
    </bean>

    <bean id="statFilter" class="com.alibaba.druid.filter.stat.StatFilter">
        <property name="mergeSql" value="true" />
    </bean>

    <bean id="wallFilter" class="com.alibaba.druid.wall.WallFilter">
        <property name="dbType" value="sqlserver" />
        <property name="logViolation" value="true" />
        <property name="throwException" value="true" />
        <property name="config" ref="wall-filter-config" />
    </bean>
    <!-- 指定配置装载的目录 https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE-wallfilter -->
    <bean id="wall-filter-config" class="com.alibaba.druid.wall.WallConfig"
        init-method="init">
        <property name="dir" value="classpath:config/druid/wall/sqlserver" />
        <property name="multiStatementAllow" value="true"/>
        <property name="noneBaseStatementAllow" value="false"/>
        <property name="commentAllow" value="true"/>
        <property name="mustParameterized" value="false"/>
        <property name="strictSyntaxCheck" value="true"/>
    </bean>

    <bean id="logfilter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter">
        <property name="connectionConnectBeforeLogEnabled" value="false"></property>
        <property name="connectionConnectAfterLogEnabled" value="false"></property>
        <property name="connectionCommitAfterLogEnabled" value="false"></property>
        <property name="connectionRollbackAfterLogEnabled" value="false"></property>
        <property name="connectionCloseAfterLogEnabled" value="false"></property>

        <property name="statementCreateAfterLogEnabled" value="false"></property>
        <property name="statementPrepareAfterLogEnabled" value="false"></property>
        <property name="statementPrepareCallAfterLogEnabled" value="false"></property>

        <property name="statementExecuteAfterLogEnabled" value="false"></property>
        <property name="statementExecuteQueryAfterLogEnabled" value="false"></property>
        <property name="statementExecuteUpdateAfterLogEnabled" value="false"></property>
        <property name="statementExecuteBatchAfterLogEnabled" value="false"></property>
        <property name="statementExecutableSqlLogEnable" value="false"></property>

        <property name="statementCloseAfterLogEnabled" value="false"></property>
        <property name="statementParameterSetLogEnabled" value="false"></property>
        <property name="statementParameterClearLogEnable" value="false"></property>

        <property name="resultSetNextAfterLogEnabled" value="false"></property>
        <property name="resultSetOpenAfterLogEnabled" value="false"></property>
        <property name="resultSetCloseAfterLogEnabled" value="false"></property>

        <property name="resultSetLogEnabled" value="false"></property>
        <property name="resultSetLogErrorEnabled" value="false"></property>

        <property name="dataSourceLogEnabled" value="true"></property>
        <property name="connectionLogEnabled" value="true"></property>
        <property name="connectionLogErrorEnabled" value="true"></property>
        <property name="statementLogEnabled" value="true"></property>
        <property name="statementLogErrorEnabled" value="true"></property>
    </bean>

    <!--sessionFactory配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="druidDataSource" />
        <!-- mybatis配置文件路径 -->
        <property name="configLocation"
            value="classpath:cn/com/dl/core/dao/sqlmapconfig.xml" />
        <!-- mapper配置文件路径,如果配置文件和接口文件位置不一致,则需配置 -->
        <property name="mapperLocations" value="classpath:cn/com/dl/core/dao/**/mapper/*Mapper.xml" />
    </bean>
    <!-- jdbcTemplate DataSource -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="druidDataSource" />
    </bean>

    <!--扫描myBatis接口,接口直接与xml文件进行映射 -->
    <!--basePackage指定要扫描的包,可指定多个包,包与包之间用逗号或分号分隔 -->
    <!-- spring 注解mapper使用,对于同一dao对象只能有一个扫描对象, -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.com.dl.core.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

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

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 为连接点指定事务属性 -->
            <tx:method name="*insert*" isolation="DEFAULT" propagation="REQUIRED" />
            <tx:method name="*update*" isolation="DEFAULT" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <!-- 切入点配置 -->
        <aop:pointcut expression="execution(* cn.com.dl.core.service.*.*.*(..))" id="point" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="point" />
    </aop:config>
</beans>
 

springmvc.xml(spring-servlet.xml)

<?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:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-4.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
    
  
    <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
    
    <context:component-scan base-package="cn.com.dl.web.controller" />
    <mvc:annotation-driven/>

    <!-- json格式转换-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="textJsonHttpMessageConverter" />
            </list>
        </property>
    </bean>
    
    <bean id="textJsonHttpMessageConverter"
        class="cn.com.dl.external.converter.json.MappingJackson2HttpMessageConverter">
        <property name="objectMapper">
            <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                <property name="dateFormat">
                    <bean class="java.text.SimpleDateFormat">
                        <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
                    </bean>
                </property>
                <!--对属性值为null的不序列化反序列化-->
                <property name="serializationInclusion">
                    <util:constant static-field="com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL"/>
                </property>
            </bean>
        </property>
        <property name="supportedMediaTypes">
            <list>
                <value>text/json</value>  <!-- 对应前台ajax的contentType:"text/json",和headers: {"Accept":"text/json"} -->
            </list>
        </property>
    </bean>

    <!-- 对模型视图名称的解析,在WEB-INF/jsp目录下找对应的jsp文件 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/" p:suffix=".jsp" />

    <mvc:resources mapping="/template/**" location="/template/" />
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/styles/**" location="/styles/" />
    <mvc:resources mapping="/image/**" location="/image/" />
    
</beans> 

web.xml

<?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" 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_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>springmvc_mybatis</display-name>

<!-- 加载spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/spring/applicationContext.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>
        <!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等) 如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml) -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- 第一种:*.action,访问以.action结尾 由DispatcherServlet进行解析 
            第二种:/,所以访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析 
            使用此种方式可以实现 RESTful风格的url 
            第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时, 仍然会由DispatcherServlet解析jsp地址,不能根据jsp页面找到handler,会报错。 -->
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

log4j.properties文件

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.ma.core=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 db.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=

spring+springmvc+mybatis配置文件

标签:sse   style   cep   scanner   一起   put   int   action   clear   

原文地址:https://www.cnblogs.com/SI0301/p/11178711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!