mybatis出现Invalid bound statement
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
出现错误
情景一Invalid bound statement (not found): **.dao.mapper.DataSetMapper.selectList
情景二 Caused by: org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 35
当情景一和情景二同时出现时可能存在以下可能性
可能一 没有读取到mapper文件
检查配置@MapperScan(basePackages = "com.xxx.mapper")
检查mybatis.mapper-locations: classpath:mapper/*.xml的配置路径是否正确
注意项目的target目录下是否有mapper.xml文件如果没有则要在pom.xml加入如下配置
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
可能二 mybatis puls的坑
使用mybatis-plus时不能使用自带的SqlSessionFactory要使用MybatisSqlSessionFactory
@Slf4j @Configuration @EnableTransactionManagement public class DataSourceConfig { @Autowired private Environment environment; @Autowired private DataSource mysqlDataSource; @Bean @ConfigurationProperties(prefix = "mybatis-plus.configuration") public MybatisConfiguration globalConfiguration() { return new MybatisConfiguration(); } @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean (); sqlSessionFactoryBean.setDataSource(mysqlDataSource); sqlSessionFactoryBean.setConfiguration(globalConfiguration()); sqlSessionFactoryBean.setTypeAliasesPackage(environment.getProperty("mybatis-plus.type-aliases-package")); sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(environment.getProperty("mybatis-plus.mapper-locations"))); return sqlSessionFactoryBean.getObject(); } }
数据库配置项
# Mybatis-plus配置 mybatis-plus: # 配置Mapper映射文件 mapper-locations: classpath*:/mapper/*Mapper.xml # 配置Mybatis数据返回类型别名(默认别名是类名) type-aliases-package: com.cserver.aischeduler.api.model.entity configuration: # 自动驼峰命名 map-underscore-to-camel-case: false #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl