看bug发现 SqlSessionFactory使用原生的,应该使用MybatisSqlSessionFactory
解决方法:将mybatis的sqlSessionFactory替换成mybatis-plusd的MybatisSqlSessionFactoryBean,添加配置类MybatisPlusConfig
注意要把yml中的 configLocation: classpath:mybatis/mybatis-config.xml去掉
因为已经再java类中配置了
原作者连接
链接:https://www.jianshu.com/p/609f9741567f#/
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
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.ResourceLoader;
import org.springframework.util.StringUtils;
import javax.sql.DataSource;
@Configuration
public class MybatisPlusConfig {
@Autowired
private DataSource dataSource;
@Autowired
private MybatisProperties properties;
@Autowired
private ResourceLoader resourceLoader = new DefaultResourceLoader();
@Autowired(required = false)
private Interceptor[] interceptors;
@Autowired(required = false)
private DatabaseIdProvider databaseIdProvider;
/** mybatis-plus分页插件 */
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor page = new PaginationInterceptor();
page.setDialectType("mysql");
return page;
}
/** 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定 <p> 配置文件和mybatis-boot的配置文件同步 @return */
@Bean
public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
mybatisPlus.setDataSource(dataSource);
mybatisPlus.setVfs(SpringBootVFS.class);
if (StringUtils.hasText(this.properties.getConfigLocation()))
mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
if (!ObjectUtils.isEmpty(this.interceptors)) mybatisPlus.setPlugins(this.interceptors);
MybatisConfiguration mc = new MybatisConfiguration();
mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
// 数据库字段设计为驼峰命名,默认开启的驼峰转下划线会报错字段找不到
mc.setMapUnderscoreToCamelCase(true);
mybatisPlus.setConfiguration(mc);
if (this.databaseIdProvider != null) mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
if (StringUtils.hasLength(this.properties.getTypeAliasesPackage()))
mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
if (StringUtils.hasLength(this.properties.getTypeHandlersPackage()))
mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations()))
mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
return mybatisPlus;
}
}