spring 基础配置

1、mybatis层


<?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="false" />


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


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

<plugins>    
        <!-- com.github.pagehelper为PageHelper类所在包名 -->    
        <plugin interceptor="com.github.pagehelper.PageHelper">    
            <!-- 4.0.0以后版本可以不设置该参数 -->    
            <property name="dialect" value="mysql"/>    
            <!-- 该参数默认为false -->    
            <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->    
            <!-- 和startPage中的pageNum效果一样-->    
            <property name="offsetAsPageNum" value="true"/>    
            <!-- 该参数默认为false -->    
            <!-- 设置为true时,使用RowBounds分页会进行count查询 -->    
            <property name="rowBoundsWithCount" value="true"/>    
            <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->    
            <!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->    
            <property name="pageSizeZero" value="true"/>    
            <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->    
            <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->    
            <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->    
            <property name="reasonable" value="true"/>    
            <!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->    
            <!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->    
            <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值 -->    
            <!-- 不理解该含义的前提下,不要随便复制该配置 -->    
            <!-- <property name="params" value="pageNum=start;pageSize=limit;"/> -->    
            <!-- 支持通过Mapper接口参数来传递分页参数 -->    
            <property name="supportMethodsArguments" value="true"/>    
            <!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->    
            <property name="returnPageInfo" value="check"/>    
        </plugin>    
    </plugins>
</configuration>


2、dao层

<?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"
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">
<!-- 配置整合mybatis过程 -->
<!-- 1.配置数据库相关参数properties的属性:${url} -->
<!--<context:property-placeholder location="classpath:profiles/dev/jdbc.properties" />-->
    <context:property-placeholder location="classpath:jdbc.properties" />


<!-- 2.数据库连接池 -->
<bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver1}" />
<property name="jdbcUrl" value="${jdbc.url1}" />
<property name="user" value="${jdbc.username1}" />
<property name="password" value="${jdbc.password1}" />


<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<!--最大空闲时间,20秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->  
<property name="maxIdleTime" value="20"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false" />
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000" />
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2" />
</bean>

<!-- 2.数据库连接池 2-->
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver2}" />
<property name="jdbcUrl" value="${jdbc.url2}" />
<property name="user" value="${jdbc.username2}" />
<property name="password" value="${jdbc.password2}" />


<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<!--最大空闲时间,20秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->  
<property name="maxIdleTime" value="20"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false" />
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000" />
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2" />
</bean>

<bean id="multipleDataSource" class="com.jw.psp.api.dsource.MultipleDataSource">
        <property name="defaultTargetDataSource" ref="dataSource2"/>
        <property name="targetDataSources">
            <map>
                <entry key="dataSource1" value-ref="dataSource1"/>
                <entry key="dataSource2" value-ref="dataSource2"/>
            </map>
        </property>
    </bean>


<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="multipleDataSource" />
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<!-- <property name="configLocation" value="classpath:profiles/dev/mybatis-config.xml" /> -->
<property name="configLocation" value="classpath:mybatis-config.xml" /> 
<!-- 扫描entity包 使用别名 -->
<property name="typeAliasesPackage" value="com.jw.psp.api.po" />
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:com/jw/psp/api/mapper/*.xml" />
</bean>


<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.jw.psp.api.dao" />
</bean>
</beans>


3、service层


<?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:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描service包下所有使用注解的类型 -->
<context:component-scan base-package="com.jw.psp.api.service" />


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

<!-- 配置基于注解的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" /> 

</beans>


4、task层

<?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:task="http://www.springframework.org/schema/task"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/task
       http://www.springframework.org/schema/task/spring-task-3.2.xsd
       http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">  
    
      
    <context:component-scan base-package="com.jw.psp.api.task"></context:component-scan> 
    <!-- task任务扫描注解 -->  
<task:annotation-driven/>
</beans>  


5、web层

<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
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/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-4.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">


<!-- 解决返回数据中文乱码问题 -->
  <bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" index="0" />
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>  


<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解模式 -->
<!-- 简化配置: (1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter 
(2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持 -->
<!--  <mvc:annotation-driven />    -->
<mvc:annotation-driven>
      <mvc:message-converters>
        <ref bean="stringHttpMessageConverter"/>
    </mvc:message-converters>
    </mvc:annotation-driven> 
 
<!-- 2.静态资源默认servlet配置 (1)加入对静态资源的处理:js,gif,png (2)允许使用"/"做整体映射 -->
<!-- <mvc:default-servlet-handler/> -->
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/img/" mapping="/img/**" />
<mvc:resources location="/libs/" mapping="/libs/**" />
<mvc:resources location="/pages/" mapping="/pages/**" />




<!-- 4.扫描action相关的bean -->
<context:component-scan base-package="com.jw.psp.api.action" />
<!-- accesstoken -->


<!-- 5.扫描exception -->
<context:component-scan base-package="com.jw.psp.api.exception" />

<context:component-scan base-package="com.jw.psp.api.interceptor" />



<!-- 7.配置数据库注解aop -->
<bean id="dataSourceAspect" class="com.jw.psp.api.dsource.DataSourceAspect" />
  <aop:config>
<aop:aspect ref="dataSourceAspect">
<!-- 拦截所有service方法 -->
<aop:pointcut id="dataSourcePointcut" expression="execution(* com.jw.psp.api.dao.*.*(..))" />
<aop:before pointcut-ref="dataSourcePointcut" method="intercept" />
</aop:aspect>
</aop:config>

<!-- 8.拦截器 -->
  <mvc:interceptors>  
    <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求   -->
    <bean class="com.jw.psp.api.interceptor.PermissionInterceptor"/>  
    <mvc:interceptor>  
        <mvc:mapping path="/ja/*"/>  
        <!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的   -->
        <bean class="com.jw.psp.api.interceptor.PermissionInterceptor"/>  
    </mvc:interceptor>  
    </mvc:interceptors>    
 
</beans>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值