我们使用若依框架,首先这个框架完全开源,真的是很牛也很友好的框架。不像芋道各种关注各种收费,看个文档还要加入199的知识星球,真是恶心,个人觉得完全没必要,若依提供的扩展项目中都做了集成,需要哪一块自己复制过来。
先看报错:
2023-09-25 17:20:56 [pool-5-thread-1] ERROR c.e.c.c.s.m.o.u.t.HandleThread [] [] - 执行错误:{}
org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: java.sql.SQLSyntaxErrorException: Table 'gary_table.kc_20230925' doesn't exist
### The error may exist in URL [jar:file:/data/bin/gary/test/test-ob/test-ob.jar!/BOOT-INF/lib/test-ob-system-1.0.0.jar!/mapper/gary/testObMapper.xml]
### The error may involve com.gary.system.modules.outbound.mapper.testObMapper.batchInserttestOb-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO kc_20230925 (ID, TYPE, STATUS, RESULT, ATTEMPT, DAILY_FROM, DAILY_TILL, TZ_ID, CHAIN_ID, CH_N, PHONE, TYPE, CONTACT_INFO, CONTACT_INFO_TYPE, DATA_01, SW_ID, CAM_ID, DATA_02, DATA_04) VALUES (?, 2, 1, 0, ?, ?, ?, 112, ?, 0, ?, 1, ?, 1, ?, 101, ?, ?, ?)
### Cause: java.sql.SQLSyntaxErrorException: Table 'gary_table.kc_20230925' doesn't exist
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Table 'gary_table.kc_20230925' doesn't exist
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:91)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
at com.sun.proxy.$Proxy136.insert(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:272)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:59)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
at com.sun.proxy.$Proxy170.batchInserttestOb(Unknown Source)
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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
at com.gary.framework.aspectj.DataSourceAspect.around(DataSourceAspect.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
数据源切换失败试了好几种办法都不行,后来看了一篇文章,介绍需要加入下面这一段
@Transactional(propagation = Propagation.REQUIRES_NEW)
结果还是真管用了,这样就需要自己控制事务了。
那还有没有其他方法呢,之前也遇到过没仔细研究过,直接使用了若依官方提供的如下的方法也可以解决。
DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
下面是官方例子:
public List selectUserList(SysUser user)
{
//手动开启
DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
List userList = userMapper.selectUserList(user);
//手动关闭
DynamicDataSourceContextHolder.clearDataSourceType();
return userList;
}
也可以通过上面这段代码也可以生效。
参考官方文档为主,其他博主为辅,主要还是不断的尝试。 最后还要为若依这种开源系统大大的点赞。