解决Mybatis-Plus的联合主键问题

29 篇文章 0 订阅
5 篇文章 1 订阅

场景:使用com.baomidou.mybatis-plus进行批量新增或更新操作时,发现后台日志只根据一个主键进行更新,实体类将两个主键都加上@TableId时,运行报错。

解决:引入了com.github.jeffreyning.mybatisplus,下面贴出解决代码

实体类(主要是将@TableId都改为@TableFiled,并将主键字段加上@MppMultiId注解)

package com.bdsoft.dc.view.entity;

import com.baomidou.mybatisplus.annotation.TableName;

import java.math.BigDecimal;

import com.baomidou.mybatisplus.annotation.TableId;

import java.time.LocalDateTime;

import com.baomidou.mybatisplus.annotation.TableField;

import java.io.Serializable;

import com.github.jeffreyning.mybatisplus.anno.MppMultiId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.persistence.EmbeddedId;

/**
 * <p>
 * 视图头设定表
 * </p>
 *
 * @author wangshuai1
 * @since 2022-03-03
 */
@TableName("dc_view_header_user")
@ApiModel(value = "DcViewHeaderUser对象", description = "用户视图头设定表")
public class DcViewHeaderUser implements Serializable{

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "主键id")
    @MppMultiId
    @TableField("RECORDID")
    private String recordid;

    @ApiModelProperty(value = "用户id")
    @MppMultiId
    @TableField("USER_ID")
    private String userId;
}

Mapper(改为继承MppBaseMapper)

package com.bdsoft.dc.view.dao;

import com.bdsoft.dc.view.entity.DcViewHeaderUser;
import com.github.jeffreyning.mybatisplus.base.MppBaseMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.Set;

/**
 * <p>
 * 视图头设定表	 Mapper 接口
 * </p>
 *
 * @author wangshuai1
 * @since 2022-03-03
 */
@Repository
public interface DcViewHeaderUserMapper extends MppBaseMapper<DcViewHeaderUser> {



}

Service(继承IMppService)

package com.bdsoft.dc.view.service.view;

import com.bdsoft.dc.view.entity.DcViewHeaderUser;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.jeffreyning.mybatisplus.service.IMppService;

/**
 * <p>
 * 视图头设定表	 服务类
 * </p>
 *
 * @author wangshuai1
 * @since 2022-03-03
 */
public interface DcViewHeaderUserService extends IMppService<DcViewHeaderUser> {
    /**
}

实现类(继承MppServiceImpl)

package com.bdsoft.dc.view.service.impl.view;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.bdsoft.cache.service.UserSessionService;

import com.bdsoft.dc.view.dao.DcHeaderViewMapper;
import com.bdsoft.dc.view.entity.DcHeaderView;
import com.bdsoft.dc.view.entity.DcViewHeaderUser;
import com.bdsoft.dc.view.dao.DcViewHeaderUserMapper;
import com.bdsoft.dc.view.service.view.DcHeaderViewService;
import com.bdsoft.dc.view.service.view.DcViewHeaderUserService;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * <p>
 * 视图头设定表	 服务实现类
 * </p>
 *
 * @author wangshuai1
 * @since 2022-03-03
 */
@Service
public class DcViewHeaderUserServiceImpl extends MppServiceImpl<DcViewHeaderUserMapper, DcViewHeaderUser> implements DcViewHeaderUserService {

}

最后,记得项目的运行类需要加上@EnableMPP,开启MPP

@SpringBootApplication
//开启事务
@EnableTransactionManagement
//开启定时器
@EnableScheduling
@ServletComponentScan
@MapperScan(value = {"com.bdsoft.**.dao"}, nameGenerator = SpringBeanNameGenerator.class)
//扫描包com.bdsoft下bean,自定义BeanName生成策略, 解决不同包下同名类问题
@ComponentScan(basePackages = {"com.bdsoft"}, nameGenerator = SpringBeanNameGenerator.class)
@EnableAsync   //开启异步注解功能
//支持MPP
@EnableMPP
public class BDPWebApplication extends SpringBootServletInitializer {

}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值