征信--双数据源

application.properties文件的配置

# 第一个数据库配置
spring.datasource.url=jdbc:mysql://192.168.11.32:3316/credit_platform_v2?useUnicode=true&characterEncoding=UTF8&autoReconnect=true&allowMultiQueries=true&useSSL=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=hxxc_sa
spring.datasource.password=hxxc_sa

# hikariDB
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.connectionTestQuery=SELECT 1
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.autoCommit=false
spring.datasource.hikari.connectionTimeout=2000
spring.datasource.hikari.minimumIdle=16
spring.datasource.hikari.maximumPoolSize=64
spring.datasource.hikari.dataSourceProperties.cachePrepStmts=true
spring.datasource.hikari.dataSourceProperties.prepStmtCacheSize=250
spring.datasource.hikari.dataSourceProperties.prepStmtCacheSqlLimit=2048
spring.datasource.hikari.dataSourceProperties.useServerPrepStmts=true

# 第二个数据库配置
spring.second-datasource.url=jdbc:mysql://192.168.11.32:3316/huaxia_datacenter_collect?useUnicode=true&characterEncoding=UTF8&autoReconnect=true&allowMultiQueries=true&useSSL=true
spring.second-datasource.driver-class-name=com.mysql.jdbc.Driver
spring.second-datasource.username=hxxc_sa
spring.second-datasource.password=hxxc_sa

# hikariDB
spring.second-datasource.type=com.zaxxer.hikari.HikariDataSource
spring.second-datasource.hikari.connectionTestQuery=SELECT 1
spring.second-datasource.hikari.connection-timeout=60000
spring.second-datasource.hikari.autoCommit=false
spring.second-datasource.hikari.connectionTimeout=2000
spring.second-datasource.hikari.minimumIdle=16
spring.second-datasource.hikari.maximumPoolSize=64
spring.second-datasource.hikari.dataSourceProperties.cachePrepStmts=true
spring.second-datasource.hikari.dataSourceProperties.prepStmtCacheSize=250
spring.second-datasource.hikari.dataSourceProperties.prepStmtCacheSqlLimit=2048
spring.second-datasource.hikari.dataSourceProperties.useServerPrepStmts=true

mybatis.mapper-locations=classpath:config/mybatis/mappings/**/*Mapper.xml
mybatis.config-location=classpath:config/mybatis/mybatis-config.xml

com.huaxia.credit.config文件下的java类

第一个数据源


import com.huaxia.common.core.dao.impl.MyBatisAccessor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DbConfig {
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public JdbcTemplate primaryJdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "sqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactory( DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:config/mybatis/mappings/**/*Mapper.xml"));
        return bean.getObject();
    }

    @Bean(name = "mBaseDao")
    @Primary
    @ConditionalOnMissingBean
    public MyBatisAccessor<?> setMyBatisAccessor( @Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        MyBatisAccessor<?> myBatisAccessor = new MyBatisAccessor<>();
        myBatisAccessor.setSqlSessionFactory(sqlSessionFactory);
        return myBatisAccessor;
    }
}

第二个数据源

import com.huaxia.common.core.dao.impl.MyBatisAccessor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DbSecondConfig {

    @Bean(name = "secondDatasource")
    @ConfigurationProperties(prefix = "spring.second-datasource")
    public DataSource secondDataSource() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "collectSqlSessionFactory")
    public SqlSessionFactory collectSqlSessionFactory(@Qualifier("secondDatasource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:config/mybatis/collectMappings/**/*Mapper.xml"));
        return bean.getObject();
    }


    @Bean(name = "mCollectDao")
    public MyBatisAccessor<?> setCollectMyBatisAccessor(@Qualifier("collectSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        MyBatisAccessor<?> myBatisAccessor = new MyBatisAccessor<>();
        myBatisAccessor.setSqlSessionFactory(sqlSessionFactory);
        return myBatisAccessor;
    }
}

公共类的Repository

DomainRepository

import java.io.Serializable;

public interface DomainRepository<T, PK extends Serializable> {
    long insert(T var1);

    long update(T var1);

    long delete(T var1);

    long deleteById(PK var1);

    T find(PK var1);
}

第一数据源MbDomainRepositoryImp


