basedao java 多数据源_springMvc中配置多个数据源

首先声明,我采用的是myBatis连接的数据库哈

1. 我们需要在配置数据库相关信息, 如果有多个数据库,复制一份下面的配置,修改DBPool1 名字和driver-url ,user, password

jdbc-mysql.proxool.alias=DBPool1

jdbc-mysql.proxool.driver-class=com.mysql.jdbc.Driver

jdbc-mysql.proxool.driver-url=jdbc:mysql:xxxx?useUnicode=true&characterEncoding=utf8

jdbc-mysql.user=xxxx

jdbc-mysql.password=xxxx

jdbc-mysql.proxool.maximum-connection-count=150

jdbc-mysql.proxool.minimum-connection-count=50

jdbc-mysql.proxool.prototype-count=20

jdbc-mysql.proxool.maximum-active-time=1800000

jdbc-mysql.proxool.house-keeping-test-sql=select 1 from DUAL

jdbc-mysql.proxool.verbose=false

jdbc-mysql.proxool.trace=false

jdbc-mysql.proxool.statistics=10s,1m,1d

jdbc-mysql.proxool.statistics-log-level=ERROR

jdbc-mysql.proxool.simultaneous-build-throttle=75

2. 在dataSource.xml文件中配置数据源

${jdbc-mysql.proxool.driver-class}

${jdbc-mysql.proxool.driver-url}

${jdbc-mysql.user}

${jdbc-mysql.password}

${jdbc-mysql.proxool.alias}

${jdbc-mysql.proxool.prototype-count}

${jdbc-mysql.proxool.maximum-connection-count}

${jdbc-mysql.proxool.minimum-connection-count}

${jdbc-mysql.proxool.maximum-active-time}

${jdbc-mysql.proxool.trace}

${jdbc-mysql.proxool.verbose}

${jdbc-mysql.proxool.statistics}

${jdbc-mysql.proxool.house-keeping-test-sql}

5000

${jdbc-mysql.proxool.simultaneous-build-throttle}

View Code

3. 在dataSource.xml文件中配置注入bean

View Code

4. 在dataSource.xml文件中配置事务

View Code

5. 配置Mybatis数据库操作

package test.ke.dao;

import static org.springframework.util.Assert.notNull;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.mybatis.spring.SqlSessionTemplate;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.dao.support.DaoSupport;

import org.springframework.stereotype.Repository;

@Repository("baseDao")

public class baseDao extends DaoSupport{

private SqlSession sqlSession;

privatebooleanexternalSqlSession;

@Autowired(required= false)

public finalvoid setMySqlSessionFactory(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory){//this.setSqlSessionFactory(sqlSessionFactory);

if(!this.externalSqlSession){this.sqlSession = newSqlSessionTemplate(sqlSessionFactory);

}

}

@Autowired(required= false)

public finalvoidsetSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate){this.sqlSession =sqlSessionTemplate;this.externalSqlSession = true;

}

publicint delete(String sqlId,Object obj){return getSqlSession().delete(sqlId,obj);

}

publicintinsertOrUpdate(String sqlId, Object obj){returngetSqlSession().update(sqlId,obj);

}

@Override

protectedvoidcheckDaoConfig() throws IllegalArgumentException {//TODO Auto-generated method stub

notNull(this.sqlSession, "Property sqlSessionFactory or sqlSessionTemplate are required");

}

public SqlSession getSqlSession() {returnsqlSession;

}

}

View Code

6. 使用方法

@Repository("iQueryDataBaseDao")

public class QueryDataBaseDaoImpl extends BaseDao implements IQueryDataBaseDao {

}

View Code

到此为止, 数据库就可以正常连接了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值