mybatis多数据源切换
2.2 不使用我们全局配置的mybatis,对指定文件夹下使用我们指定的Session。项目中有可能需要去其他的数据库取其他的表的信息。2.1 直接使用原生jdbc(不推荐)读取配置文件中的配置项。
·
1.前提
项目中有可能需要去其他的数据库取其他的表的信息
2.思路
2.1 直接使用原生jdbc(不推荐)
2.2 不使用我们全局配置的mybatis,对指定文件夹下使用我们指定的Session
3.解决办法
指定该配置的范围
package com.maycur.openapi.dao.mybatis.jde.config;
import com.maycur.openapi.dao.mybatis.BaseDataSourceConfiguration;
import com.maycur.openapi.dao.mybatis.interceptor.ParamInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.sql.SQLException;
@Configuration
@MapperScan(basePackages = "com.maycur.openapi.dao.mybatis.jde", sqlSessionTemplateRef = "jdeSessionTemplate")
public class JdeConfiguration extends BaseDataSourceConfiguration {
private static final Logger logger = LoggerFactory.getLogger(JdeConfiguration.class);
@Autowired
JdeDataSourceProperties properties;
@Bean(name = "jdeDataSource")
public DataSource getDataSource() throws SQLException {
DataSource dataSource = buildDataSource(properties);
logger.info("jdeDataSource init,url");
return dataSource;
}
@Bean(name = "jdeDataSourceTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("jdeDataSource") DataSource dataSource) throws SQLException {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "jdeSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("jdeDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
//指定作用范围
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:com/maycur/openapi/dao/mybatis/jde/mapper/*.xml"));
sessionFactory.setTypeHandlersPackage("com.maycur.openapi.dao.mybatis.typehandler");
sessionFactory.setPlugins(new ParamInterceptor());
return sessionFactory.getObject();
}
@Bean(name = "jdeSessionTemplate")
public SqlSessionTemplate sessionTemplate(
@Qualifier("jdeSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
package com.maycur.openapi.dao.mybatis;
import com.alibaba.druid.pool.DruidDataSource;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
* @author gy
* @date 2/21/22 1:35 PM
*/
public class BaseDataSourceConfiguration {
public DataSource buildDataSource(BaseDataSourceProperties properties) throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(properties.getDriverClassName());
dataSource.setUrl(properties.getUrl());
dataSource.setUsername(properties.getUsername());
dataSource.setPassword(properties.getPassword());
dataSource.setInitialSize(properties.getInitialSize());
dataSource.setMinIdle(properties.getMinIdle());
dataSource.setMaxActive(properties.getMaxActive());
dataSource.setMaxWait(properties.getMaxWait());
dataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
dataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
dataSource.setValidationQuery(properties.getValidationQuery());
dataSource.setTestWhileIdle(properties.isTestWhileIdle());
dataSource.setTestOnBorrow(properties.isTestOnBorrow());
dataSource.setTestOnReturn(properties.isTestOnReturn());
dataSource.setDbType(properties.getDbType());
dataSource.setFilters(properties.getFilters());
return dataSource;
}
}
读取配置文件中的配置项
package com.maycur.openapi.dao.mybatis.jde.config;
import com.maycur.openapi.dao.mybatis.BaseDataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = JdeDataSourceProperties.PREFIX)
public class JdeDataSourceProperties extends BaseDataSourceProperties {
public static final String PREFIX = "jde.datasource";
}
package com.maycur.openapi.dao.mybatis;
import lombok.Data;
/**
* @author gy
* @date 2/21/22 1:34 PM
*/
@Data
public class BaseDataSourceProperties {
private String url;
private String username;
private String password;
private String driverClassName;
private ClassLoader classLoader;
private int initialSize;
private int minIdle;
private int maxActive;
private long maxWait;
private long timeBetweenEvictionRunsMillis;
private long minEvictableIdleTimeMillis;
private String validationQuery;
private boolean testWhileIdle;
private boolean testOnBorrow;
private boolean testOnReturn;
private String filters;
private String dbType;
}

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)