1. 程式人生 > 其它 >mybatisplus根據多個欄位聯合主鍵進行增刪改查

mybatisplus根據多個欄位聯合主鍵進行增刪改查

技術標籤:mybatisplus聯合主鍵增刪改查多主鍵查詢

**根據多個欄位聯合主鍵增刪改查**
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"獲取下載密碼