首页 > 代码库 > springBoot和c3p0的整合

springBoot和c3p0的整合

首先创建c3p0的数据源类

package com.example.demo.config;


import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class C3p0DataSourceConfig {

    @Bean(name="dataSource")
    @Qualifier(value="dataSource")//限定描述符除了能根据名字进行注入,但能进行更细粒度的控制如何选择候选者
    @Primary//用@Primary区分主数据源
    @ConfigurationProperties(prefix="c3p0")//指定配置文件中,前缀为c3p0的属性值
    public DataSource dataSource(){
        return  DataSourceBuilder.create().type(com.mchange.v2.c3p0.ComboPooledDataSource.class).build();//创建数据源
    }
    /**
    *返回sqlSessionFactory
    */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(){
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource());
        return sqlSessionFactory;
    }
}

 

然后配置application.properties

c3p0.jdbcUrl=jdbc:mysql:///testcrm?characterEncoding=utf8
c3p0.user=root
c3p0.password=123
c3p0.driverClass=com.mysql.jdbc.Driver

 

springBoot和c3p0的整合