mybatis-plus 增删改查(普通)

一、Insert

主键生成策略

1、雪花算法(默认)

插入语句

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

2、自增

a、修改实现类

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、修改数据库的字段

id为自增

插入语句不变

3、自定义插入数据

a、

id取消自增

二、update

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

三、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);
        }
    }

 四、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);
}

 

posted @ 2020-07-21 10:41  市丸银  阅读(5148)  评论(0编辑  收藏  举报