其他分享
首页 > 其他分享> > springMvc中配置多个数据源

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文件中配置数据源

<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
        <property name="driver">
            <value>${jdbc-mysql.proxool.driver-class}</value>
        </property>
        <property name="driverUrl">
            <value>${jdbc-mysql.proxool.driver-url}</value>
        </property>
        <property name="user">
            <value>${jdbc-mysql.user}</value>
        </property>
        <property name="password">
            <value>${jdbc-mysql.password}</value>
        </property>
        <property name="alias">
            <value>${jdbc-mysql.proxool.alias}</value>
        </property>
        <property name="prototypeCount">
            <value>${jdbc-mysql.proxool.prototype-count}</value>
        </property>
        <property name="maximumConnectionCount">
            <value>${jdbc-mysql.proxool.maximum-connection-count}</value>
        </property>
        <property name="minimumConnectionCount">
            <value>${jdbc-mysql.proxool.minimum-connection-count}</value>
        </property>
        <property name="maximumActiveTime">
            <value>${jdbc-mysql.proxool.maximum-active-time}</value>
        </property>
        <property name="trace">
            <value>${jdbc-mysql.proxool.trace}</value>
        </property>
        <property name="verbose">
            <value>${jdbc-mysql.proxool.verbose}</value>
        </property>
        <property name="statistics">
            <value>${jdbc-mysql.proxool.statistics}</value>
        </property>
        <property name="houseKeepingTestSql">
            <value>${jdbc-mysql.proxool.house-keeping-test-sql}</value>
        </property>
        <property name="loginTimeout">
            <value>5000</value>
        </property>
        <property name="simultaneousBuildThrottle">
            <value>${jdbc-mysql.proxool.simultaneous-build-throttle}</value>
        </property>
    </bean>
View Code

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

<bean id="mysqlSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mySqlMapConfig.xml" />
        <property name="dataSource" ref="dataSource" />

    </bean>
View Code

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

<tx:annotation-driven transaction-manager="transactionManager1" />
    <bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
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

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

标签:jdbc,多个,springMvc,数据源,org,mysql,import,proxool,public
来源: https://www.cnblogs.com/leavescy/p/11240384.html