idea+mybatis+springboot+bonecp连接多数据库

数据源配置:有几个数据源就建几个类

@Configuration
//对不同数据库的mapper分包扫描
@MapperScan(basePackages = "com.mapper.census",sqlSessionFactoryRef="censusSqlSessionFactory")
public class CensusConfig {
    @Value("${mybatis_config_location}")
    private String mybatisConfigFilePath;
    @Value("${census_mapper_locations}")
    private String mapperPath;
    @Value("${mybatis_entity_package}")
    private String entityPackage;

    @Bean(name = "censusDataSource")
    public DataSource getCensusDataSource() {
        BoneCPDataSource datasource = new BoneCPDataSource();
        datasource.setDriverClass("com.mysql.jdbc.Driver");
        //可在这里注入配置文件中属性
        datasource.setJdbcUrl(url);
        datasource.setUsername(username);
        datasource.setPassword(password);
        return datasource;
    }

    @Bean(name = "censusSqlSessionFactory")
    public SqlSessionFactoryBean createSqlSessionFactoryBean(@Qualifier("censusDataSource")DataSource censusDataSource) throws IOException {
        return SqlSessionFactoryUtil.getSqlSessionFactoryBean(mybatisConfigFilePath,mapperPath,censusDataSource,entityPackage);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: bonecp.driverClass=org.gjt.mm.mysql.Driver bonecp.jdbcUrl=jdbc:mysql://localhost/manytomany?useUnicode=true&characterEncoding=UTF-8 bonecp.username=root bonecp.password=2008 #分区数量 bonecp.partitionCount = 1 #每个分区含有的最小连接bonecp.minConnectionsPerPartition = 1 #每个分区含有的最大连接bonecp.maxConnectionsPerPartition = 2 #每次新增连接的数量 bonecp.acquireIncrement = 1 #连接池阀值,当 可用连接/最大连接 < 连接阀值 时,创建新的连接 bonecp.poolAvailabilityThreshold = 20 #连接超时时间阀值,获取连接时,超出阀值时间,则获取失败,毫秒为单位 bonecp.connectionTimeout = 10000 #连接池助手线程数量,可设置为0,该参数会降低运行速度,但程序有大量连接时,有助于提升高并发程序的性能 bonecp.releaseHelperThreads = 0 #语句助手线程数,可设置为0,该参数会降低运行速度,但程序有大量的查询语句时,有助于提升高并发程序的性能 bonecp.statementReleaseHelperThreads = 0 #测试连接有效性的间隔时间,单位分钟 bonecp.idleConnectionTestPeriod = 60 #连接的空闲存活时间,当连接空闲时间大于该阀值时,清除该连接 bonecp.idleMaxAge = 240 #语句缓存个数,默认是0 bonecp.statementsCacheSize = 5 Spring中的配置信息 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="com.mvc" /> <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:default-servlet-handler /> <aop:config proxy-target-class="true"/> <tx:annotation-driven transaction-manager="txManager"/> <!-- 采用单数据源事务控制方式,通过注解来定义事务--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:proxool.properties</value> </property> </bean> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close"> <property name="driver"> <value>org.gjt.mm.mysql.Driver</value> </property> <property name="driverUrl"> <value>jdbc:mysql://localhost/manytomany?useUnicode=true&characterEncoding=UTF-8</value> </property> <property name="user"> <value>root</value> </property> <property name="password"> <value>2008</value> </property> <property name="alias"> <value>Db_name</value> </property> <property name="houseKeepingSleepTime"> <value>90000</value> </property> <property name="prototypeCount"> <value>50</value> </property> <property name="maximumConnectionCount"> <value>50</value> </property> <property name="minimumConnectionCount"> <value>2</value> </property> <property name="trace"> <value>true</value> </property> <property name="verbose"> <value>true</value> </property> </bean> </beans>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值