Spring Boot + Mybatis 配置多数据源出错 20C

刚接触Spring Boot 不久,今天用 Spring Boot + Mybatis 按照网上的方法配置多数据源,
配置了多个DataConfig,并给其中一个打上了Primary注解,现在出现这样的问题:
给哪个数据源加Primary注解哪个数据源才能查询成功,其他的数据源一查就报异常,搞了好久不知道哪出的问题,各位大婶帮忙看看,下面是堆栈信息
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error invoking SqlProvider method (tk.mybatis.mapper.provider.ExampleProvider.dynamicSQL). Cause: java.lang.InstantiationException: tk.mybatis.mapper.provider.ExampleProvider
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:79)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:447)
at com.sun.proxy.$Proxy62.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:231)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:128)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:68)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
at com.sun.proxy.$Proxy67.selectByExample(Unknown Source)
at com.hworld.clp.component.companysetting.CompanySettingComponentImpl.getVerifyCodeSettings(CompanySettingComponentImpl.java:38)
at com.hworld.clp.service.member.MemberServiceImpl.verifyCodeSetting(MemberServiceImpl.java:368)
at com.hworld.clp.service.member.MemberServiceImpl.sendVerifyCode(MemberServiceImpl.java:280)
at com.hworld.clp.controller.member.MemberController.sendVerifyCode(MemberController.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.ibatis.builder.BuilderException: Error invoking SqlProvider method (tk.mybatis.mapper.provider.ExampleProvider.dynamicSQL). Cause: java.lang.InstantiationException: tk.mybatis.mapper.provider.ExampleProvider
at org.apache.ibatis.builder.annotation.ProviderSqlSource.createSqlSource(ProviderSqlSource.java:102)
at org.apache.ibatis.builder.annotation.ProviderSqlSource.getBoundSql(ProviderSqlSource.java:73)
at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:292)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:434)
... 64 more
Caused by: java.lang.InstantiationException: tk.mybatis.mapper.provider.ExampleProvider
at java.lang.Class.newInstance(Class.java:427)
at org.apache.ibatis.builder.annotation.ProviderSqlSource.createSqlSource(ProviderSqlSource.java:85)
... 74 more
Caused by: java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.ExampleProvider.()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 75 more

2个回答

你是否一个接口多个实现类?如果是Primary要加上这是告诉spring要选择哪个具体的实现方法

json_wf
json_wf 调试了一下,使用Primary注解的库会使用 DynamicSqlSource,非Primary的库使用ProviderSqlSource
大约 2 年之前 回复
json_wf
json_wf 我是配置的多个数据源,没有实现任何接口。都是通过注解实现的
大约 2 年之前 回复

package com.css.nsfw.dp.config.ds;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DataSourceConfig {

@Bean(name = "armDataSource")
@ConfigurationProperties(prefix = "arm.datasource")
// application.properteis中对应属性的前缀
public DataSource dataSource1() {
    return DataSourceBuilder.create().build();
}

//
@Bean(name = "systemDataSource")
@ConfigurationProperties(prefix = "system.datasource")
// application.properteis中对应属性的前缀
public DataSource dataSource2() {
    return DataSourceBuilder.create().build();
}

@Bean(name = "wtDataSource")
@ConfigurationProperties(prefix = "wt.datasource")
// application.properteis中对应属性的前缀
public DataSource dataSource3() {
    return DataSourceBuilder.create().build();
}

@Bean(name = "localDataSource")
@ConfigurationProperties(prefix = "local.datasource")
// application.properteis中对应属性的前缀
public DataSource dataSource10() {
    return DataSourceBuilder.create().build();
}

}

package com.css.nsfw.dp.config.ds;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
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;

@Configuration
@MapperScan(basePackages = {"com.css.nsfw.dp.dao.arm"}, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDbArmConfig {

@Autowired
@Qualifier("armDataSource")
private DataSource armDataSource;


@Bean
public SqlSessionFactory sqlSessionFactory1() throws Exception {
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    factoryBean.setDataSource(armDataSource); // arm数据库
    factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()  
    .getResources("classpath:mapper/arm/*.xml"));  
    return factoryBean.getObject();

}

@Bean
public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
    SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1()); // 使用上面配置的Factory
    return template;
}

}

package com.css.nsfw.dp.config.ds;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
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;

@Configuration
@MapperScan(basePackages = {"com.css.nsfw.dp.dao.local"}, sqlSessionFactoryRef = "sqlSessionFactory0")
public class MybatisDbLocalConfig {

@Autowired
@Qualifier("localDataSource")
private DataSource localDataSource;


@Bean
public SqlSessionFactory sqlSessionFactory0() throws Exception {
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    factoryBean.setDataSource(localDataSource); // local数据库
    factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()  
    .getResources("classpath:mapper/local/*.xml"));  
    return factoryBean.getObject();

}

@Bean
public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
    SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory0()); // 使用上面配置的Factory
    return template;
}

}

三个类 后面两个是数据源
下面是配置

local 数据源配置

local:
datasource:
url: jdbc:oracle:thin:@localhost:1521:orcl
username: wmhuang
password: wmhuang
driverClassName: oracle.jdbc.driver.OracleDriver

arm 数据源配置

arm:
datasource:
url: jdbc:oracle:thin:@78.12.73.134:1521:inspur
username: arm_jhj
password: arm_jhj
driverClassName: oracle.jdbc.driver.OracleDriver

json_wf
json_wf 我就是这样设置的,但还是不行,查询非Primary库时就会报错。你那边方便远程帮我看一下吗?可以加币
大约 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