helloworlde / springboot-dynamicdatasource Goto Github PK
View Code? Open in Web Editor NEWDynamic datasource, multiple datasource of spring boot/ distribution transaction; spring boot 多数据源,动态数据源,分布式事务
License: Apache License 2.0
Dynamic datasource, multiple datasource of spring boot/ distribution transaction; spring boot 多数据源,动态数据源,分布式事务
License: Apache License 2.0
既然使用了Threadlocal,应该就可以不使用线程锁了吧
比如我有一个base库, 里面有一张表存放其他数据库的名称,用户,密码等,我想通过请求参数来来指定到其中的一个数据库,我应该怎么做?
private static final ThreadLocal CONTEXT_HOLDER = ThreadLocal.withInitial(DataSourceKey.master);
java: 无法将类 java.lang.ThreadLocal中的方法 withInitial应用到给定类型;
需要: java.util.function.Supplier<? extends S>
找到: org.poem.enumeration.DataSourceKey
原因: 无法推断类型变量 S
(参数不匹配; org.poem.enumeration.DataSourceKey无法转换为java.util.function.Supplier<? extends S>)
报这个错
mybatis.type-aliases-package=cn.com.hellowood.dynamicdatasource.mapper
这个是配置pojo扫描路径的,你配置到了mapper路径了,你已经配置了mapper.xml文件位置就不用配置mapper接口位置了,它回根据namespace 去找的。
I want to check out this project.But I didn't find the pom.xml. Please help me ,thank u!
但是写成如下方法又能切换数据源了,请问一下会是什么问题?
@bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dynamicDataSource());
//此处设置为了解决找不到mapper文件的问题
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml"));
return sqlSessionFactoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(sqlSessionFactory());
}
private final String[] QUERY_PREFIX = {"list"};
控制台打印
Switch DataSource to [slave] in Method [List com.weiyin.streetlamp.menu.dao.MenuDao.listAll()]
Restore DataSource to [master] in Method [List com.weiyin.streetlamp.menu.dao.MenuDao.listAll()]
但是查询的还是主库中的数据,我配置的一主一次从
http://localhost:9999/product/1 分别返回几个从库的数据
http://localhost:9999/product 返回主库的数据
Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'ServletContext resource [/classpath*:mappers/Mapper.xml]'; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/classpath:mappers/Mapper.xml]
at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:523)
at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:380)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1753)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1690)
... 55 common frames omitted
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/classpath:mappers/*Mapper.xml]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:159)
at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:519)
... 58 common frames omitted
大神好,主从针对读写,那主从数据库同步问题,您是如何解决的?
数据源1是只读的权限,数据源2是读写的权限。在@transactional的方法中切换数据,数据源切换的注解是在@service上添加的,并且注解切换数据源的操作也执行了,并且日志显示进行切换了。但是在执行sql时数据源还是没有切换。
需要实现两个数据库的操作在一个service中进行事务管理该怎么办呢
我pull代码下来运行并且运行了,代码逻辑很清晰,而且代码精炼。但是发现了个小问题,始终会有找不到mapper/*.xml,下面是原来的配置。
@bean
@ConfigurationProperties(prefix = "mybatis")
public SqlSessionFactoryBean sqlSessionFactoryBean() {
...........
return sqlSessionFactoryBean;
}
在查看mybatis官网内容,其实不需要向spring容器中注入SqlSessionFactoryBean,只需要注入SqlSessionFactory就行,就不会有上面的问题了。截图是mybatis官网的解释(地址:http://mybatis.org/spring/zh/factorybean.html)。
用master分支的代码,下载下来,无法切换数据库。 只能将Pointcut弄到service才可以,这是为什么?
@pointcut("execution( * cn.com.hellowood.dynamicdatasource.mapper..(..))")
spring mvc mybaits 怎样动态切换数据?能提供一下吗
配置文件里是设置了stat,wall,log4j,是否每个DruidDataSource的Filters列表都根据Filter的配置信息构建?还是只有Primary DataSource构建了?
如果我想在这个多个数据库的springboot项目中,再追加多个数据库,能不能直接在配置文件yml中添加,而不用去改后台的java文件呢??谢谢
好像你这个只能动态切换数据源啊,没有实现动态增减数据源
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.