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

Mybatis-Flex之基础查询的更多相关文章

  1. Spring+SpringMVC+MyBatis+easyUI整合基础篇(八)mysql中文查询bug修复

    写在前面的话 在测试搜索时出现的问题,mysql通过中文查询条件搜索不出数据,但是英文和数字可以搜索到记录,中文无返回记录.本文就是写一下发现问题的过程及解决方法.此bug在第一个项目中点这里还存在, ...

  2. Spring+SpringMVC+MyBatis+easyUI整合基础篇

    基础篇 Spring+SpringMVC+MyBatis+easyUI整合基础篇(一)项目简介 Spring+SpringMVC+MyBatis+easyUI整合基础篇(二)牛刀小试 Spring+S ...

  3. SpringBoot集成Mybatis实现多表查询的两种方式(基于xml)

     下面将在用户和账户进行一对一查询的基础上进行介绍SpringBoot集成Mybatis实现多表查询的基于xml的两种方式.   首先我们先创建两个数据库表,分别是user用户表和account账户表 ...

  4. 三、mybatis多表关联查询和分布查询

    前言 mybatis多表关联查询和懒查询,这篇文章通过一对一和一对多的实例来展示多表查询.不过需要掌握数据输出的这方面的知识.之前整理过了mybatis入门案例和mybatis数据输出,多表查询是在前 ...

  5. MyBatis实现关联表查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  6. HQL基础查询语句

    HQL基础查询语句 1.使用hql语句检索出Student表中的所有列 //核心代码 @Test public void oneTest() { Query query=session.createQ ...

  7. MyBatis——实现关联表查询

    原文:http://www.cnblogs.com/xdp-gacl/p/4264440.html 一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创 ...

  8. MyBatis学习总结_13_Mybatis查询之resultMap和resultType区别

    MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性 ...

  9. MyBatis 多表联合查询,字段重复的解决方法

    MyBatis 多表联合查询,两张表中字段重复时,在配置文件中,sql语句联合查询时使用字段别名,resultMap中对应的column属性使用相应的别名: <resultMap type=&q ...

  10. Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM

    写在前面的话   承接前文<Spring+SpringMVC+MyBatis+easyUI整合基础篇(五)讲一下maven>,本篇所讲述的是如何使用maven与原ssm项目整合,使得一个普 ...

随机推荐

  1. 使用 OpenTelemetry 构建 .NET 应用可观测性(1):什么是可观测性

    目录 什么是系统的可观测性(Observability) 为什么需要软件系统需要可观测性 可观测性的三大支柱 日志(Logging) 指标(Metrics) 分布式追踪(Distributed Tra ...

  2. mpi转以太网连接200plc通信不上实际问题和解决方法

    西门子S7200plc通信不上实际问题和解决方法 现场通信的同学在现场调试的时候,现在特别是做项目改造的项目,西门子S7200plc通信面临淘汰,但是在改造的项目中还能经常看到他们的身影,下面我们就来 ...

  3. Python实现与MySQL长连接的客户端

    下面的代码是使用Python建立的和MySQL长连接的简单客户端示例. 当和MySQL的连接断开后,会自动进行重连(被动式的重连,即只有调用增self.execute().删self.execute( ...

  4. Redis系列之——使用常见问题

    文章目录 一 子进程开销和优化 二 fork操作 三 aof追加阻塞 一 子进程开销和优化 1 cpu 开销:rdb和aof文件生成,属于cpu密集型 优化:不做cpu绑定,不和cpu密集型的服务一起 ...

  5. jdk-14.0.1环境搭建及cmd环境编译执行

    1.安装包获取 https://www.oracle.com/java/technologies/javase/jdk14-archive-downloads.html 2.环境变量配置 最新版本的J ...

  6. Unexpected keys "@@dva", "user" found in preloadedState argument passed to createStore. Expected to find one of the known reducer keys instead: "router", "loading". Unexpected keys will be ignored.

    Please use `require("history").DOMUtils` instead of `require("history/DOMUtils") ...

  7. Go 复合类型之切片类型介绍

    Go 复合类型之切片类型 目录 Go 复合类型之切片类型 一.引入 二.切片(Slice)概述 2.1 基本介绍 2.2 特点 2.3 切片与数组的区别 三. 切片声明与初始化 3.1 方式一:使用切 ...

  8. 文心一言 VS 讯飞星火 VS chatgpt (110)-- 算法导论10.2 1题

    一.用go语言,单链表上的动态集合操作 INSERT 能否在 O(1)时间内实现?DELETE 操作呢? 文心一言: 在单链表中,INSERT 和 DELETE 操作的时间复杂度通常是 O(n),其中 ...

  9. 报错Intel MKL FATAL ERROR: Cannot load libmkl_core.so.的一种解决方法

    问题 今天上80服务器跑mdistiller的代码时,意外发现torch.numpy都不能用了T_T 以torch为例,出现如下报错情况 以numpy为例,出现如下报错情况 我们先看看报错信息,这个报 ...

  10. 关于如何解决visualc++6.0打开文件闪退的一种方式(附带解决输入法无法显示)

    这里我把VisualC++6.0安装程序和filetool分享在我的网盘里面了 网盘下载QAQ 链接:https://pan.baidu.com/s/1azSMX_cOKgb64WT7-gTdbQ?p ...