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;
    private boolean externalSqlSession;
    @Autowired(required = false)
    public final void setMySqlSessionFactory(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        //this.setSqlSessionFactory(sqlSessionFactory);
        if(!this.externalSqlSession){
            this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    @Autowired(required = false)
    public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate){
        this.sqlSession = sqlSessionTemplate;
        this.externalSqlSession = true;
    }
    public int delete(String sqlId,Object obj){
        return getSqlSession().delete(sqlId,obj);
    }


    public int insertOrUpdate(String sqlId, Object obj){
        return getSqlSession().update(sqlId,obj);
    }

    @Override
    protected void checkDaoConfig() throws IllegalArgumentException {
        // TODO Auto-generated method stub
        notNull(this.sqlSession, "Property sqlSessionFactory or sqlSessionTemplate are required");
    }

    public SqlSession getSqlSession() {
        return sqlSession;
    }

}
View Code

6. 使用方法

@Repository("iQueryDataBaseDao")
public class QueryDataBaseDaoImpl extends BaseDao implements IQueryDataBaseDao {
    
}
View Code

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

转载于:https://www.cnblogs.com/leavescy/p/11240384.html

你可能感兴趣的:(java,数据库)