首页 > 编程语言 > 详细

springboot mybatis使注解和xml两种方式同时生效

时间:2019-11-18 10:16:02      阅读:97      评论:0      收藏:0      [点我收藏+]

声明:该博客参考了:https://www.jianshu.com/p/53762ac6d31c

如果上面这个博客中的内容已经解决了你的问题,那就不用往下看了,如何按照上面的配置一直报这个异常:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

请参考如下博客解决:https://blog.csdn.net/softwarehe/article/details/8889206

如果仍然无法解决,可以尝试下面的方法,这也是我写这篇博客的目的。

一般在项目中都会使用springboot,如果需要使用mysql,一般都会使用阿里的druid数据库连接池,那使用这个连接池的时候,一般都会对druid做一些配置,有的人喜欢在yml中直接配置了,但是有些人可能在程序中搞一个配置类:类似于下面这种(没有全部贴出来,太长):

@Configuration
@MapperScan(basePackages = "com.gbgg.graph.goods.mapper", sqlSessionTemplateRef = "sqlSessionTemplate")
public class DuridConfig {
	@Value("${spring.datasource.primary.url:#{null}}")
	private String dbUrl;
	@Value("${spring.datasource.primary.username: #{null}}")
	private String username;
	@Value("${spring.datasource.primary.password:#{null}}")
	private String password;
	@Value("${spring.datasource.primary.driverClassName:#{null}}")
	private String driverClassName;
	@Value("${spring.datasource.initialSize:#{null}}")
	private Integer initialSize;
	@Value("${spring.datasource.minIdle:#{null}}")
	private Integer minIdle;
	@Value("${spring.datasource.maxActive:#{null}}")
	private Integer maxActive;
	@Value("${spring.datasource.maxWait:#{null}}")
	private Integer maxWait;
	@Value("${spring.datasource.timeBetweenEvictionRunsMillis:#{null}}")
	private Integer timeBetweenEvictionRunsMillis;
	@Value("${spring.datasource.minEvictableIdleTimeMillis:#{null}}")
	private Integer minEvictableIdleTimeMillis;
	@Value("${spring.datasource.validationQuery:#{null}}")
	private String validationQuery;
@Bean(name = "jdbcTemplate")@Primary
public JdbcTemplate jdbcTemplate() {
   return new JdbcTemplate(dataSource());
}

@Bean(name = "transactionManager")
@Primary
public DataSourceTransactionManager transactionManager() {
   return new DataSourceTransactionManager(dataSource());
}

@Bean(name = "sqlSessionFactory")
@Primary
public SqlSessionFactory setSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

这个配置类中自己new了一个SqlSessionFactoryBean,这就会导致在yml文件中配置的xml路径根本不起作用,也就是说根本找不到xml文件,所以上面的异常就会一直报。那怎么解决呢?

就是在在即new 的这个SqlSessionFactoryBean中,把xml路径给指定了就可以了,那怎么指定呢,看下面代码:

@Bean(name = "sqlSessionFactory")
    @Primary
    public SqlSessionFactory setSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
		configuration.setCallSettersOnNulls(true);
		bean.setConfiguration(configuration);
		bean.setVfs(SpringBootVFS.class);
        //下面这两个就是指定xml路径的
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		bean.setMapperLocations(resolver.getResources("classpath*:/mapper/*.xml"));
                bean.setDataSource(dataSource);
        return bean.getObject();
    }

 好了,以上就是这个问题的解决办法,但有个问题,为什么这里自己new了一个SqlSessionFactoryBean之后,yml文件中配置的就不起作用了呢?这个还在研究,等以后更新,如果有那位大佬知道原理,也麻烦在评论中告知。

 

 

springboot mybatis使注解和xml两种方式同时生效

原文:https://www.cnblogs.com/gunduzi/p/11867061.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!