业务场景:旧版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);