import com.github.pagehelper.PageInfo;
import com.huaxia.common.core.dao.MBaseDao;
import com.huaxia.common.util.utils.ClassNameUtil;
import com.huaxia.common.util.utils.ReflectionUtils;
import com.huaxia.data.center.common.model.QueryConditionBase;
import java.io.Serializable;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class MbDomainRepositoryImp<T, PK extends Serializable> implements DomainRepository<T, PK> {
    public static final String SELECT = "select";
    public static final String BY_CONDITION = "ByCondition";
    @Autowired
    private MBaseDao dao;
    protected Class<T> entityClass = ReflectionUtils.getSuperClassGenricType(this.getClass());
    private transient Logger logger;

    protected Logger getLogger() {
        if (this.logger == null) {
            this.logger = LoggerFactory.getLogger(this.getClass());
        }

        return this.logger;
    }

    public Class<T> getEntityClass() {
        return this.entityClass;
    }

    public void setEntityClass(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    public MbDomainRepositoryImp() {
    }

    public MBaseDao getDao() {
        return this.dao;
    }

    public void setDao(MBaseDao dao) {
        this.dao = dao;
    }

    public long insert(T entity) {
        String insertStatement = this.getInsertStatement(entity);
        return this.dao.create(insertStatement, entity);
    }

    public T find(PK id) {
        String selectStatement = this.getSelectByIdStatement(this.entityClass);
        T persistentObject = this.dao.findObjectByTemplate(selectStatement, id);
        return persistentObject;
    }

    public long update(T transientObject) {
        String updateStatement = this.getUpdateStatement(transientObject);
        return this.dao.update(updateStatement, transientObject);
    }

    public long delete(T persistentObject) {
        throw new IllegalStateException("not  permit");
    }

    public long deleteById(PK id) {
        throw new IllegalStateException("not permit");
    }

    public String getInsertStatement(T object) {
        return this.getStatement(object.getClass(), "insert");
    }

    public String getUpdateStatement(T object) {
        return this.getStatement(object.getClass(), "update");
    }

    public String getDeleteStatement(Class<?> persistentObjectClass) {
        return this.getStatement(persistentObjectClass, "delete");
    }

    public String getSelectStatement(Class<?> persistentObjectClass) {
        return this.getStatement(persistentObjectClass, "select");
    }

    public String getSelectByIdStatement(Class<?> persistentObjectClass) {
        return this.getStatement(persistentObjectClass, "select", "ById");
    }

    public String getStatement(Class<?> persistentObjectClass, String prefix) {
        String statement = ClassNameUtil.getClassNameWithoutPackage(persistentObjectClass).toLowerCase() + "." + prefix;
        return statement;
    }

    public String getStatement(Class<?> persistentObjectClass, String prefix, String suffix) {
        String statement = ClassNameUtil.getClassNameWithoutPackage(persistentObjectClass).toLowerCase() + "." + prefix;
        statement = statement.substring(0, statement.length()) + suffix;
        return statement;
    }

    public <T> T fetchOnlyOne(List<T> list) {
        return list != null && list.size() > 0 ? list.get(0) : null;
    }

    public <E extends QueryConditionBase> PageInfo<T> queryPage(E query) {
        String sql = this.getStatement(this.getEntityClass(), "select", "ByCondition");
        PageInfo<T> page = this.getDao().findByQuery(sql, query.getCurrentPage(), query.getPageSize(), new Object[]{query});
        if (query.isCountTotal()) {
            int count = this.count(query);
            page.setTotal((long)count);
        }

        return page;
    }

    public <E extends QueryConditionBase> List<T> queryList(E query) {
        if (query.getDeleteMark() == null && query.isDeleteMarkEqOneWhenQuery()) {
            query.setDeleteMark(1);
        }

        String sql = this.getStatement(this.getEntityClass(), "select", "ByCondition");
        return this.getDao().findListByTemplate(sql, query);
    }

    public <E extends QueryConditionBase> T query(E query) {
        if (query.getDeleteMark() == null && query.isDeleteMarkEqOneWhenQuery()) {
            query.setDeleteMark(1);
        }

        String sql = this.getStatement(this.getEntityClass(), "select", "ByCondition");
        List<T> list = this.getDao().findListByTemplate(sql, query);
        return this.fetchOnlyOne(list);
    }

    public Integer count(Object query) {
        return (Integer)this.getDao().findObjectByTemplate(this.getCountStatement(this.getEntityClass()), query);
    }

    public String getCountStatement(Class<?> persistentObjectClass) {
        return this.getStatement(persistentObjectClass, "select", "CountByQueryCriteria");
    }
}

第二数据源CollectDomainRepositoryImp


import com.huaxia.common.core.dao.MBaseDao;
import com.huaxia.common.core.dao.impl.MyBatisAccessor;
import com.huaxia.data.center.common.dao.MbDomainRepositoryImp;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * 
 * 
 * @author ww
 * 
 */
public abstract class CollectDomainRepositoryImp<T, PK extends Serializable> extends MbDomainRepositoryImp<T,PK> {

	@Resource(name="mCollectDao")
	private MBaseDao mCollectDao;

	@Override
	public MBaseDao getDao() {
		return mCollectDao;
	}

	@Override
	public void setDao(MBaseDao dao) {
		this.mCollectDao = dao;
	}

}

业务类

使用第一数据源

@Repository(value = "RhQuerecordTimesRepository")
public class RhQuerecordTimesRepository extends
		MbDomainRepositoryImp<RhQuerecordTimes, Serializable> {
}

使用第二数据源

@Repository(value = "RhCustomerInfoRepository")
public class RhCustomerInfoRepository extends
		CollectDomainRepositoryImp<RhCustomerInfo, Serializable> {
}

业务类直接调用dao

@Repository(value = "RhCredreportQuerecordRepository")
public class RhCredreportQuerecordRepository extends
        MbDomainRepositoryImp<RhCredreportQuerecord, Serializable> {

    @Autowired
    private MBaseDao mBaseDao;

    @Resource(name="mCollectDao")
    private MBaseDao mCollectDao;

    public long insertBatch(List<RhCredreportQuerecord> recordList) {
        return mBaseDao.create("rhcredreportquerecord.insertBatch", recordList);
    }

    public List<RhCredreportQuerecord> getListForOtherTimes(RhCredreportQuerecordQuery query) {
        return mBaseDao.findListByTemplate("rhcredreportquerecord.selListForOtherTimes", query);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值