关于码云开源项目SpringBootAdmin多数据源配置

SpringBootAdmin是码云上一个以springboot为核心的开源的后台管理系统。

这里是链接地址:点击打开链接

由于是后台系统,应该采用数据库分离,权限,用户,角色和业务模块分开。

application.properties

#服务端口
server.port = 8001
# ĿcontextPath
server.context-path = /
# session
server.session-timeout=60
#which active
#spring.profiles.active=pro  


logging.level.com.mys.my.mapper = DEBUG


#主数据库配置
spring.datasource.master.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.master.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.master.name = geekcattle
spring.datasource.master.url=jdbc:mysql://localhost:3306/geekcattle?useUnicode=true&characterEncoding=UTF-8
spring.datasource.master.username = root
spring.datasource.master.password = 1234
spring.datasource.master.filters = stat
spring.datasource.master.maxActive = 20
spring.datasource.master.initialSize =  5
spring.datasource.master.maxWait = 60000
spring.datasource.master.minIdle = 20
spring.datasource.master.timeBetweenEvictionRunsMillis = 60000
spring.datasource.master.minEvictableIdleTimeMillis = 300000
spring.datasource.master.validationQuery = select 'x'
spring.datasource.master.testWhileIdle = true
spring.datasource.master.testOnBorrow = false
spring.datasource.master.testOnReturn = false
spring.datasource.master.poolPreparedStatements = true
spring.datasource.master.maxOpenPreparedStatements = 20


#从数据库配置
spring.datasource.film.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.film.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.film.name = film
spring.datasource.film.url=jdbc:mysql://localhost:3306/film?useUnicode=true&characterEncoding=UTF-8
spring.datasource.film.username = root
spring.datasource.film.password = 1234
spring.datasource.film.filters = stat
spring.datasource.film.maxActive = 20
spring.datasource.film.initialSize =  1
spring.datasource.film.maxWait = 60000
spring.datasource.film.minIdle = 20
spring.datasource.film.timeBetweenEvictionRunsMillis = 60000
spring.datasource.film.minEvictableIdleTimeMillis = 300000
spring.datasource.film.validationQuery = select 'x'
spring.datasource.film.testWhileIdle = true
spring.datasource.film.testOnBorrow = false
spring.datasource.film.testOnReturn = false
spring.datasource.film.poolPreparedStatements = true
spring.datasource.film.maxOpenPreparedStatements = 20

#MVC
spring.mvc.view.prefix = classpath:/templates/
spring.mvc.view.suffix = .html
spring.mvc.date-format=yyyy-MM-dd HH:mm:ss
#
spring.thymeleaf.mode = HTML5
spring.thymeleaf.cache = false
spring.thymeleaf.encoding = UTF-8
spring.thymeleaf.content-type = text/html
#mybaties
spring.mapper.plugin = tk.mybatis.mapper.generator.MapperPlugin
spring.mapper.Mapper = com.mys.my.util.CustomerMapper
#json
spring.jackson.time-zone=Asia/Chongqing
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.joda-date-time-format=yyyy-MM-dd HH:mm:ss


# Redis数据库索引(默认为0)
spring.redis.database=1
# Redis服务器地址
spring.redis.host=
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=30
# 超时时间
spring.redis.timeout=100000
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=20
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1

MyBatisConfig.java

/*
 * Copyright (c) 2017  All rights reserved.
 */

package com.mys.my.conf;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import com.github.pagehelper.PageHelper;
/**
 * MyBatis基础配置
 */
@Configuration
@PropertySource("classpath:application.properties")
//@EnableTransactionManagement
public class MyBatisConfig {

//    @Autowired
//    DataSource dataSource;
	
	@Bean(name="masterDataSource")
	@ConfigurationProperties(prefix = "spring.datasource.master")
	@Primary//默认数据源
	public DataSource dataSource() {
		System.out.println("======================================"+DataSourceBuilder.create().build());
		return DataSourceBuilder.create().build();
	}
	
