Eleven, MyBatis configures the C3P0 connection pool

Although MyBatis comes with its own connection pool POOLED, we do not recommend using it. It is recommended to use mainstream connection pools on the market such as C3P0

One, use C3P0 connection pool

  1. Import dependencies
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.4</version>
</dependency>
  1. Create a data source factory class compatible with C3P0 and MyBatis (same for other connection pools)
/**
 * C3P0与mybatis兼容使用的数据源工厂类
 */
public class C3P0DataSourceFactory extends UnpooledDataSourceFactory {
    
    
    public C3P0DataSourceFactory() {
    
    
        this.dataSource = new ComboPooledDataSource();
    }
}
  1. Modify the mybatis-config.xml file
<?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>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!--指定对应的数据库,这个可以不写,插件会自动识别,但是严谨角度建议还是写一下-->
            <property name="helperDialect" value="mysql"/>
            <!--开启分页合理化(如输入第0页时,展示第一页数据,输入超过最大页数的数据时候,展示最大页数的数据)-->
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>
            <!--采用C3PO进行连接-->
            <dataSource type="com.imooc.mybatis.datasource.C3P0DataSourceFactory">
                <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
                <property name="jdbcUrl"
                          value="jdbc:mysql://localhost:3306/babytun?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone=GMT"/>
                <property name="user" value="root"/>
                <property name="password" value="123456"/>
                <property name="initialPoolSize" value="5"/>
                <property name="maxPoolSize" value="20"/>
                <property name="minPoolSize" value="5"/>
            </dataSource>
        </environment>
    
    <!--声明goods.xml类,让mybatis认识这个文件-->
    <mappers>
        <mapper resource="mappers/goods.xml"/>
    </mappers>

</configuration>

Guess you like

Origin blog.csdn.net/qq_36792120/article/details/112492671