Spring、Spring MVC、Mybatis开发环境搭建

1.引入第三方Jar包或者用Maven的pom添加依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.candle</groupId>
    <artifactId>home-finance-sample</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>home-finance-sample Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <build>
        <finalName>home-finance-sample</finalName>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <!-- spring jar -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>

        <!-- spring mvc jar -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>

        <!-- mybatis jar -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.4</version>
        </dependency>

        <!-- mybatis-spring jar -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>

        <!-- mysql-connector-java jar -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.28</version>
        </dependency>
        <!-- 数据库连接池 jar -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>1.6</version>
        </dependency>

        <!-- log jar -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>

        <!-- jsp Template jar -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.20</version>
        </dependency>

        <!-- apache servlet api jar -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>servlet-api</artifactId>
            <version>6.0.37</version>
        </dependency>

    </dependencies>


</project>

2.配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


    <!-- 配置session超时时间,单位分钟 -->
    <session-config>
        <session-timeout>-1</session-timeout>
    </session-config>


    <!--  字符集 过滤器  -->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 加载spring文件  -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:/config/spring/spring-common.xml
        </param-value>
    </context-param>

    <!-- ContextLoaderListener启动容器,在启动Web容器时,自动装配Spring applicationContext.xml的配置信息 -->
    <listener>
        <listener-class>        org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <!-- 配置监听器 根据需要选配-->
    <listener>
        <listener-class>
            com.sz.mt.listener.LoginListenner
        </listener-class>
    </listener>

    <!-- SpringMVC 配置,配置文件名称默认为{servlet-name}-servlet.xml,路径默认在/WEB-INF/下-->
    <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:/config/spring/springmvc-servlet.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--  
    <servlet>  
        <servlet-name>log4j</servlet-name>  
        <servlet-class>com.sz.mt.listener.Log4jInitServlet</servlet-class>  
        <init-param>  
            <param-name>configfile</param-name>  
            <param-value>/WEB-INF/log4j.properties</param-value>  
        </init-param>  

        <load-on-startup>1</load-on-startup>  
    </servlet>  
    -->

<!--  主要作用是 如果bean想使用作用域为reuqest的时候需要必须配置的-->
    <listener>
        <listener-class>        org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>
    <!-- 此监听器它的主要作用是负责处理因JavaBean Inteceptor的使用而引起的缓存泄露问题 -->   
    <listener>
        <listener-class>
org.springframework.web.util.IntrospectorCleanupListener
        </listener-class>
    </listener>
    <!-- 把监听启动配置到web中-->
        <listener-class>com.sz.mt.listener.ServersInit</listener-class>
    </listener>

    <!--配置错误页面 -->
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/views/common/timeout.jsp</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/views/common/404.jsp</location>
    </error-page>
    <error-page>
        <error-code>403</error-code>
        <location>/WEB-INF/views/common/timeout.jsp</location>
    </error-page>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

