首页 > 其他分享 >Mybatis-Flex之基础查询

Mybatis-Flex之基础查询

时间:2023-08-05 16:13:31浏览次数:47  
标签:Flex account age ACCOUNT 查询 accounts Mybatis tb id

1、selectOneById
/**
     * selectOneById(id):根据主键查询数据。
     */
    @Test
    public void testSelectOneById() {
        /**
         * SELECT * FROM `tb_account` WHERE `id` = ?
         */
        Account account = accountMapper.selectOneById(10L);
        System.out.println("account = " + account);
    }
2、selectOneByMap
/**
     * selectOneByMap(whereConditions):根据 Map 构建的条件来查询数据。查出满足条件的一条数据
     */
    @Test
    public void testSelectOneByMap() {
        Map<String, Object> condition = Map.of("age", 20);
        /**
         * SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `tb_account`.`user_name` = ? LIMIT 1
         */
        Account account = accountMapper.selectOneByMap(condition);
        Assertions.assertNotNull(account, "用户名不存在");
    }
3、selectOneByCondition
/**
     * selectOneByCondition(whereConditions):根据查询条件查询数据。
     */
    @Test
    public void testSelectOneByCondition() {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(ACCOUNT.AGE, ACCOUNT.ID, ACCOUNT.BIRTHDAY, ACCOUNT.USER_NAME)
                .from(ACCOUNT)
                .where(ACCOUNT.AGE.eq(20));
        /**
         * 查出多条记录会报错
         * SELECT `age`, `id`, `birthday`, `user_name` FROM `tb_account` WHERE `age` = ?
         */
        Account account = accountMapper.selectOneByQuery(wrapper);
        Assertions.assertNotNull(account);
    }
4、获取表名、列名
/**
     * 获取表名、列名
     */
    @Test
    public void testGetTableName() {
        // String schema = ACCOUNT.getSchema();
        // System.out.println("schema = " + schema);
        // 表名
        String tableName = ACCOUNT.getTableName();
        System.out.println("tableName = " + tableName);

        // 列名
        String columnName = ACCOUNT.AGE.getName();
        System.out.println("columnName = " + columnName);
    }
5、selectOneByQuery
/**
     * selectOneByQuery(queryWrapper):根据查询条件来查询 1 条数据。
     */
    @Test
    public void testSelectOneByQuery() {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(Account::getUserName)
                .from(ACCOUNT)
                .where(ACCOUNT.ID.eq(15));
        /**
         * SELECT `user_name` FROM `tb_account` WHERE `id` = ?
         */
        Account account = accountMapper.selectOneByQuery(wrapper);
        Assertions.assertNotNull(account);
    }
6、selectOneByQueryAs
/**
     * selectOneByQueryAs(queryWrapper, asType):根据查询条件来查询 1 条数据。
     */
    @Test
    public void testSelectOneByQueryAs() {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(Account::getUserName)
                .from(ACCOUNT)
                .where(ACCOUNT.ID.eq(10));
        /**
         * SELECT `user_name` FROM `tb_account` WHERE `id` = ?
         */
        AccountDTO accountDTO = accountMapper.selectOneByQueryAs(wrapper, AccountDTO.class);
        // accountDTO = AccountDTO(age=null, userName=UpdateEntity2)
        System.out.println("accountDTO = " + accountDTO);
        Assertions.assertNotNull(accountDTO);
    }
7、selectOneWithRelationsByMap
 /**
     * selectOneWithRelationsByMap(whereConditions):根据 Map 构建的条件来查询 1 条数据。
     */
    @Test
    public void testSelectOneWithRelationsByMap() {
        Map<String, Object> condition = Map.of("age", 20);
        /**
         * SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `tb_account`.`age` = ? LIMIT 1
         */
        Account account = accountMapper.selectOneWithRelationsByMap(condition);
        System.out.println("account = " + account);
        Assertions.assertNotNull(account);
    }
