Mybatis-Plus basic crud

Table of contents

1. Add

1. Add

2. Modification

1. Modify according to ID

 2. Modify according to conditions

3. Delete

1. Delete based on id

2. Delete deleteByMap according to conditions

 3. Batch deletion based on ID

4. Delete based on conditions delete

4. Inquiry

1. Query based on id

2. Batch query based on ID

 3. Query a piece of data

4. Query the number of data items

 5. Query the total number of data items based on conditions

6. Query collections based on conditions

7. Paging query


1. Add

1. Add

 //添加
    @Test
    void insert() {
        User user = new User();
        user.setAge(20);
        user.setEmail("[email protected]");
        user.setName("小黑子");
        user.setPassword("ctrl");
        user.setUserName("haha");
        // //返回的result是受影响的行数,并不是自增后的id
        int result = userMapper.insert(user);
        System.out.println(result);
    }

2. Modification

1. Modify according to ID

//根据id修改
    @Test
    void updateById() {
        User user = new User();
        user.setId(1L);
        user.setAge(22);
        user.setName("坤坤");
        int result = userMapper.updateById(user);
        System.out.println(result);

    }

 2. Modify according to conditions

//根据条件修改
    @Test
    void update() {
        User user = new User();
        user.setAge(22);
        user.setName("坤坤");
        user.setPassword("1111");

        //添加条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //根究 字段user_name 值为zhangsan进行更新操作
        wrapper.eq("user_name","zhangsan");
        //根据条件更新
        int result = userMapper.update(user,wrapper);
        System.out.println(result);

    }

3. Delete

1. Delete based on id

 //根据id删除
    @Test
    void deleteById() {
        //根据id删除
        int result = userMapper.deleteById(2L);
        System.out.println(result);

    }

2. Delete deleteByMap according to conditions

 //根据条件删除
    @Test
    public void testDeleteByMap() {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("age",20);
        columnMap.put("user_name","haha");
        //将columnMap中的元素设置为删除的条件,多个之间为and关系
        int result = this.userMapper.deleteByMap(columnMap);
        System.out.println("result = " + result);
    }

 3. Batch deletion based on ID

 //根据id批量删除
    @Test
    public void deleteBatchIds() {

        //根据id批量删除
        int result = this.userMapper.deleteBatchIds(Arrays.asList(6L,7L));
        System.out.println("result = " + result);
    }

4. Delete based on conditions delete

//删除delete
    @Test
    public void delete() {

        //方式一
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name","11")
                .eq("password","11");

        //方式二
        User user = new User();
        user.setUserName("11");
        user.setPassword("11");
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>(user);
        //根据id批量删除
        int result = this.userMapper.delete(queryWrapper1);
        System.out.println("result = " + result);
    }

4. Inquiry

1. Query based on id

 //根据id查询
    @Test
    public void selectById() {
        //根据id查询
        User user = this.userMapper.selectById(1L);
        System.out.println("result = " + user);
    }

2. Batch query based on ID

 //根据id批量查询
    @Test
    public void selectBatchIds() {
        //根据id批量查询
        List<User> userList = this.userMapper.selectBatchIds(Arrays.asList(1L, 3L));
        System.out.println("result = " + userList);
    }

 3. Query a piece of data

 //查询一条数据
    @Test
    public void selectOne() {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name","11");
        //查询一条,如果多余1条会报异常
        User user = this.userMapper.selectOne(queryWrapper);
        System.out.println("result = " + user);
    }

4. Query the number of data items

//查询数据条数
    @Test
    public void selectCount1() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //查询数据总条数
        Integer integer = this.userMapper.selectCount(queryWrapper);
        System.out.println("result = " + integer);
    }

 5. Query the total number of data items based on conditions

 //根据条件查询数据总条数
    @Test
    public void selectCount2() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("age",20);//查询年龄大于20的条数
        //根据条件查询数据总条数
        Integer integer = this.userMapper.selectCount(queryWrapper);
        System.out.println("result = " + integer);
    }

6. Query collections based on conditions

//根据条件查询集合
    @Test
    public void selectList() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("age",20);//查询年龄大于20的条数
        //根据条件查询集合
        List<User> userList = this.userMapper.selectList(queryWrapper);
        userList.forEach(System.out::println);
    }

7. Paging query

Create a paging plugin

@Configuration
@MapperScan("com.mybatisplus.mapper")
public class MybatisPlusConfig {
    @Bean
    public PaginationInterceptor  paginationInterceptor(){
        return new PaginationInterceptor();
    }
}

 Inquire

//分页查询
    @Test
    public void selectPage() {

        //创建分页对象
        Page<User> page = new Page<>(2,2);

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("email","itcast");//模糊查询

        IPage<User> iPage = this.userMapper.selectPage(page, queryWrapper);
        System.out.println("数据总条数:" + iPage.getTotal());
        System.out.println("数据总页数:" + iPage.getPages());
        System.out.println("当前页数:" + iPage.getCurrent());
        System.out.println("当前分页总页数:" + iPage.getSize());
        System.out.println("分页对象记录列表:" + iPage.getRecords());

        List<User> records = iPage.getRecords();

        //根据条件查询集合
        records.forEach(System.out::println);
    }

Guess you like

Origin blog.csdn.net/qi341500/article/details/132526487