3.spring-common.xml(Spring配置),此处省略config.properties配置的数据库连接的内容

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:task="http://www.springframework.org/schema/task"  
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-3.0.xsd   
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    >
     <!--配置缓存-->
     <import resource="classpath:/config/ehcache/applicationContext-ehcache.xml"/>  

     <mvc:annotation-driven />  

     <task:annotation-driven/>  

     <aop:aspectj-autoproxy/>  

     <!-- enable autowire -->
    <context:annotation-config />

    <!-- 添加注解驱动 -->
    <mvc:annotation-driven />

    <!-- 自动搜索@Component , @Service , @Repository等标注的类 不搜索@Controller的类 -->
    <!-- 自动扫描组件,这里要把web下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。 -->
    <context:component-scan base-package="com.sz.mt" annotation-config="true">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>



    <!-- 引入属性文件 ——jdbc配置文件  -->
    <context:property-placeholder location="classpath:/config/ibatis/jdbc.properties" />

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

    <!--数据库连接 数据源的方式直接连接——JNDI方式
    <bean id="dataSource"
        class="org.springframework.jndi.JndiObjectFactoryBean">
         <property name="jndiName" value="java:comp/env/mt" /> 
    </bean>
        --> 

     <!--创建jdbc数据源,直连的方式-->     
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">  
         <property name="url" value="${jdbc.url}" />  
         <property name="username" value="${jdbc.username}" />  
         <property name="password" value="${jdbc.password}" />  
         <property name="driverClassName" value="${jdbc.driverClassName}" />  
         <property name="filters" value="${jdbc.filters}" />  
        <!-- 配置初始化大小、最小、最大 -->
      <property name="initialSize" value="8" />
      <property name="minIdle" value="8" /> 
      <property name="maxActive" value="100" />

      <!-- 配置获取连接等待超时的时间 -->
      <property name="maxWait" value="60000" />

      <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
      <property name="timeBetweenEvictionRunsMillis" value="60000" />

      <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
      <property name="minEvictableIdleTimeMillis" value="300000" />

      <property name="validationQuery" value="SELECT 'x'" />
      <property name="testWhileIdle" value="true" />
      <property name="testOnBorrow" value="false" />
      <property name="testOnReturn" value="false" />

      <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
      <property name="poolPreparedStatements" value="true" />
      <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
    </bean> 

     <!-- 创建SqlSessionFactory,并指定数据源 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:/config/ibatis/mybatis-config.xml" />
        <property name="mapperLocations" value="classpath*:com/sz/mt/dao/*Mapper.xml" />
    </bean>

    <!--  Mapper文件扫描配置工具,Spring将自动扫描对应配置路径下的Mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.sz.mt.dao" />
    </bean>

 <!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring bean对象 -->
    <bean class="com.sz.mt.common.springmvc.SpringContextHolder" lazy-init="false" />

    <!-- 数据连接事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

     <!--连接事务的注解配置 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

   <!-- <import resource="spring-jedis.xml" /> -->
</beans>

4.spring-servlet.xml文件配置(SpringMVC)

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

    <aop:aspectj-autoproxy proxy-target-class="true" />

    <!-- 激活 @Required @Autowired,JSR 250's @PostConstruct, @PreDestroy and @Resource 等标注 -->
    <context:annotation-config />
    <!-- 只搜索@Controller 标注的类 不搜索其他标注的类 -->
    <context:component-scan base-package="com.sz.mt"
        use-default-filters="false">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!-- controller层的属性和配置文件读入 ,多个用逗号隔开 <context:property-placeholder location="classpath:/config/others/config.properties" /> -->

    <!-- 应用属性文件读入 -->
    <bean id="applicationProperties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <!-- 标准配置 -->
                <value>classpath:/config/ibatis/jdbc.properties</value>
            </list>
        </property>
    </bean>

    <!-- 用于持有applicationProperties,将properties转变为静态方法使用,PropertiesHolder.getProperty("somekey") --><!--
        <bean class="cn.gov.hnjt.gs.common.utils.PropertiesHolder">
        <property name="properties" ref="applicationProperties" />
        </bean>
        <bean id="springContextHolder" class="com.sz.mt.common.springmvc.SpringContextHolder" />  

    --><!-- PropertyPlaceholderConfigurer,用于spring ${placeholder}的解析 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName"
            value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="properties" ref="applicationProperties" />
    </bean>



    <!-- 对某些静态资源,如css,图片等进行过滤 ,有引用 "/resources/**" 的路径引用转到工程的/resources/目录取资源 -->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:resources mapping="/upload/**" location="/upload/" />
    <mvc:resources mapping="/pdf/**" location="/pdf/" />
    <mvc:annotation-driven />

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

    <!-- 针对类、方法级别的权限拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <!-- <mvc:mapping path="/fileupload" /> -->
            <mvc:mapping path="/main*" />
            <mvc:mapping path="/main/**" />
            <mvc:mapping path="/user*" />
            <mvc:mapping path="/user/**" />     
            <bean
                class="com.sz.mt.common.interseptor.LoginInterceptor">
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>

    <!--开发期间注释掉,上线后启用 错误解析器 发生错误默认会跳转到/web-inf/views/timeout.jsp -->
    <!-- <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" 
        value="timeout" /> <property name="exceptionMappings"> <props> <prop key="java.sql.SQLException">timeout</prop> <prop key="java.lang.RuntimeException">timeout</prop> 
        <prop key="org.springframework.transaction.TransactionException">timeout</prop> <prop key="org.springframework.dao.DataAccessException">timeout</prop> 
        </props> </property> </bean> -->

    <!-- controller异常统一处理 -->
    <bean
        class="com.sz.mt.common.interseptor.ControllerJsonExceptionHandler">
    </bean>

 <!-- ===================================================== -->
    <!-- ViewResolver For FreeMarker -->
    <!-- ===================================================== -->
    <bean id="freemarkerResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="order" value="1" />
        <property name="suffix" value=".ftl" />
        <property name="contentType" value="text/html;charset=utf-8" />
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.freemarker.FreeMarkerView</value>
        </property>
    </bean>
    <!-- ===================================================== -->
    <!-- ViewResolver For FreeMarkerConfigurer -->
    <!-- ===================================================== -->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath">
            <value>/views/</value>
        </property>
        <property name="freemarkerSettings"><!-- 设置FreeMarker环境属性 -->
            <props>
                <prop key="template_update_delay">5</prop><!--刷新模板的周期,单位为秒 -->
                <prop key="default_encoding">UTF-8</prop><!--模板的编码格式 -->
                <prop key="locale">UTF-8</prop><!-- 本地化设置 -->
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="time_format">HH:mm:ss</prop>
                <prop key="number_format">0.####</prop>
                <prop key="boolean_format">true,false</prop>
                <prop key="whitespace_stripping">true</prop>
                <prop key="tag_syntax">auto_detect</prop>
                <prop key="url_escaping_charset">UTF-8</prop>
            </props>
        </property>
    </bean>