8、selectOneWithRelationsByCondition
/**
     * selectOneWithRelationsByCondition(whereConditions):根据查询条件查询 1 条数据。
     */
    @Test
    public void testSelectOneWithRelationsByCondition() {
        QueryCondition condition = ACCOUNT.ID.eq(10L);
        /**
         * SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `id` = ? LIMIT 1
         */
        Account account = accountMapper.selectOneWithRelationsByCondition(condition);
        Assertions.assertNotNull(account);
    }
9、selectOneWithRelationsByQuery
/**
     * selectOneWithRelationsByQuery(queryWrapper):根据查询条件来查询 1 条数据。
     */
    @Test
    public void testSelectOneWithRelationsByQuery() {
        QueryWrapper wrapper = QueryWrapper.create().select(Account::getId, Account::getUserName)
                .from(ACCOUNT)
                .where(ACCOUNT.ID.ge(6))
                .orderBy(Account::getBirthday)
                .asc()
                .limit(1);
        /**
         * SELECT `id`, `user_name` FROM `tb_account` WHERE `id` >= ? ORDER BY `birthday` ASC LIMIT 1
         */
        Account account = accountMapper.selectOneWithRelationsByQuery(wrapper);
        Assertions.assertNotNull(account);
    }
10、selectOneWithRelationsByQueryAs
/**
     * selectOneWithRelationsByQueryAs(queryWrapper, asType):根据查询条件来查询 1 条数据。
     */
    @Test
    public void testSelectOneWithRelationsByQueryAs() {
        QueryWrapper wrapper = QueryWrapper.create().select(ACCOUNT.DEFAULT_COLUMNS)
                .from(ACCOUNT)
                .where(ACCOUNT.ID.ge(6))
                .groupBy(ACCOUNT.ID)
                .having(ACCOUNT.AGE.le(30))
                .orderBy(Account::getBirthday)
                .desc()
                .limit(1);
        /**
         * SELECT `id`, `age`, `birthday`, `user_name` FROM `tb_account` WHERE `id` >= ? GROUP BY `id` HAVING `age` <= ? ORDER BY `birthday` DESC LIMIT 1
         */
        AccountDTO accountDTO = accountMapper.selectOneWithRelationsByQueryAs(wrapper, AccountDTO.class);
        Assertions.assertNotNull(accountDTO);
    }
11、selectListByIds
 /**
     * selectListByIds(ids):根据多个主键来查询多条数据。
     */
    @Test
    public void testSelectListByIds() {
        List<Integer> ids = List.of(2, 4, 6, 8, 10);
        /**
         * SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `id` = ? OR `id` = ? OR `id` = ? OR `id` = ? OR `id` = ?
         */
        List<Account> accounts = accountMapper.selectListByIds(ids);
        Assertions.assertTrue(accounts.size() > 0);
        System.out.println("accounts = " + accounts);
    }
12、selectListByMap
/**
     * selectListByMap(whereConditions):根据 Map 来构建查询条件,查询多条数据。
     */
    @Test
    public void testSelectListByMap() {
        // 注意这里的key要和数据库的列名column对应
        Map<String, Object> condition = Map.of("age", 20, "user_name", "zs");
        /**
         * SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `tb_account`.`user_name` = ? AND `tb_account`.`age` = ?
         */
        List<Account> accounts = accountMapper.selectListByMap(condition);
        Assertions.assertTrue(accounts.size() > 0);
        System.out.println("accounts = " + accounts);
    }
13、selectListByMap
/**
     * selectListByMap(whereConditions, count):根据 Map 来构建查询条件,查询多条数据。
     */
    @Test
    public void testSelectListByMapAndCount() {
        int count = 2;
        Map<String, Object> condition = Map.of("age", 20);
        /**
         * SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `tb_account`.`age` = ? LIMIT 2
         */
        List<Account> accounts = accountMapper.selectListByMap(condition, count);
        Assertions.assertEquals(2, accounts.size());
        System.out.println("accounts = " + accounts);
    }
