在Spring Boot和MyBatis项目中配置多个数据源时,发生BindingException

时间:2018-07-13 07:50:28

标签: java spring-boot datasource mybatis datamapper

这个问题来自NoUniqueBeanDefinitionException when multiple datasouces in Spring Boot and Mybatis project。我尝试在Spring Boot和MyBatis项目中配置两个dataSource,但是会发生异常:

  

org.apache.ibatis.binding.BindingException:无效的绑定语句   (未找到):com.bookSystem.mapper.UserDao.findByDomain位于   org.apache.ibatis.binding.MapperMethod $ SqlCommand。(MapperMethod.java:227)   〜[mybatis-3.4.6.jar:3.4.6]在   org.apache.ibatis.binding.MapperMethod。(MapperMethod.java:49)   〜[mybatis-3.4.6.jar:3.4.6]在   org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65)   〜[mybatis-3.4.6.jar:3.4.6]在   org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58)   〜[mybatis-3.4.6.jar:3.4.6]在   com.sun.proxy。$ Proxy83.findByDomain(未知来源)〜[na:na]在   com.bookSystem.serviceImp.UserServiceImp.findByDomain(UserServiceImp.java:72)   〜[classes /:na]在sun.reflect.NativeMethodAccessorImpl.invoke0(本机   方法)〜[na:1.8.0_171]在   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)   〜[na:1.8.0_171]在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   〜[na:1.8.0_171]在java.lang.reflect.Method.invoke(Method.java:498)   〜[na:1.8.0_171]

application.properties

# multiple datases
spring.datasource.primary.driverClassName = com.mysql.jdbc.Driver
spring.datasource.primary.url = jdbc:mysql://127.0.0.1/book?useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.primary.username = root
spring.datasource.primary.password = 123456
spring.datasource.primary.initialization-mode=always

spring.datasource.second.driverClassName = com.mysql.jdbc.Driver
spring.datasource.second.url = jdbc:mysql://127.0.0.1/newBook?useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.second.username = root
spring.datasource.second.password = 123456
spring.datasource.second.initialization-mode=always

mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

映射器目录

enter image description here

项目开始

@SpringBootApplication( exclude = {
        DataSourceAutoConfiguration.class, 
        DataSourceTransactionManagerAutoConfiguration.class
})
@EnableTransactionManagement
public class BookSystemApplication {
}

数据源配置

@Configuration
public class DataSourceConfig {
    @Bean(name = "primaryDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.second")
    public DataSource secondDataSource() {
        return DataSourceBuilder.create().build();
    }
}

主要数据源

@Configuration
@MapperScan(basePackages = {"com.bookSystem.mapper"}, sqlSessionFactoryRef = "primarySqlSessionFactory")
public class MyBatisPrimaryDbConfig {
    @Autowired
    @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;

    @Bean
    public SqlSessionFactory primarySqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(primaryDataSource);
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate primarySqlSessionTempmlate() throws  Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(primarySqlSessionFactory());
        return template;
    }
}

交易

@Configuration
public class TransactionConfig {
    @Autowired
    @Qualifier("primaryDataSource")
    private DataSource primary;

    @Autowired
    @Qualifier("secondDataSource")
    private DataSource second;

    @Bean(name="primaryTx")
    @Primary
    public PlatformTransactionManager primaryTransaction() {
        return new DataSourceTransactionManager(primary);
    }

    @Bean(name="secondTx")
    public PlatformTransactionManager secondTransaction() {
        return new DataSourceTransactionManager(second);
    }
}

UserDao

package com.bookSystem.mapper;

@Mapper
public interface UserDao {
    public User findByDomain(String domainName);
}

userMapper.xml,使用主要数据源

<mapper namespace = "com.bookSystem.mapper.UserDao">
    <select id="findByDomain" resultMap= "userResultMap">
        SELECT
            u.userId as user_user_id,
            u.userName as user_user_name,
            u.domainName as user_domain_name,
            u.department as user_department,
            u.title as user_title,
            u.date as user_date,
            u.password as user_password,
            u.accountRole as user_account_role
        FROM USER u
        WHERE u.domainName = #{domainName} 
    </select>

UserServiceImp,使用主要数据源。导致异常的服务层

@Service("UserService")
public class UserServiceImp implements UserService {
    @Override
    public User findByDomain(String domainName) {
        User user = userDao.findByDomain(domainName);
        return user;
    } 
}

0 个答案:

没有答案