错误描述:
引入mybatis-plus后,调用mapper接口的方法,报错:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.chenliang.dao.TbRoleMapper.deleteByPrimaryKey
环境:
-
之前用的是mybaits(持久层框架)+github.pagehelper(分页)
-
后来,想改成mybatis-plus,以便于简化开发;但是分页组件暂时还是采用github.pagehelper,而没有使用mybatis-plus带的分页功能;所以暂时没有移除这个依赖:
<!--分页github.pagehelper--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> <exclusions> <exclusion> <artifactId>mybatis</artifactId> <groupId>org.mybatis</groupId> </exclusion> </exclusions> </dependency>
原因:
mybatis-plus需要使用特定的SqlSessionFactory:MybatisSqlSessionFactoryBean,
本来如果没有引入github.pagehelper,那么正常引入Mybatis-plus会创建MybatisSqlSessionFactoryBean并交给spring容器管理;之后也无需我们来写配置类,但是因为引入了这个依赖:pagehelper-spring-boot-starter,导致这个依赖创建的SqlSessionFactoryBean交给Spring容器管理,所以Mybatis-Plus就没有办法获取MybatisSqlSessionFactoryBean对象,而是获取到了SqlSessionFactoryBean这个对象;
解决:
手动配置MybatisSqlSessionFactoryBean
代码如下:
package com.chenliang.confg;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.autoconfigure.SpringBootVFS;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import javax.sql.DataSource;
import java.io.IOException;
@Configuration
@MapperScan(basePackages = "com.chenliang.dao")
public class MybatisPlusConfig {
@Autowired
private DataSource dataSource;
@Autowired
private MybatisPlusProperties properties;
@Autowired
private ResourceLoader resourceLoader = new DefaultResourceLoader();
@Autowired(required = false)
private Interceptor[] interceptors;
@Autowired(required = false)
private DatabaseIdProvider databaseIdProvider;
@Bean
public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() throws IOException {
MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
mybatisPlus.setDataSource(dataSource);
mybatisPlus.setVfs(SpringBootVFS.class);
String configLocation = this.properties.getConfigLocation();
if(StringUtils.isNotEmpty(configLocation)){
mybatisPlus.setConfigLocation(this.resourceLoader.getResource(configLocation));
}
mybatisPlus.setConfiguration(properties.getConfiguration());
mybatisPlus.setPlugins(interceptors);
MybatisConfiguration mc = new MybatisConfiguration();
mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
mc.setMapUnderscoreToCamelCase(false);// 数据库和java都是驼峰,就不需要
mybatisPlus.setConfiguration(mc);
if (this.databaseIdProvider != null) {
mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
}
mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
// 设置mapper.xml文件的路径
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resource = resolver.getResources("classpath:dao/*.xml");
mybatisPlus.setMapperLocations(resource);
return mybatisPlus;
}
}