我一直在 MyBatis 中使用 Spring,它对于单个数据库来说运行得非常好。我在尝试添加另一个数据库时遇到了困难(请参阅Github 上可重现的示例 https://github.com/alexwoolford/mybatis-spring-multiple-mysql-reproducible-example).
我正在使用 Spring Java 配置(即不是 XML)。我见过的大多数示例都展示了如何使用 XML 来实现这一点。
我有两个数据配置类(A 和 B),如下所示:
@Configuration
@MapperScan("io.woolford.database.mapper")
public class DataConfigDatabaseA {
@Bean(name="dataSourceA")
public DataSource dataSourceA() throws SQLException {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriver(new com.mysql.jdbc.Driver());
dataSource.setUrl("jdbc:mysql://" + dbHostA + "/" + dbDatabaseA);
dataSource.setUsername(dbUserA);
dataSource.setPassword(dbPasswordA);
return dataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSourceA());
return sessionFactory.getObject();
}
}
两个映射器和一个自动装配映射器的服务:
@Service
public class DbService {
@Autowired
private DbMapperA dbMapperA;
@Autowired
private DbMapperB dbMapperB;
public List<Record> getDabaseARecords(){
return dbMapperA.getDatabaseARecords();
}
public List<Record> getDabaseBRecords(){
return dbMapperB.getDatabaseBRecords();
}
}
应用程序不会启动:
Error creating bean with name 'dataSourceInitializer':
Invocation of init method failed; nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type [javax.sql.DataSource] is defined:
expected single matching bean but found 2: dataSourceB,dataSourceA
我读过可以使用@Qualifier
注释来消除自动装配的歧义,尽管我不确定在哪里添加它。
你能看出我错在哪里吗?
如果你想同时使用两个数据源并且它们没有主从关系,你应该禁用DataSourceAutoConfiguration
by @EnableAutoConfiguration(excludes = {DataSourceAutoConfiguration.class})
在您的应用程序中注释为@SpringBootApplication
。之后,您可以创建自己的SqlSessionFactory
并捆绑你自己的DataSource
。如果您也想使用DataSourceTransactionManager
,你也应该这样做。
在这种情况下,您还没有禁用DataSourceAutoConfiguration
,所以spring框架会尝试@Autowired
只有一个DataSource
但得到了两个,发生错误。
正如我之前所说,你应该禁用DataSourceAutoConfiguration
并手动配置它。
您可以禁用数据源自动配置,如下所示:
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class YourApplication implements CommandLineRunner {
public static void main (String... args) {
SpringApplication.run(YourApplication.class, args);
}
}
如果你确实想同时使用多个数据库,我建议你手动注册适当的bean,例如:
package xyz.cloorc.boot.mybatis;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Repository;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.sql.DataSource;
@Configuration
public class SimpleTest {
private DataSource dsA;
private DataSource dsB;
@Bean(name = "dataSourceA")
public DataSource getDataSourceA() {
return dsA != null ? dsA : (dsA = new BasicDataSource());
}
@Bean(name = "dataSourceB")
public DataSource getDataSourceB() {
return dsB != null ? dsB : (dsB = new BasicDataSource());
}
@Bean(name = "sqlSessionFactoryA")
public SqlSessionFactory getSqlSessionFactoryA() throws Exception {
// set DataSource to dsA
return new SqlSessionFactoryBean().getObject();
}
@Bean(name = "sqlSessionFactoryB")
public SqlSessionFactory getSqlSessionFactoryB() throws Exception {
// set DataSource to dsB
return new SqlSessionFactoryBean().getObject();
}
}
@Repository
public class SimpleDao extends SqlSessionDaoSupport {
@Resource(name = "sqlSessionFactoryA")
SqlSessionFactory factory;
@PostConstruct
public void init() {
setSqlSessionFactory(factory);
}
@Override
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
public <T> T get (Object id) {
return super.getSqlSession().selectOne("sql statement", "sql parameters");
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)