mybatis 使用中的一些坑

最近接手了一个项目(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的注册信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值