mybatis-plus 增刪改查(普通)

1、Insert算法

主鍵生成策略數據庫

一、雪花算法(默認)mybatis

插入語句app

    @Test
    public void insertSql(){
        User user = new User();
        user.setName("wt");
        user.setAge(10);
        user.setEmail("1589574809@qq.com");
        userMapper.insert(user);
    }

二、自增ide

a、修改實現類spa

package com.wt.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

b、修改數據庫的字段it

id爲自增io

插入語句不變class

三、自定義插入數據email

a、

id取消自增

2、update

    @Test
    public void updateUser(){
        User user = new User();
        user.setId(1285437905490583554L);
        user.setName("tom");
        userMapper.updateById(user);
    }

3、select

@Test
    // 查詢經過ID
    public void selectByID(){
        User user = userMapper.selectById(1);
        System.out.println(user);
    }

    @Test
    // 查詢經過ids
    public void  selectBatchByIds(){
        List<User> userList = userMapper.selectBatchIds(Arrays.asList(1, 3, 5));
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    // 查詢經過 map
    public void selectByMap(){
        Map<String, Object> map = new HashMap<>();
        map.put("name", "Jack");
        List<User> userList = userMapper.selectByMap(map);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    // 查詢全部
    public void selectAll(){
        List<User> userList = userMapper.selectList(null);
        for (User user : userList) {
            System.out.println(user);
        }
    }

 4、delete

@Test
// 根據 ID 刪除
public void deleteById(){
    userMapper.deleteById(1);
}

@Test
// 根據 ID 批量刪除
public void deleteBatch(){
    userMapper.deleteBatchIds(Arrays.asList(1285400410916884481L, 1285400783777943555L));
}

@Test
// 根據 map 刪除
public void deleteByMap(){
    Map<String, Object> map = new HashMap<>();
    map.put("name", "tom");
    userMapper.deleteByMap(map);
}
相關文章
相關標籤/搜索