springboot + druid + mybatis 扫包方式配置多数据源,仅仅扫mapper,和mybaits 的xml包,容易懂,好理解

主要是扫包,然后开启注解,暂时就算存档吧

@SuppressWarnings("AlibabaRemoveCommentedCode") //开启注解
@MapperScan(basePackages = {"com.selang.dataservice.hzgx.datasource","com.selang.dataservice.common.dao"}, sqlSessionFactoryRef = "hangZhouSqlSessionFactory")
@Configuration
public class DruidDBHangZhouConfig {
    private Logger logger = LoggerFactory.getLogger(DruidDBHangZhouConfig.class);
    @Value("${spring.datasource.hzgg.url}")
    private String dbUrl;

    @Value("${spring.datasource.hzgg.username}")
    private String username;

    @Value("${spring.datasource.hzgg.password}")
    private String password;

    @Value("${spring.datasource.hzgg.driverClassName}")
    private String driverClassName;

    @Value("${spring.datasource.initialSize}")
    private int initialSize;

    @Value("${spring.datasource.minIdle}")
    private int minIdle;

    @Value("${spring.datasource.maxActive}")
    private int maxActive;

    @Value("${spring.datasource.maxWait}")
    private int maxWait;

    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;

    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;

    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;

    @Value("${spring.datasource.testWhileIdle}")
    private boolean testWhileIdle;

    @Value("${spring.datasource.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${spring.datasource.testOnReturn}")
    private boolean testOnReturn;

    @Value("${spring.datasource.poolPreparedStatements}")
    private boolean poolPreparedStatements;

    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
    private int maxPoolPreparedStatementPerConnectionSize;

    @Value("${spring.datasource.filters}")
    private String filters;

    @Value("{spring.datasource.connectionProperties}")
    private String connectionProperties;

    
    @Bean(name = "dataSourceHangZhou")   //声明其为Bean实例
      //在同样的DataSource中,首先使用被标注的DataSource
    public DataSource dataSourceHangZhou() {
        DruidDataSource datasource = new DruidDataSource();

        datasource.setUrl(dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);

        //configuration
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        try {
            datasource.setFilters(filters);
        } catch (SQLException e) {
            logger.error("druid configuration initialization filter", e);
        }
        datasource.setConnectionProperties(connectionProperties);

        return datasource;
    }
    
    @Bean(name = "hangZhouSqlSessionFactory")
    public SqlSessionFactory hangZhouSqlSessionFactory()
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSourceHangZhou());
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/*/*.xml"));
        return bean.getObject();
    }
    
    @Bean(name = "hangZhouSqlSessionTemplate")
    public SqlSessionTemplate hangZhouSessionTemplate() throws Exception {
    	
        return new SqlSessionTemplate(hangZhouSqlSessionFactory());
    }
    @Bean(name = "hangZhouTransactionManager")
    public DataSourceTransactionManager hangZhouTransactionManager() {
        return new DataSourceTransactionManager(dataSourceHangZhou());
    }
    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("allow", ""); //白名单
        return reg;
    }

    @Bean 
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        filterRegistrationBean.addInitParameter("principalCookieName","USER_COOKIE");
        filterRegistrationBean.addInitParameter("principalSessionName","USER_SESSION");
        filterRegistrationBean.addInitParameter("DruidWebStatFilter","/*");
        return filterRegistrationBean;
    }

}

你可能感兴趣的:(java)