关于c3p0数据库连接池的配置

在这儿保存一下,防止丢失
配置spring-web框架的话需要放在resources下面

<!--数据库连接池配置-->
<c3p0-config>
    <default-config>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/**</property>
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password">*****</property>
        <property name="acquireIncrement">3</property>
        <property name="initialPoolSize">10</property>
        <property name="minPoolSize">2</property>
        <property name="maxPoolSize">10</property>
    </default-config>
    <named-config name="oracle-config">
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/**</property>
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password">*****</property>
        <property name="acquireIncrement">3</property>
        <property name="initialPoolSize">10</property>
        <property name="minPoolSize">2</property>
        <property name="maxPoolSize">10</property>
    </named-config>
</c3p0-config>

若使用mybatis框架,则不用上述的配置,添加db.properties文件

dataSource.driverClass=com.mysql.cj.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://localhost:3306/**
dataSource.user=root
dataSource.password=**
dataSource.maxPoolSize=20
dataSource.maxIdleTime=1000
dataSource.minPoolSize=1
dataSource.initialPoolSize=1

创建mybatis-config.xml文件,配置如下

<?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <!-- 环境配置:事务管理器和数据源配置 -->
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC" />
                <dataSource type="POOLED">
                    <property name="driver" value="${jdbc.driver}" />
                    <property name="url" value="${jdbc.url}" />
                    <property name="username" value="${jdbc.username}" />
                    <property name="password" value="${jdbc.password}" />
                </dataSource>
            </environment>
        </environments>
        <!-- 映射器 -->
        <mappers>
            <mapper resource="mybatis-config.xml" />
        </mappers>
    </configuration>

添加applicationContext.xml文件
在web.xml文件中添加下列内容

   <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- contextConfigLocation参数用来指定Spring的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </context-param>

applicationContext.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:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
        <!-- 配置数据源(里面存放了若干个连接对象):数据库交互的。    数据源:c3p0,druid(阿里) -->
<!--        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">-->
<!--            <property name="jdbcUrl" value="{}"></property>-->
<!--            <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>-->
<!--            <property name="user" value="root"></property>-->
<!--            <property name="password" value="wn200012113019"></property>-->
<!--        </bean>-->
<!--        &lt;!&ndash; 配置springJdbc的模板类 &ndash;&gt;-->
<!--        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">-->
<!--            <property name="dataSource" ref="dataSource"></property>-->
<!--        </bean>-->
    <mybatis:scan base-package="com.chaoxing.dao" />
    <context:component-scan base-package="com.chaoxing" />
    <context:property-override location="classpath:database.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"/>

    <!-- 配置SqlSessionFactory,org.mybatis.spring.SqlSessionFactoryBean是Mybatis社区开发用于整合Spring的bean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
          p:dataSource-ref="dataSource"/>
    <!-- JDBC事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          p:dataSource-ref="dataSource"/>
    <!-- 启用支持annotation注解方式事务管理 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="teacher" class="com.chaoxing.service.TeacherImpl"/>
</beans>

maven依赖

<dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
 <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.2</version>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值