使用mybatis-plus
时不能使用自带的SqlSessionFactory
,要使用MybatisSqlSessionFactory
,在配置类中加入如下配置(springboot)
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
String typeAliasesPackage = env.getProperty("mybatisPlus.typeAliasesPackage");
String mapperLocations = env.getProperty("mybatisPlus.mapperLocations");
String configLocation = env.getProperty("mybatisPlus.configLocation");
typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
VFS.addImplClass(SpringBootVFS.class);
final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean ();
sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
sessionFactory.setPlugins(mybatisSqlInterceptor(),mybatisPlusInterceptor());
return sessionFactory.getObject();
}
看看