Mybatis-Plus報Invalid bound statement (not found) 常見的錯誤原因
1,xml中的 namespace 標簽
<mapper namespace="com.demo.dao.DemoJavaMapper">
確認這個路徑可以找到JAVA類文件,正確的路徑
2,確認Mapper類的方法和xml文件中的方法名稱,參數列表相同,返回類型相同
<select id="selectByNameAndSex" resultMap="BaseResultMap">
DemoJava selectByNameAndSex(@Param("name") String name,@Param("sex") String sex);
3,是否重復掃描,啟動類和數據源配置 類掃描重復
@SpringBootApplication
@MapperScan(basePackages="com.demo.dao")
@EnableScheduling
public class SpringBootApplication{
}
@MapperScan(basePackages = "com.demo.dao")
public class DataSourceConfig {
}
4,JAVA類是否有標明主鍵,使用注解 @TableId
@Data
public class DemoJava implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
}
5,SqlSessionFactory不要使用原生的,請使用MybatisSqlSessionFactor
@MapperScan(basePackages = "com.demo.dao")
public class DataSourceConfig {
@Bean(name = "sqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource datasource)
throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
return bean.getObject();
}
}