springboot配置mysql数据源

@Configuration
@MapperScan(basePackages = XXX.PACKAGE, sqlSessionFactoryRef = "XXSqlSessionFactory")
public class XXX{
    static final String PACKAGE = "com.hikvision.ezviz.XX.mapper";
    static final String MAPPER_LOCATION = "classpath*:mapper/*.xml";


    @Bean(name = "XXDataSource")
    public DataSource dataSource() {
        final DruidDataSource dataSource = new DruidDataSource();
        if (url.indexOf("?") <= 0) {
            url += "??rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&failOverReadOnly=false&autoReconnect=true&roundRobinLoadBalance=true&generateSimpleParameterMetadata=true";
        }
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        //连接池的最大值
        dataSource.setMaxActive(connections);
         // 最小空闲值
        dataSource.setMinIdle(connections);
        return dataSource;
    }

    @Bean(name = "upgradeSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("XXDataSource") DataSource XXDataSource) throws Exception {
        try {
            MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(XXDataSource);
            sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                    .getResources(MAPPER_LOCATION));
            return sqlSessionFactoryBean.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

你可能感兴趣的:(Mysql)