**根据多个字段联合主键增删改查**
mybatisplus只支持一个主键,如何使mybatisplus支持对多个主键的增删改查?
mapper继承MppBaseMapper<br>
实体类中联合主键的字段需要用@MppMultiId注解修饰<br>
**从中央库引入jar**

    <dependency>
        <groupId>com.github.jeffreyning</groupId>
        <artifactId>mybatisplus-plus</artifactId>
        <version>1.2.0-RELEASE</version>
    </dependency>
**根据多个字段联合主键增删改查**
在实例类成员变量上使用@MppMultiId表明联合主键

@TableName("test07")
public class Test07Entity {
    @MppMultiId
    @TableField(value = "k1")
    private Integer k1;

    @MppMultiId
    @TableField(value = "k2")
    private String k2;
    
    @TableField(value = "col1")
    private String col1;
    @TableField(value = "col2")
    private String col2;    


mapper需要继承MppBaseMapper

@Mapper
public interface Test07Mapper extends MppBaseMapper<Test07Entity> {
}

根据多主键增删改查

    public void testMultiId(){
        //id
        Test07Entity idEntity=new Test07Entity();
        idEntity.setK1(1);
        idEntity.setK2("111");
        //del
        test07Mapper.deleteByMultiId(idEntity);
        //add
        test07Mapper.insert(idEntity);
        //query
        Test07Entity retEntity=test07Mapper.selectByMultiId(idEntity);
        retEntity.setCol1("xxxx");
        //update
        test07Mapper.updateByMultiId(retEntity);
    }

**demo下载**
mybatisplus-plus 1.2.0 示例工程下载地址
链接:https://pan.baidu.com/s/1k7vP8NYWhJtUF24lQOppFg

扫描订阅公众号,回复"plus"获取下载密码

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