最近接手了一个项目(mybatis-spring-boot),将打包方式由war改为jar,期间发现了一些小坑
1:mybatis config正确写法
@Configuration
@MapperScan("**.dao")//在dao中尽量用@Mapper来修饰,否则依赖jar中的dao扫描不到
public class MybatisConfig {
@Autowired private DataSource dataSource;
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setTypeAliasesPackage("**.model");
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//classpath* 扫描jar中xml
sessionFactory.setMapperLocations(resolver.getResources(“classpath*😗*/sqlmap/*.xml”)); return sessionFactory.getObject();
}
}
2:发现dao扫描报错
class path resource [xx] cannot be resolved to absolute file path because it does not reside
PathMatchingResourcePatternResolver的一个坑,有该异常的请参照上边的示例代码
3:依赖jar中的dao中没有使用@Mapper的一些异常
1:IllegalArgumentException: Result Maps collection already contains value for
发现异常类中并没有重复的resultMap,断点发现PathMatchingResourcePatternResolver。getResources方法中依赖的jar被扫描了两次,第二次注册jar中的xml时异常
2:BindingException: Invalid bound statement (not found): org.springframework.dao.support.PersistenceExceptionTranslator.translateExceptionIfPossible
mybatis sql异常没有打印具体sql异常而是报了一个很模糊的错误,断点发现MapperProxy.invoke 方法中没有取到dao中的具体xxDao的接口;mapperInterface没有该xxDao的注册信息