Code Monkey home page Code Monkey logo

springboot-dynamicdatasource's Issues

java: 无法将类 java.lang.ThreadLocal<T>中的方法 withInitial应用到给定类型

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配置问题

mybatis.type-aliases-package=cn.com.hellowood.dynamicdatasource.mapper
这个是配置pojo扫描路径的,你配置到了mapper路径了,你已经配置了mapper.xml文件位置就不用配置mapper接口位置了,它回根据namespace 去找的。

where is the pom.xml

I want to check out this project.But I didn't find the pom.xml. Please help me ,thank u!

SqlSessionFactoryBean异常报错

你好,我写SqlSessionFactoryBean这个方法的时候一直说mapper.xml解析错误,不存在,无法创建Bean。
@bean
@ConfigurationProperties(prefix = "mybatis")
public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// 配置数据源,此处配置为关键配置,如果没有将 dynamicDataSource 作为数据源则不能实现切换
sqlSessionFactoryBean.setDataSource(dynamicDataSource());
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml"));
return sqlSessionFactoryBean;
}

但是写成如下方法又能切换数据源了,请问一下会是什么问题?
@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()]

但是查询的还是主库中的数据,我配置的一主一次从

yml文件中配置mybatis.mapper-locations,启动报错

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

Caused by: java.io.FileNotFoundException: class path resource [mapper/*.xml] cannot be opened because it does not exist

我pull代码下来运行并且运行了,代码逻辑很清晰,而且代码精炼。但是发现了个小问题,始终会有找不到mapper/*.xml,下面是原来的配置。
@bean
@ConfigurationProperties(prefix = "mybatis")
public SqlSessionFactoryBean sqlSessionFactoryBean() {
...........
return sqlSessionFactoryBean;
}
在查看mybatis官网内容,其实不需要向spring容器中注入SqlSessionFactoryBean,只需要注入SqlSessionFactory就行,就不会有上面的问题了。截图是mybatis官网的解释(地址:http://mybatis.org/spring/zh/factorybean.html)。
5

后期再追加多个数据库的问题

如果我想在这个多个数据库的springboot项目中,再追加多个数据库,能不能直接在配置文件yml中添加,而不用去改后台的java文件呢??谢谢

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.