1、selectOneById
@Test
public void testSelectOneById() {
Account account = accountMapper.selectOneById(10L);
System.out.println("account = " + account);
}
2、selectOneByMap
@Test
public void testSelectOneByMap() {
Map<String, Object> condition = Map.of("age", 20);
Account account = accountMapper.selectOneByMap(condition);
Assertions.assertNotNull(account, "用户名不存在");
}
3、selectOneByCondition
@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));
Account account = accountMapper.selectOneByQuery(wrapper);
Assertions.assertNotNull(account);
}
4、获取表名、列名
@Test
public void testGetTableName() {
String tableName = ACCOUNT.getTableName();
System.out.println("tableName = " + tableName);
String columnName = ACCOUNT.AGE.getName();
System.out.println("columnName = " + columnName);
}
5、selectOneByQuery
@Test
public void testSelectOneByQuery() {
QueryWrapper wrapper = QueryWrapper.create()
.select(Account::getUserName)
.from(ACCOUNT)
.where(ACCOUNT.ID.eq(15));
Account account = accountMapper.selectOneByQuery(wrapper);
Assertions.assertNotNull(account);
}
6、selectOneByQueryAs
@Test
public void testSelectOneByQueryAs() {
QueryWrapper wrapper = QueryWrapper.create()
.select(Account::getUserName)
.from(ACCOUNT)
.where(ACCOUNT.ID.eq(10));
AccountDTO accountDTO = accountMapper.selectOneByQueryAs(wrapper, AccountDTO.class);
System.out.println("accountDTO = " + accountDTO);
Assertions.assertNotNull(accountDTO);
}
7、selectOneWithRelationsByMap
@Test
public void testSelectOneWithRelationsByMap() {
Map<String, Object> condition = Map.of("age", 20);
Account account = accountMapper.selectOneWithRelationsByMap(condition);
System.out.println("account = " + account);
Assertions.assertNotNull(account);
}
8、selectOneWithRelationsByCondition
@Test
public void testSelectOneWithRelationsByCondition() {
QueryCondition condition = ACCOUNT.ID.eq(10L);
Account account = accountMapper.selectOneWithRelationsByCondition(condition);
Assertions.assertNotNull(account);
}
9、selectOneWithRelationsByQuery
@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);
Account account = accountMapper.selectOneWithRelationsByQuery(wrapper);
Assertions.assertNotNull(account);
}
10、selectOneWithRelationsByQueryAs
@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);
AccountDTO accountDTO = accountMapper.selectOneWithRelationsByQueryAs(wrapper, AccountDTO.class);
Assertions.assertNotNull(accountDTO);
}
11、selectListByIds
@Test
public void testSelectListByIds() {
List<Integer> ids = List.of(2, 4, 6, 8, 10);
List<Account> accounts = accountMapper.selectListByIds(ids);
Assertions.assertTrue(accounts.size() > 0);
System.out.println("accounts = " + accounts);
}
12、selectListByMap
@Test
public void testSelectListByMap() {
Map<String, Object> condition = Map.of("age", 20, "user_name", "zs");
List<Account> accounts = accountMapper.selectListByMap(condition);
Assertions.assertTrue(accounts.size() > 0);
System.out.println("accounts = " + accounts);
}
13、selectListByMap
@Test
public void testSelectListByMapAndCount() {
int count = 2;
Map<String, Object> condition = Map.of("age", 20);
List<Account> accounts = accountMapper.selectListByMap(condition, count);
Assertions.assertEquals(2, accounts.size());
System.out.println("accounts = " + accounts);
}
14、selectListByCondition
@Test
public void testSelectListByCondition() {
QueryCondition condition = ACCOUNT.ID.eq(5).and(ACCOUNT.AGE.ge(20));
List<Account> accounts = accountMapper.selectListByCondition(condition);
Assertions.assertTrue(accounts.size() > 0);
System.out.println("accounts = " + accounts);
}
15、selectListByCondition
@Test
public void testSelectListByConditionAndCount() {
QueryCondition condition = ACCOUNT.ID.eq(5).and(ACCOUNT.AGE.ge(20));
List<Account> accounts = accountMapper.selectListByCondition(condition, 2);
Assertions.assertEquals(2, accounts.size());
System.out.println("accounts = " + accounts);
}
16、selectListByQuery
@Test
public void testSelectListByQuery() {
List<Account> accounts = accountMapper.selectListByQuery(QueryWrapper.create().select(ACCOUNT.ALL_COLUMNS).from(ACCOUNT).where("id >= " + 10));
Assertions.assertTrue(accounts.size() > 0);
}
17、selectListByQuery
@Test
public void testSelectListByQueryAndConsumers() {
Consumer<FieldQueryBuilder<Account>> consumer = (builder) -> {
builder.nestedField(Account::getUserName);
};
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
@Transactional
@Test
public void testSelectCursorByQuery() {
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
@Test
public void testSelectRowsByQuery() {
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
@Test
public void testSelectListByQueryAs() {
List<AccountDTO> accountDTOS = accountMapper.selectListByQueryAs(QueryWrapper.create().where("age >=" + 20), AccountDTO.class);
Assertions.assertTrue(accountDTOS.size() > 0);
System.out.println("accountDTOS = " + accountDTOS);
}
21、selectAll
@Test
public void testSelectAll() {
List<Account> accounts = accountMapper.selectAll();
Assertions.assertTrue(accounts.size() > 0);
System.out.println("accounts = " + accounts);
}
22、selectObjectByQuery
@Test
public void testSelectObjectByQuery() {
String where = String.format("id = %d", 10);
QueryWrapper wrapper = QueryWrapper.create().select(ACCOUNT.USER_NAME).where(where);
String userName = (String) accountMapper.selectObjectByQuery(wrapper);
Assertions.assertNotNull(StringUtil.trimOrNull(userName));
System.out.println("userName = " + userName);
}
23、selectObjectByQueryAs
@Test
public void testSelectObjectByQueryAs() {
String where = String.format("id = %d", 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
@Test
public void testSelectObjectListByQuery() {
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
@Test
public void testSelectObjectListByQueryAs() {
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
@Test
public void testSelectCountByQuery() {
long count = accountMapper.selectCountByQuery(QueryWrapper.create().select(ACCOUNT.ID));
Assertions.assertTrue(count > 0);
System.out.println("count = " + count);
}
27、selectCountByCondition
@Test
public void test() {
long count = accountMapper.selectCountByCondition(ACCOUNT.ID.ge(0));
Assertions.assertTrue(count > 0);
System.out.println("count = " + count);
}