• MybatisPlus Warpper实现复杂查询


    MybatisPlus Warpper实现复杂查询

    关于博客

    [整合MybatisPlus测试]

    [MybatisPlus自动填充时间]

    [MybatisPlus乐观锁]

    [MybatisPlus物理删除、逻辑删除]

    [MybatisPlus性能分析插件]

    测试类 ComplexQuery

    package com.xiang;
    
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.xiang.mapper.UserMapper;
    import com.xiang.pojo.User;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.List;
    
    /**
     * Created by IntelliJ IDEA.
     * User: xiang
     * Date: 2021/10/25 0:17
     */
    @SpringBootTest
    public class ComplexQuery {
        @Autowired
        UserMapper userMapper;
    
        //创建QueryWrapper
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
    
        //方法
        //查所有wrapper
        void findAll() {
            List<User> list = userMapper.selectList(wrapper);
            for (User user : list) {
                System.out.println(user);
            }
        }
    
        /**
         * MybatisPlus Warpper实现复杂查询
         */
    
        /**
         * - ge:大于等于
         * - gt:大于
         * - le:小于等于
         * - lt:小于
         */
        @Test
        void queryGe() {
            wrapper.ge("age", 18);
            findAll();
        }
    
        @Test
        void queryGt() {
            wrapper.gt("version", 0);
            findAll();
        }
    
        @Test
        void queryLe() {
            wrapper.le("age", 18);
            findAll();
        }
    
        @Test
        void queryLt() {
            wrapper.lt("deleted", 1);
            findAll();
        }
    
        /**
         * - eq:等于
         * - ne:不等于
         */
    
        @Test
        void queryEq() {
            wrapper.eq("username", "向某");
            findAll();
        }
    
        @Test
        void queryNe() {
            wrapper.ne("username", "向某");
            findAll();
        }
    
        /**
         * - between:范围内的数据
         */
        @Test
        void queryBetween() {
            wrapper.between("age", 18, 21);
            findAll();
        }
    
        /**
         * - like:模糊查询
         */
        @Test
        void queryLike() {
            wrapper.like("username", "向");
            findAll();
        }
    
    
        /**
         * - orderByDesc:降序
         * - orderByAsc:升序
         */
        @Test
        void queryOrderByDesc() {
            wrapper.orderByDesc("age");
            findAll();
        }
    
        @Test
        void queryOrderByAsc() {
            wrapper.orderByAsc("age");
            findAll();
        }
    
        /**
         * - last:最后可以拼接sql语句
         */
        @Test
        void queryLast() {
            wrapper.last("limit 1,3");//分页查询(开始索引,每页展示条数)
            findAll();
        }
    
        /**
         * - select:指定要查询的列
         */
        @Test
        void querySelect() {
            wrapper.select("username", "age");
            findAll();
        }
    
    }
    
    
    

    运行结果

    ge:大于等于

    JDBC Connection [HikariProxyConnection@362030306 wrapping com.mysql.cj.jdbc.ConnectionImpl@29bcf51d] will not be managed by Spring
    ==>  Preparing: SELECT id,username,sex,age,birthday,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age >= ?
    ==> Parameters: 18(Integer)
    <==    Columns: id, username, sex, age, birthday, create_time, update_time, version, deleted
    <==        Row: 1, xiang, 男, 18, 2021-10-03, null, null, null, 0
    <==        Row: 559, 小向, 男, 18, 2021-10-04, null, null, null, 0
    <==        Row: 602, admin, 女, 18, 2021-10-20, null, null, null, 0
    <==        Row: 605, 小二, 女, 18, null, null, null, null, 0
    <==        Row: 607, 小四, 女, 21, null, null, null, null, 0
    <==        Row: 1452309953320730625, 小不点儿, null, 19, null, 2021-10-25 00:24:10, 2021-10-25 00:24:10, 1, 0
    <==      Total: 6
     Time:30 ms - ID:com.xiang.mapper.UserMapper.selectList
    Execute SQL:
        SELECT
            id,
            username,
            sex,
            age,
            birthday,
            create_time,
            update_time,
            version,
            deleted 
        FROM
            user 
        WHERE
            deleted=0 
            AND age >= 18
    

    gt:大于

    JDBC Connection [HikariProxyConnection@253171986 wrapping com.mysql.cj.jdbc.ConnectionImpl@7d4d8579] will not be managed by Spring
    ==>  Preparing: SELECT id,username,sex,age,birthday,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND version > ?
    ==> Parameters: 0(Integer)
    <==    Columns: id, username, sex, age, birthday, create_time, update_time, version, deleted
    <==        Row: 1452309953320730625, 小不点儿, null, 19, null, 2021-10-25 00:24:10, 2021-10-25 00:24:10, 1, 0
    <==      Total: 1
     Time:40 ms - ID:com.xiang.mapper.UserMapper.selectList
    Execute SQL:
        SELECT
            id,
            username,
            sex,
            age,
            birthday,
            create_time,
            update_time,
            version,
            deleted 
        FROM
            user 
        WHERE
            deleted=0 
            AND version > 0
    

    le:小于等于

    JDBC Connection [HikariProxyConnection@700249373 wrapping com.mysql.cj.jdbc.ConnectionImpl@1e54a6b1] will not be managed by Spring
    ==>  Preparing: SELECT id,username,sex,age,birthday,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND age <= ?
    ==> Parameters: 18(Integer)
    <==    Columns: id, username, sex, age, birthday, create_time, update_time, version, deleted
    <==        Row: 1, xiang, 男, 18, 2021-10-03, null, null, null, 0
    <==        Row: 559, 小向, 男, 18, 2021-10-04, null, null, null, 0
    <==        Row: 602, admin, 女, 18, 2021-10-20, null, null, null, 0
    <==        Row: 605, 小二, 女, 18, null, null, null, null, 0
    <==        Row: 609, 小五, 女, 0, null, null, null, null, 0
    <==        Row: 1451097869404868609, 向某, 男, 0, null, null, null, null, 0
    <==        Row: 1451098975287668738, 周某, 男, 0, null, null, null, null, 0
    <==      Total: 7
     Time:38 ms - ID:com.xiang.mapper.UserMapper.selectList
    Execute SQL:
        SELECT
            id,
            username,
            sex,
            age,
            birthday,
            create_time,
            update_time,
            version,
            deleted 
        FROM
            user 
        WHERE
            deleted=0 
            AND age <= 18
    

    lt:小于

    JDBC Connection [HikariProxyConnection@508864177 wrapping com.mysql.cj.jdbc.ConnectionImpl@f238e4f] will not be managed by Spring
    ==>  Preparing: SELECT id,username,sex,age,birthday,create_time,update_time,version,deleted FROM user WHERE deleted=0 AND deleted < ?
    ==> Parameters: 1(Integer)
    <==    Columns: id, username, sex, age, birthday, create_time, update_time, version, deleted
    <==        Row: 1, xiang, 男, 18, 2021-10-03, null, null, null, 0
    <==        Row: 559, 小向, 男, 18, 2021-10-04, null, null, null, 0
    <==        Row: 602, admin, 女, 18, 2021-10-20, null, null, null, 0
    <==        Row: 605, 小二, 女, 18, null, null, null, null, 0
    <==        Row: 607, 小四, 女, 21, null, null, null, null, 0
    <==        Row: 609, 小五, 女, 0, null, null, null, null, 0
    <==        Row: 1451097869404868609, 向某, 男, 0, null, null, null, null, 0
    <==        Row: 1451098975287668738, 周某, 男, 0, null, null, null, null, 0
    <==        Row: 1452309953320730625, 小不点儿, null, 19, null, 2021-10-25 00:24:10, 2021-10-25 00:24:10, 1, 0
    <==      Total: 9
     Time:26 ms - ID:com.xiang.mapper.UserMapper.selectList
    Execute SQL:
        SELECT
            id,
            username,
            sex,
            age,
            birthday,
            create_time,
            update_time,
            version,
            deleted 
        FROM
            user 
        WHERE
            deleted=0 
            AND deleted < 1
    

    等运行结果...

    数据库表内容

  • 相关阅读:
    javascript中的screen对象
    javascript的navigator对象
    Javascript的location对象
    javascript 关于闭包的知识点
    微信小程序教程(第三篇)
    微信小程序----关于变量对象data 和 前端wxml取后台js变量值
    微信小程序教程(第四篇)
    微信小程序教程(第二篇)
    微信小程序教程(第一篇)
    操作文件的方法
  • 原文地址:https://www.cnblogs.com/d534/p/15457239.html
Copyright © 2020-2023  润新知