泛型批量保存 Type class com.sun.proxy.$Proxy205 is not known to the MybatisPlusMapperRegistry.

7 篇文章 0 订阅

业务场景:旧版mybatis使用SqlSession手动提交事务

//BaseMapper 不能写成BaseMapper<T>,否则会出现下图错误
//警告:参数化类 'BaseMapper' 的原始使用。。。
public interface CommonMapper<T> extends BaseMapper {
    /**
     * 获取主键序列
     * @param count 查询数
     * @return 主键集合
     */
    Set<Long> selectIdsByNum(int count);

    /**
     * 保存实体
     * @param t t
     * @return 影响行数
     */
    int save(T t);
}

在这里插入图片描述

public class Test {
  //不能使用@Resource 否则Spring无法正确注入BaseMapper<T,ID>  无法识别
   @Autowired
   protected M baseMapper;
   
   void test(){
        SqlSession batchSqlSession =
        sqlSessionFactory.openSession(ExecutorType.BATCH, false)
   		batchSqlSession.getMapper(baseMapper.getClass()).save(t1);
   }
}
 ERROR [CommonSaveServiceImpl]  - [lambda$doSaveInit$0]   - Type class com.sun.proxy.$Proxy205 is not known to the MybatisPlusMapperRegistry.
org.apache.ibatis.binding.BindingException: Type class com.sun.proxy.$Proxy205 is not known to the MybatisPlusMapperRegistry.
	at com.baomidou.mybatisplus.MybatisMapperRegistry.getMapper(MybatisMapperRegistry.java:55)
	at com.baomidou.mybatisplus.MybatisConfiguration.getMapper(MybatisConfiguration.java:116)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:292)
//最终代码
Type[] genericInterfaces = AopUtils.getTargetClass(baseMapper).getGenericInterfaces();
Class<CommonMapper> mapper = (Class<CommonMapper>) genericInterfaces[0];
batchSqlSession.getMapper(mapper).save(t1);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值