情况1:没有重新创建SqlSessionFactory的Bean
解决:在配置文件中增加mybatis-puls的mapper扫描路径即可
# MyBatis Plus 的配置项
mybatis-plus:
configuration:
map-underscore-to-camel-case: true # 虽然默认为 true ,但是还是显示去指定下。
mapper-locations: classpath*:mapper/**/*Mapper.xml
情况2: 如果自己在Config配置类(例如MybatisConfig.java)中重新创建了Bean(SqlSessionFactory)
解决: 将SqlSessionFactoryBean替换为:MybatisSqlSessionFactoryBean
@Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage"); String mapperLocations = env.getProperty("mybatis.mapperLocations"); String configLocation = env.getProperty("mybatis.configLocation"); typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage); VFS.addImplClass(SpringBootVFS.class); final MybatisSqlSessionFactoryBean sessionFactory2 = new MybatisSqlSessionFactoryBean(); sessionFactory2.setDataSource(dataSource); sessionFactory2.setTypeAliasesPackage(typeAliasesPackage); sessionFactory2.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ","))); sessionFactory2.setConfigLocation(new DefaultResourceLoader().getResource(configLocation)); return sessionFactory2.getObject(); /*final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setTypeAliasesPackage(typeAliasesPackage); sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ","))); sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation)); return sessionFactory.getObject();*/ }
文章讲述了在SpringBoot集成MyBatisPlus时遇到的两种情况及解决方案。情况1是未配置mybatis-puls的mapper扫描路径,解决方法是在配置文件中添加相应路径。情况2是自定义了Config配置类中的SqlSessionFactoryBean,解决方案是将其替换为MybatisSqlSessionFactoryBean以正确初始化。
6667

被折叠的 条评论
为什么被折叠?



