3.复杂查询,删除和逻辑删除

1.复杂查询

 //测试查询
    @Test
    public void select(){
        //通过id查询
        User user = userMapper.selectById(1L);
        System.out.println(user);
        //多个id查询
        List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
        users.forEach(System.out::println);
        //条件查询map
        HashMap<String, Object> map = new HashMap<>();
        //自定义查询 名字为小吴2并且年龄为3
        map.put("name","小吴2");
        map.put("age",3);
        List<User> users1 = userMapper.selectByMap(map);
    }
//分页查询
    @Test
    public void selectByPage(){
        Page<User> page=new Page<>(2,5);//第二页显示五条数据
        userMapper.selectPage(page,null);
        page.getRecords().forEach(System.out::println);
        System.out.println(page.getTotal());//获得总页 除此之外还有很多的页面的参数
    }
@SpringBootTest
public class WrapperTest {
    @Autowired
    private UserMapper userMapper;//继承了baseMapper所有的方法都来自于父类,也可以自己扩展
    @Test
    void contextLoads() {
        //参数是一个wrapper,条件构造器,用于复杂查询
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        QueryWrapper<User> wrapper1 = new QueryWrapper<>();
        QueryWrapper<User> wrapper2 = new QueryWrapper<>();
        QueryWrapper<User> wrapper3 = new QueryWrapper<>();
        QueryWrapper<User> wrapper4 = new QueryWrapper<>();
        QueryWrapper<User> wrapper5 = new QueryWrapper<>();
        //名字和邮箱不为空,并且年龄大于等于2
        wrapper.isNotNull("name").isNotNull("email").ge("age",2);
        //查询名字为小吴的
        wrapper1.eq("name","小吴");
        //查询年龄在1-3之间的用户(不包括1,3)
        wrapper2.between("age",1,3);
        //名字没有小 左和右 就是%6和6% 6结尾和6开头
        wrapper3.notLike("name","小").likeRight("email","6");
        //子查询 Preparing: SELECT id,name,age,email,version,create_time,update_time,
        // deleted FROM user WHERE deleted=0 AND (id IN (select id from user where id < 3))
        wrapper4.inSql("id","select id from user where id < 3");
        //通过id进行降序 升序是ASC
        wrapper5.orderByDesc("id");
        val users = userMapper.selectList(wrapper3);
        //查询出对象
        List<Object> list = userMapper.selectObjs(wrapper4);
        //users.forEach(System.out::println);
        list.forEach(System.out::println);
    }
}

 

2.删除

    //删除
    @Test
    public void delete(){
        userMapper.deleteById(1408045409112195076L);
        //批量删除
        userMapper.deleteBatchIds(Arrays.asList(1408045409112195075L,1408045409112195074L));
        //条件删除
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","吴某");
        userMapper.deleteByMap(map);
        userMapper.deleteById(1);
    }

3.逻辑删除

3.1 在数据库添加此字段

3.复杂查询,删除和逻辑删除

3.2 在pojo类上添加注解

3.复杂查询,删除和逻辑删除

 

3.复杂查询,删除和逻辑删除

上一篇:#!/usr/bin/env node


下一篇:echarts系列-样式调整总结