14、selectListByCondition
/**
     * selectListByCondition(whereConditions):根据查询条件查询多条数据。
     */
    @Test
    public void testSelectListByCondition() {
        QueryCondition condition = ACCOUNT.ID.eq(5).and(ACCOUNT.AGE.ge(20));
        /**
         * SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `id` = ? AND `age` >= ?
         */
        List<Account> accounts = accountMapper.selectListByCondition(condition);
        Assertions.assertTrue(accounts.size() > 0);
        System.out.println("accounts = " + accounts);
    }
15、selectListByCondition
/**
     * selectListByCondition(whereConditions, count):根据查询条件查询多条数据。
     */
    @Test
    public void testSelectListByConditionAndCount() {
        QueryCondition condition = ACCOUNT.ID.eq(5).and(ACCOUNT.AGE.ge(20));
        /**
         * SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `id` = ? AND `age` >= ? LIMIT 2
         */
        List<Account> accounts = accountMapper.selectListByCondition(condition, 2);
        Assertions.assertEquals(2, accounts.size());
        System.out.println("accounts = " + accounts);
    }
16、selectListByQuery
 /**
     * selectListByQuery(queryWrapper):根据查询条件查询数据列表。
     */
    @Test
    public void testSelectListByQuery() {
        /**
         * SELECT * FROM `tb_account` WHERE id >= 10
         */
        List<Account> accounts = accountMapper.selectListByQuery(QueryWrapper.create().select(ACCOUNT.ALL_COLUMNS).from(ACCOUNT).where("id >= " + 10));
        Assertions.assertTrue(accounts.size() > 0);
    }
17、selectListByQuery
/**
     * selectListByQuery(queryWrapper, consumers):根据查询条件查询数据列表。
     */
    @Test
    public void testSelectListByQueryAndConsumers() {
        // 暂时没整明白consumers参数的用处
        Consumer<FieldQueryBuilder<Account>> consumer = (builder) -> {
            builder.nestedField(Account::getUserName);
        };
        /**
         * SELECT * FROM `tb_account` WHERE `age` >= ?
         */
        List<Account> accounts = accountMapper.selectListByQuery(QueryWrapper.create().select(ACCOUNT.ALL_COLUMNS)
                .from(ACCOUNT).where(ACCOUNT.AGE.ge(15)), consumer);
        Assertions.assertTrue(accounts.size() > 0);
        System.out.println("accounts = " + accounts);
    }
18、selectCursorByQuery
/**
     * selectCursorByQuery(queryWrapper):根据查询条件查询游标数据,该方法必须在事务中才能正常使用,非事务下无法获取数据。
     */
    @Transactional
    @Test
    public void testSelectCursorByQuery() {
        /**
         * SELECT * FROM `tb_account` WHERE `age` <= ?
         */
        Cursor<Account> accounts = accountMapper.selectCursorByQuery(QueryWrapper.create().select(ACCOUNT.ALL_COLUMNS).from(ACCOUNT).where(ACCOUNT.AGE.le(18)));
        accounts.forEach(item -> {
            System.out.println("item = " + item);
        });
        Assertions.assertNotNull(accounts);
    }
19、selectRowsByQuery
/**
     * selectRowsByQuery(queryWrapper):根据查询条件查询 Row 数据。
     */
    @Test
    public void testSelectRowsByQuery() {
        /**
         * SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `user_name` = ?
         */
        List<Row> rows = accountMapper.selectRowsByQuery(QueryWrapper.create().where(ACCOUNT.USER_NAME.eq("zs")));
        rows.stream().parallel().forEach(item -> {
            Object id = item.get("id");
            System.out.println("id = " + id);
            System.out.println("item = " + item);
        });
        Assertions.assertTrue(rows.size() > 0);
        System.out.println("rows = " + rows);
    }