</beans>

5.mybatis-config.xml文件(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>
    <mappers>
        <mapper resource="config/ibatis/common_SqlMap.xml" />
    </mappers>
</configuration>

6.common_SqlMap.xml文件配置分页

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="common" >
  <sql id="Oracle_Pagination_Head" >
    <if test="oracleStart != null and oracleEnd != null" >
      <![CDATA[ select y.* from ( select z.*, rownum as oracleStart from ( ]]>
    </if>
  </sql>
  <sql id="Oracle_Pagination_Tail" >
    <if test="oracleStart != null and oracleEnd != null" >
      <![CDATA[ ) z where rownum <= #{oracleEnd} ) y where y.oracleStart > #{oracleStart} ]]>
    </if>
  </sql>
</mapper>

7.jdbc.properties文件配置

#druid Connection
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.username=user
jdbc.password=password

jdbc.filters=stat 
jdbc.maxActive=20
jdbc.initialSize=1
jdbc.maxWait=60000
jdbc.minIdle=10
jdbc.maxIdle=15

jdbc.timeBetweenEvictionRunsMillis=60000
jdbc.minEvictableIdleTimeMillis=300000

jdbc.validationQuery=SELECT 'x'
jdbc.testWhileIdle=true
jdbc.testOnBorrow=false
jdbc.testOnReturn=false

jdbc.maxOpenPreparedStatements=20
jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=1800
jdbc.logAbandoned=true

8.连接池的配置spring-pool.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- C3P0数据库配置 -->
    <bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${oracle.jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${oracle.jdbc.url}" />
        <property name="user" value="${oracle.jdbc.username}" />
        <property name="password" value="${oracle.jdbc.password}" />
        <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement" value="5" />
        <!-- 定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
        <property name="acquireRetryAttempts" value="30" />
        <!-- 两次连接中间隔时间,单位毫秒。Default: 1000 -->
        <property name="acquireRetryDelay" value="1000" />
        <!-- 连接关闭时默认将所有未提交的操作回滚。Default: false -->
        <property name="autoCommitOnClose" value="false" />
        <!-- 当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出 SQLException,如设为0则无限期等待。单位毫秒。Default: 0 -->
        <property name="checkoutTimeout" value="10000" />
        <!-- 每60秒检查所有连接池中的空闲连接。Default: 0 -->
        <property name="idleConnectionTestPeriod" value="60" />
        <!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize" value="20" />
        <!-- 连接池中保留的最小连接数 -->
        <property name="minPoolSize" value="20" />
        <!-- 连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize" value="100" />
        <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime" value="60" />
        <!-- c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么 属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试 使用。Default: null -->
        <property name="automaticTestTable" value="c3p0_TestTable" />
        <!-- 获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
        <property name="breakAfterAcquireFailure" value="false" />
    </bean>


    <!-- BoneCP配置 -->
    <bean id="bonecp" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
        <property name="driverClass" value="${oracle.jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${oracle.jdbc.url}" />
        <property name="username" value="${oracle.jdbc.username}" />
        <property name="password" value="${oracle.jdbc.password}" />
        <!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
        <property name="idleConnectionTestPeriodInMinutes" value="60" />
        <!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
        <property name="idleMaxAgeInMinutes" value="30" />
        <!-- 每个分区最大的连接数 -->
        <property name="maxConnectionsPerPartition" value="30" />
        <!-- 每个分区最小的连接数 -->
        <property name="minConnectionsPerPartition" value="10" />
        <!-- 分区数 ,默认值2,最小1,推荐3-4,视应用而定 -->
        <property name="partitionCount" value="3" />
        <!-- 每次去拿数据库连接的时候一次性要拿几个,默认值:2 -->
        <property name="acquireIncrement" value="5" />
        <!-- 缓存prepared statements的大小,默认值:0 -->
        <property name="statementsCacheSize" value="100" />
        <!-- 每个分区释放链接助理进程的数量,默认值:3,除非你的一个数据库连接的时间内做了很多工作,不然过多的助理进程会影响你的性能 -->
        <property name="releaseHelperThreads" value="3" />
    </bean>

    <!-- proxool配置 -->
    <bean id="proxool" class="org.logicalcobwebs.proxool.ProxoolDataSource" >
        <property name="driver" value="${oracle.jdbc.driverClassName}" />
        <property name="driverUrl" value="${oracle.jdbc.url}" />
        <property name="user" value="${oracle.jdbc.username}" />
        <property name="password" value="${oracle.jdbc.password}" />
        <!--数据源的别名 -->
        <property name="alias" value="proxool_alias" />
        <!-- 空闲连接个数 默认为0 -->
        <property name="prototypeCount" value="4" />
        <!--最小连接数(默认5个) -->
        <property name="minimumConnectionCount" value="1" />
        <!--最大连接数(默认15个),超过了这个连接数,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定 -->
        <property name="maximumConnectionCount" value="30" />
        <!-- 如果侦察线程发现闲置连接,则会使用这个SQL语句来对这些连接进行检查 -->
        <property name="houseKeepingTestSql" value="select sysdate from dual" />
    </bean>

    <!-- dbcp配置 -->
    <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${oracle.jdbc.driverClassName}" />
        <property name="url" value="${oracle.jdbc.url}" />
        <property name="username" value="${oracle.jdbc.username}" />
        <property name="password" value="${oracle.jdbc.password}" />
        <!--连接池启动时的初始化 -->
        <property name="initialSize" value="1" />
        <!--连接池的最大值 -->
        <property name="maxActive" value="30" />
        <!-- 最大空闲值,当经过一个高峰时间后,连接池可以慢慢将已经用不到的链接慢慢释放一部分,一直减少到maxle为止 -->
        <property name="maxIdle" value="2" />
        <!-- 最小空闲值,当空闲的连接数少于阀值时,连接池就会预申请去一些链接,以免洪峰来时来不及申请 -->
        <property name="minIdle" value="1" />
        <!-- 运行判断连接超时任务的时间间隔,单位为毫秒,默认为-1,即不执行任务。 -->
        <property name="timeBetweenEvictionRunsMillis" value="3600000" />
        <!-- 连接的超时时间,默认为半小时。 -->
        <property name="minEvictableIdleTimeMillis" value="3600000" />
    </bean>

</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值