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);@AutowiredJdeDataSourceProperties 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;}