把多个项目合并到同一个项目,有个SQL写在Mapper.java里有的写在xml里。
运行时报错Invalid bound statement (not found)。读取不到xml中的SQL,但是Mapper中的SQL是可以运行的。就读取xml中的报错,很是费解。
试了很多很方法。例:classpath*:mapper/*.xml。
网上说的所有我都试了不管用:
1、namespace对应一致。
2、xml的id要和Mapper.java的方法名一致。
3、如果放在java目录下要配置pom文件里的resource,要不然maven打包不到。
4、把对应的xml文件删除行空格。(我觉得最灵异以及最可能成功的一个方法)。
5、mybatis-plus的配置会覆盖mybatis的配置。
6、resource目录创建文件夹的规则不同。以mapper.itm这种是不能分层的,读取是 是读取不到的。要一层一层创建文件夹或者mapper/item这种。
7、多数据源的MapperLocation,在application.yml(后缀properties这种都是一样的)配置文件中是不起作用的。要在数据源配置文件中设置setMapperLocations。这也是我遇到的问题。
org.springframework.core.io.Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml");
bean.setMapperLocations(resources);
@Bean
@Primary
public SqlSessionFactory humanSqlSessionFactory(@Qualifier("humanDataSource") DataSource ds) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(ds);
org.springframework.core.io.Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml");
bean.setMapperLocations(resources);
//org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
//configuration.setLogImpl(StdOutImpl.class);
//bean.setConfiguration(configuration);
return bean.getObject();
}



