配置

mybatis.datasource.xxx.configLocation=classpath*:xxx-mybatis.xml
mybatis.datasource.xxx.mapperLocations=classpath*:sqlmap/*/*.xml
mybatis.datasource.xxx.type=com.alibaba.druid.pool.DruidDataSource
mybatis.datasource.xxx.driverClassName=com.mysql.jdbc.Driver
mybatis.datasource.xxx.url=jdbc:mysql://100.xx.xx.31:xxx/xxx?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
mybatis.datasource.xxx.username=xxx
mybatis.datasource.xxx.password=xxxx
mybatis.datasource.xxx.initialSize=20
mybatis.datasource.xxx.minIdle=20
mybatis.datasource.xxx.maxActive=50
mybatis.datasource.xxx.timeBetweenEvictionRunsMillis=1800
mybatis.datasource.xxx.minEvictableIdleTimeMillis=60000
mybatis.datasource.xxx.maxEvictableIdleTimeMillis=100000
mybatis.datasource.xxx.maxWait=1000
mybatis.datasource.xxx.maxIdleTime=60000
mybatis.datasource.xxx.idleConnectionTestPeriod=60000
mybatis.datasource.xxx.validationQuery=SELECT 1
mybatis.datasource.xxx.testOnBorrow=false
mybatis.datasource.xxx.testWhileIdle=true
mybatis.datasource.xxx.phyTimeoutMillis=25200000
mybatis.datasource.xxx.keepAlive=true
mybatis.datasource.xxx.testOnReturn=false

properties


@Component
@Getter
@Setter
@PropertySource(value = { XXXProperties.LOCATION })
@ConfigurationProperties(prefix = XXXProperties.PREFIX)
public class XXXProperties {

    static final String PREFIX = "mybatis.datasource.xxx";

    static final String LOCATION = "classpath:application-datasource.properties";

    private String mapperLocations;

    private String configLocation;

    private String type;

    private String driverClassName;
}

datasource config


@Configuration
@MapperScan(basePackages = "com.xxxx.mapper", sqlSessionFactoryRef = "xxxSqlSessionFactory")
public class DataSourceConfig {

    @Resource
    private XxxProperties xxxProperties;

    @Resource
    private Environment environment;

    @Bean(name = "xxxDataSource")
    public DataSource xxxDataSource() {

        DataSource ds = DataSourceBuilder.create()
            .type((Class<? extends DataSource>) ClassUtils.resolveClassName(xxxProperties.getType(), null))
            .driverClassName(xxxProperties.getDriverClassName()).build();

        return Binder.get(environment).bind(XxxProperties.PREFIX, ds.getClass()).get();
    }

    @Bean(name = "xxxSqlSessionFactory")
    public SqlSessionFactory xxxSqlSessionFactory(@Qualifier("xxxDataSource") DataSource dataSource)
        throws Exception {

        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setConfigLocation(
            new PathMatchingResourcePatternResolver().getResources(xxxProperties.getConfigLocation())[0]
        );
        sessionFactory.setMapperLocations(
            new PathMatchingResourcePatternResolver().
                getResources(xxxProperties.getMapperLocations()));

        return sessionFactory.getObject();
    }

    @Bean(name = "xxxSqlSession")
    public SqlSessionTemplate xxxSqlSession(
        @Qualifier("xxxSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "xxxTx")
    public XxxTransactionManager xxxTx(@Qualifier("xxxDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

如果需要配置多数据数,则创建多个配置,及DataSourceConfig 并指定相应的mapper

如果dataSource要调整为HighAvailableDataSource及NodeListener的使用方式,则可以如下创建dataSource,并且在配置文件对象中添加url、password及username

DataSource ds = DataSourceBuilder.create()
    .type((Class<? extends DataSource>) ClassUtils.resolveClassName(distributionProperties.getType(), null))
    .driverClassName(distributionProperties.getDriverClassName())
    .build();
DataSource result = Binder.get(environment)
    .bind(DistributionProperties.PREFIX, ds.getClass())
    .get();
DisfNodeListener nodeListener = new DisfNodeListener();
nodeListener.setUrl(distributionProperties.getUrl());
nodeListener.setUsername(distributionProperties.getUsername());
nodeListener.setPassword(distributionProperties.getPassword());
HighAvailableDataSource highAvailableDataSource = (HighAvailableDataSource) result;
highAvailableDataSource.setNodeListener(nodeListener);
return highAvailableDataSource;