    @Bean(name="masterSqlSessionFactory")
    @Primary//默认数据源
    public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("masterDataSource") DataSource dataSource) throws SQLException {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    System.out.println("dataSource===================================================="+dataSource);
    bean.setDataSource(dataSource);
    bean.setTypeAliasesPackage("com.mys.my.model");

    //分页插件
    PageHelper pageHelper = new PageHelper();
    Properties properties = new Properties();
    properties.setProperty("reasonable", "true");
    properties.setProperty("supportMethodsArguments", "true");
    properties.setProperty("returnPageInfo", "check");
    properties.setProperty("params", "count=countSql");
    pageHelper.setProperties(properties);

    //添加插件
    bean.setPlugins(new Interceptor[]{pageHelper});

    //添加XML目录
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    try {
        bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
        bean.setMapperLocations(resolver.getResources("classpath:mapper/*/*.xml"));
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Bean("masterSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean("masterTransactionManager")
    @Primary
    public PlatformTransactionManager annotationDrivenTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

新建一个数据库配置文件 MyBatisConfigFilm.java

/*
 * Copyright (c) 2017  All rights reserved.
 */

package com.mys.my.conf;


import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.github.pagehelper.PageHelper;
/**
 * MyBatis基础配置
 */
@Configuration
@MapperScan(basePackages={"com.mys.my.mapper.fiz"},sqlSessionFactoryRef="filmSqlSessionFactory")
public class MyBatisConfigFilm{
	@Bean(name="filmDatasource")
	@ConfigurationProperties(prefix = "spring.datasource.film")
	public DataSource dataSource() {
		System.out.println("======================================"+DataSourceBuilder.create().build());
		return DataSourceBuilder.create().build();
	}
    
    @Bean(name="filmSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("filmDatasource") DataSource dataSource) throws SQLException {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    System.out.println("dataSource===================================================="+dataSource());
    bean.setDataSource(dataSource());
    bean.setTypeAliasesPackage("com.mys.my.pojo");

    //分页插件
    PageHelper pageHelper = new PageHelper();
    Properties properties = new Properties();
    properties.setProperty("reasonable", "true");
    properties.setProperty("supportMethodsArguments", "true");
    properties.setProperty("returnPageInfo", "check");
    properties.setProperty("params", "count=countSql");
    pageHelper.setProperties(properties);

    //添加插件
    bean.setPlugins(new Interceptor[]{pageHelper});

    //添加XML目录
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    try {
//        bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
        bean.setMapperLocations(resolver.getResources("classpath:mapper/fiz/*.xml"));
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Bean("filmSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("filmSqlSessionFactory")SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "filmTransactionManager")
    public PlatformTransactionManager annotationDrivenTransactionManager(@Qualifier("filmDatasource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource());
    }
}

@MapperScan(basePackages={"com.mys.my.mapper.fiz"}的配置是扫描配置的dao文件 *.java
bean.setMapperLocations(resolver.getResources("classpath:mapper/fiz/*.xml"));
这个配置是扫描对应的mapper文件 *.xml。

MyBatisMapperScannerConfig.java

/*
 * Copyright (c) 2017  All rights reserved.
 */

package com.mys.my.conf;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//import com.mys.my.mapper.dataSource.MyFilmDatasource;

import tk.mybatis.spring.mapper.MapperScannerConfigurer;

import java.util.Properties;

/**
 * MyBatis扫描接口,使用的tk.mybatis.spring.mapper.MapperScannerConfigurer,如果你不使用通用Mapper
 */
@Configuration
//TODO 注意,由于MapperScannerConfigurer执行的比较早,所以必须有下面的注解
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig {
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("masterSqlSessionFactory");
        mapperScannerConfigurer.setSqlSessionTemplateBeanName("masterSqlSessionTemplate");
        mapperScannerConfigurer.setBasePackage("com.mys.my.mapper");
        Properties properties = new Properties();
        properties.setProperty("mappers", "com.mys.my.util.CustomerMapper");
        properties.setProperty("notEmpty", "false");
        properties.setProperty("IDENTITY", "MYSQL");
        mapperScannerConfigurer.setProperties(properties);
        return mapperScannerConfigurer;
    }
}

你可能感兴趣的:(JAVA)