20、selectListByQueryAs
/**
     * selectListByQueryAs(queryWrapper, asType):根据查询条件查询数据列表,要求返回的数据为 asType。这种场景一般用在 left join 时,有多出了实体类本身的字段内容,可以转换为 dto、vo 等场景
     */
    @Test
    public void testSelectListByQueryAs() {
        /**
         *SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE age >=20
         */
        List<AccountDTO> accountDTOS = accountMapper.selectListByQueryAs(QueryWrapper.create().where("age >=" + 20), AccountDTO.class);
        Assertions.assertTrue(accountDTOS.size() > 0);
        System.out.println("accountDTOS = " + accountDTOS);
    }
21、selectAll
/**
     * selectAll():查询全部数据。
     */
    @Test
    public void testSelectAll() {
        /**
         * SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account`
         */
        List<Account> accounts = accountMapper.selectAll();
        Assertions.assertTrue(accounts.size() > 0);
        System.out.println("accounts = " + accounts);
    }
22、selectObjectByQuery
 /**
     * selectObjectByQuery(queryWrapper):查询第一列返回的数据,QueryWrapper 执行的结果应该只有 1 列,例如:QueryWrapper.create().select(ACCOUNT.id).where(...);
     */
    @Test
    public void testSelectObjectByQuery() {
        String where = String.format("id = %d", 10);
        QueryWrapper wrapper = QueryWrapper.create().select(ACCOUNT.USER_NAME).where(where);
        /**
         *  SELECT `user_name` FROM `tb_account` WHERE id = 10
         */
        String userName = (String) accountMapper.selectObjectByQuery(wrapper);
        Assertions.assertNotNull(StringUtil.trimOrNull(userName));
        System.out.println("userName = " + userName);
    }
23、selectObjectByQueryAs
/**
     * selectObjectByQueryAs(queryWrapper, asType):查询第一列返回的数据,QueryWrapper 执行的结果应该只有 1 列,例如:QueryWrapper.create().select(ACCOUNT.id).where(...);
     */
    @Test
    public void testSelectObjectByQueryAs() {
        String where = String.format("id = %d", 10);
        /**
         * SELECT `user_name` FROM `tb_account` WHERE id = 10
         */
        QueryWrapper wrapper = QueryWrapper.create().select(ACCOUNT.USER_NAME).where(where);
        String res = accountMapper.selectObjectByQueryAs(wrapper, String.class);
        Assertions.assertNotNull(res);
        System.out.println("res = " + res);
    }
24、selectObjectListByQuery
/**
     * selectObjectListByQuery(queryWrapper):查询第一列返回的数据集合,QueryWrapper 执行的结果应该只有 1 列,例如:QueryWrapper.create().select(ACCOUNT.id).where(...);
     */
    @Test
    public void testSelectObjectListByQuery() {
        /**
         * SELECT `age` FROM `tb_account` WHERE `age` = ?
         */
        List<Object> objects = accountMapper.selectObjectListByQuery(QueryWrapper.create().select(ACCOUNT.AGE).where(ACCOUNT.AGE.eq(20)));
        Assertions.assertNotNull(objects);
        Assertions.assertTrue(objects.size() > 0);
        System.out.println("objects = " + objects);
    }
25、testSelectObjectListByQueryAs
/**
     * selectObjectListByQueryAs(queryWrapper, asType):查询第一列返回的数据集合,QueryWrapper 执行的结果应该只有 1 列,例如:QueryWrapper.create().select(ACCOUNT.id).where(...);
     */
    @Test
    public void testSelectObjectListByQueryAs() {
        /**
         * SELECT `user_name` FROM `tb_account` WHERE `age` = ?
         */
        List<String> userNameList = accountMapper.selectObjectListByQueryAs(QueryWrapper.create().select(ACCOUNT.USER_NAME).where(ACCOUNT.AGE.eq(20)), String.class);
        Assertions.assertNotNull(userNameList);
        Assertions.assertTrue(userNameList.size() > 0);
        System.out.println("userNameList = " + userNameList);
    }
26、selectCountByQuery
/**
     * selectCountByQuery(queryWrapper):查询数据量。
     */
    @Test
    public void testSelectCountByQuery() {
        /**
         * SELECT COUNT(*) FROM `tb_account` WHERE `id` > ?
         */
        long count = accountMapper.selectCountByQuery(QueryWrapper.create().select(ACCOUNT.ID));
        Assertions.assertTrue(count > 0);
        System.out.println("count = " + count);
    }
27、selectCountByCondition
/**
     * selectCountByCondition(whereConditions):根据条件查询数据总量。
     */
    @Test
    public void test() {
        long count = accountMapper.selectCountByCondition(ACCOUNT.ID.ge(0));
        Assertions.assertTrue(count > 0);
        System.out.println("count = " + count);
    }

标签:Flex,account,age,ACCOUNT,查询,accounts,Mybatis,tb,id
From: https://www.cnblogs.com/ybbit/p/17608060.html

相关文章

  • MySQL查询排序和分页
    连接数据库mysql-hlocalhost-uroot-proot排序查询语法:select字段列表from表名orderby字段1排序方式1,字段3排序方式2,字段3排序方式3,....;ASC:升序(默认值)DESC降序注意:如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序。根据年龄对公司......
  • 动力节点|MyBatis从入门实战到深入源码
    MyBatis是一种简单易用、灵活性高且高性能的持久化框架,也是Java开发中不可或缺的一部分。动力节点老杜的MyBatis教程,上线后广受好评从零基础小白学习的角度出发,层层递进从简单到深入,从实战到源码一步一案例,一码一实操,嘴对嘴指导MyBatis重点、难点、考点一网打尽不管你是小白还是正......
  • ERP-SQL查询
    --查询垫布的颜色/尺码--工作单物料需求-物料需求明细selectm.OrderNo,m.MatrClass,m.MatrCode,m.Color,m.Sizxfrommrjmdtlmwherem.MatrClass='DB'intodsResult1;--查询出料单中已出库且无单价的物料信息--出料单(每一笔出库数据)/物料名称/收货方/出库金额=0--......
  • web分页查询测试
    查询单条件查询输入框测试输入文本文本数据格式检查文本前面带空格文本中间带空格文本结尾带空格特殊字符&emoji查询清空查询条件返回结果检查各字段值检查返回的结果为空前端展示组合条件查询重置功能安全测试用户权限校验重要数据脱敏防止SQL注入......
  • mybatis-plus中的@Select注解里面写sql语句的in
    @Select("<script>"+"select\n"+"email\n"+"fromsys_user\n"+"whereidin\n"+"<foreachitem='item'index='index'collection='ids'open='(&......
  • mybatis-plus中的@Select注解里面写sql语句的in
    @Select("<script>"+    "select\n"+    "email\n"+    "fromsys_user\n"+    "whereidin\n"+    "  <foreachitem='item'index='index'colle......
  • mybatis-plus中的@Select注解里面写sql语句的in
    @Select("<script>"+    "select\n"+    "email\n"+    "fromsys_user\n"+    "whereidin\n"+    "  <foreachitem='item'index='index'colle......
  • MySQL查询聚合函数与分组查询
    连接数据库mysql-hlocalhost-uroot-proot聚合函数聚合函数:作用于某一列,对数据进行计算。ps:所有的null值是不参与聚合函数的运算的。06常见的聚合函数conut统计数量conut(*)统计整张表的数量max最大值min最小值avg平均值sum求......
  • springboot 集成druid 集成mybatise
    spring加载druid和mybatisepom依赖<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=&quo......
  • h index查询
     https://www.scopus.com/results/authorNamesList.uri?sort=count-f&src=al&sid=cbe762454a53aa5a9b57dc7da86b3644&sot=al&sdt=al&sl=44&s=AUTHLASTNAME%28Scheirer%29+AND+AUTHFIRST%28Walter%29&st1=Scheirer&st2=Walter&orcidId=&am......