MyBatis-Flex 的增删改功能 - MyBatis-Flex 官方网站 https://mybatis-flex.com/zh/base/add-delete-update.html
https://mybatis-flex.com/zh/base/add-delete-update.html
代码 https://gitee.com/hntianshu/mybatis-flex-test
https://gitee.com/hntianshu/mybatis-flex-test
一 新增数据
不忽略
null值。 就是允许有null忽略null 就是不允许有null
BaseMapper 的接口提供了 insert 和 insertBatch 方法,用于新增数据;
- insert(entity):插入实体类数据,不忽略- null值。
- insertSelective(entity):插入实体类数据,但是忽略- null的数据,只对有值的内容进行插入。这样的好处是数据库已经配置了一些默认值,这些默认值才会生效。
- insert(entity, ignoreNulls):插入实体类数据。
- insertWithPk(entity):插入带有主键的实体类,不忽略- null值。
- insertSelectiveWithPk(entity):插入带有主键的实体类,忽略- null值。
- insertWithPk(entity, ignoreNulls):带有主键的插入,此时实体类不会经过主键生成器生成主键。
- insertBatch(entities):批量插入实体类数据,只会根据第一条数据来构建插入的字段内容。
- insertBatch(entities, size):批量插入实体类数据,按 size 切分。
- insertOrUpdate(entity):插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都不会忽略- null值。
- insertOrUpdateSelective(entity):插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都会忽略- null值。
- insertOrUpdate(entity, ignoreNulls):插入或者更新,若主键有值,则更新,若没有主键值,则插入。
1. insert(entity):插入实体类数据,不忽略 null 值。
 
@Autowiredprivate AccountMapper accountMapper;  @Testvoid insert() {Account account = new Account(16, "哈皮", null, LocalDateTime.now()); //还是自增//INSERT INTO `tb_account`(`user_name`, `age`, `birthday`) VALUES (?, ?, ?)//Parameters: 哈皮(String), null, 2024-01-02T09:42:14.063491400(LocalDateTime)int row = accountMapper.insert(account);System.err.println("新增数量:" + row + "条");}2.insertSelective(entity):插入实体类数据,但是忽略 null 的数据,只对有值的内容进行插入。这样的好处是数据库已经配置了一些默认值,这些默认值才会生效。
 
 
    /*** insertSelective(entity):插入实体类数据,但是忽略 null 的数据,只对有值的内容进行插入。这样的好处是数据库已经配置了一些默认值,这些默认值才会生效。*/@Testvoid insertSelective() {Account account = new Account(null, "赵华", null, LocalDateTime.now());//Preparing: INSERT INTO `tb_account`(`user_name`, `birthday`) VALUES (?, ?)//Parameters: 赵华(String), 2024-01-02T09:44:54.492425100(LocalDateTime)int row = accountMapper.insertSelective(account);System.err.println("新增数量:" + row + "条");}3. insert(entity, ignoreNulls):插入实体类数据。
- ignoreNulls: false相当于 insert(entity)
- ignoreNulls: true相当于 insertSelective(entity)
   /*** insert(entity, ignoreNulls):插入实体类数据。* ignoreNulls: false相当于 insert(entity)* ignoreNulls: true相当于 insertSelective(entity)*/@Testvoid insert2() {Account account = new Account(null, "赵华", null, LocalDateTime.now());int row = accountMapper.insert(account, true);System.err.println("新增数量:" + row + "条");}4.insertWithPk(entity):插入带有主键的实体类,不忽略 null 值。
   /*** insertWithPk(entity):插入带有主键的实体类,不忽略 null 值。*/@Testvoid insertWithPk() {Account account = new Account(null, "赵华", null, LocalDateTime.now());int row = accountMapper.insertWithPk(account);System.err.println("新增数量:" + row + "条");}5. insertSelectiveWithPk(entity):插入带有主键的实体类,忽略 null 值。 相当于insertWithPk(entity, true)
  /*** insertSelectiveWithPk(entity):插入带有主键的实体类,忽略 null 值。* 相当于insertWithPk(entity, true)*/@Testvoid insertSelectiveWithPk() {Account account = new Account(5, "赵华", null, LocalDateTime.now());int row = accountMapper.insertSelectiveWithPk(account);System.err.println("新增数量:" + row + "条");}6.insertWithPk(entity, ignoreNulls):带有主键的插入,此时实体类不会经过主键生成器生成主键。
 
insertWithPk = insertWithPk(entity, false)insertSelectiveWithPk = insertWithPk(entity, true)
  /*** insertWithPk(entity, ignoreNulls):带有主键的插入,此时实体类不会经过主键生成器生成主键。* insertWithPk = insertWithPk(entity, false)* insertSelectiveWithPk = insertWithPk(entity, true)*/@Testvoid insertWithPk2() {Account account = new Account(5, "赵华", null, LocalDateTime.now());int row = accountMapper.insertWithPk(account, true);System.err.println("新增数量:" + row + "条");}7.insertBatch(entities):批量插入实体类数据,只会根据第一条数据来构建插入的字段内容。
只会根据字段数量最多的数据来构建插入的字段内容不忽略null
/*** insertBatch(entities):批量插入实体类数据,只会根据第一条数据来构建插入的字段内容。* 只会根据字段数量最多的数据来构建插入的字段内容* 不忽略null*/@Testvoid insertBatch() {ArrayList<Account> list = new ArrayList<>();list.add(new Account(null, "林一", 25, LocalDateTime.now()));list.add(new Account(null, null, null, null));list.add(new Account("王五", 23, LocalDateTime.now()));int row = accountMapper.insertBatch(list);System.err.println("新增数量:" + row + "条");}8. insertBatch(entities, size):批量插入实体类数据,按 size 切分。分开插入 n条做切分
/*** insertBatch(entities, size):批量插入实体类数据,按 size 切分。* 分开插入 n条做切分*/@Testvoid insertBatch2() {ArrayList<Account> list = new ArrayList<>();list.add(new Account(null, "剑一", 25, LocalDateTime.now()));list.add(new Account(null, "剑二", 25, LocalDateTime.now()));list.add(new Account(null, "剑三", 25, LocalDateTime.now()));int row = accountMapper.insertBatch(list, 2);System.err.println("新增数量:" + row + "条");}9.insertOrUpdate(entity):插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都不会忽略 null 值。
    /*** insertOrUpdate(entity):插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都不会忽略 null 值。*/@Testvoid insertOrUpdate() {Account account = new Account(56, "林二", 25, LocalDateTime.now());int row = accountMapper.insertOrUpdate(account);System.err.println("新增/更新 数量:" + row + "条");}10.insertOrUpdateSelective(entity):插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都会忽略 null 值。
    /*** insertOrUpdateSelective(entity):插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都会忽略 null 值。*/@Testvoid insertOrUpdateSelective() {Account account = new Account(56, "林二", null, LocalDateTime.now());int row = accountMapper.insertOrUpdateSelective(account);System.err.println("新增/更新 数量:" + row + "条");}11. insertOrUpdate(entity, ignoreNulls):插入或者更新,若主键有值,则更新,若没有主键值,则插入。
 
 /*** insertOrUpdate(entity, ignoreNulls):插入或者更新,若主键有值,则更新,若没有主键值,则插入。* insertOrUpdate = insertOrUpdate(entity, false)* insertOrUpdateSelective = insertOrUpdate(entity, true)*/@Testvoid insertOrUpdate2() {Account account = new Account(56, "林二", null, LocalDateTime.now());int row = accountMapper.insertOrUpdate(account, false);System.err.println("新增/更新 数量:" + row + "条");}12.用 UpdateWrapper 新增数据
  /*** 用 UpdateWrapper 新增数据* INSERT INTO `tb_account`(`user_name`,  `birthday`)* VALUES (?, now())*/@Testpublic void testInsertWithRaw() {Account account = new Account();account.setUserName("剑一");Account newAccount = UpdateWrapper.of(account)
//       .setRaw("birthday", "now()")
//       .setRaw(ACCOUNT.BIRTHDAY, "now()").setRaw(Account::getBirthday, "now()").toEntity();int row = accountMapper.insert(newAccount);System.err.println("新数量:" + row + "条");}/*** 用 UpdateWrapper 新增数据 复杂一点* INSERT INTO `tb_account`(`user_name`,  `birthday`)* VALUES (?, (select xxx from ...))*/@Testpublic void testInsertWithRaw2() {Account account = new Account();account.setUserName("剑二");Account newAccount = UpdateWrapper.of(account).setRaw(Account::getBirthday, "(select a.birthday from (SELECT birthday FROM tb_account where id = 1)" +" a )").toEntity();int row = accountMapper.insert(newAccount);System.err.println("新数量:" + row + "条");}二 删除数据
不忽略
null值。 就是允许有null忽略null 就是不允许有null
BaseMapper 的接口提供了 deleteById、deleteBatchByIds、deleteByMap、deleteByQuery 方法,用于删除数据;
- deleteById(id):根据主键删除数据。如果是多个主键的情况下,需要传入数组,例如:- new Integer[]{100,101}。
- delete(entity):根据实体主键来删除数据。相比deleteById(id),此方法更便于对复合主键实体类的删除。
- deleteBatchByIds(ids):根据多个主键批量删除数据。
- deleteBatchByIds(ids, size):根据多个主键批量删除数据。
- deleteByMap(whereConditions):根据 Map 构建的条件来删除数据。
- deleteByCondition(whereConditions):根据查询条件来删除数据。
- deleteByQuery(queryWrapper):根据查询条件来删除数据。
1. deleteById(id):根据主键删除数据。如果是多个主键的情况下,需要传入数组,例如:new Integer[]{100,101}。
 
 /*** deleteById(id):根据主键删除数据。如果是多个主键的情况下,需要传入数组,例如:new Integer[]{100,101}。*/@Testpublic void deleteById() {int row = accountMapper.deleteById(59);System.err.println("删除数量:" + row + "条");}2. deleteBatchByIds(ids):根据多个主键批量删除数据。
 
    /*** deleteBatchByIds(ids):根据多个主键批量删除数据。*/@Testpublic void deleteBatchByIds() {List<Integer> array = new ArrayList<Integer>();array.add(59);array.add(60);array.add(61);int row = accountMapper.deleteBatchByIds(array);System.err.println("删除数量:" + row + "条");}
3. deleteBatchByIds(ids, size):根据多个主键批量删除数据。 分片删除
    /*** deleteBatchByIds(ids, size):根据多个主键批量删除数据。 分片删除*/@Testpublic void deleteBatchByIds2() {List<Integer> array = new ArrayList<Integer>();array.add(62);array.add(64);array.add(65);int row = accountMapper.deleteBatchByIds(array, 2);System.err.println("删除数量:" + row + "条");}4. deleteByMap(whereConditions):根据 Map 构建的条件来删除数据。
 
    /*** deleteByMap(whereConditions):根据 Map 构建的条件来删除数据。*/@Testpublic void deleteByMap() {//条件 where id =63 and age = 18Map<String, Object> map = new HashMap<>();map.put("id", 63);map.put("age", 18);int row = accountMapper.deleteByMap(map);System.err.println("删除数量:" + row + "条");}
5.deleteByCondition(whereConditions):根据查询条件来删除数据。
    /*** deleteByCondition(whereConditions):根据查询条件来删除数据。* ge >=* le <=* gt >* eq =* lt <* notIN*/@Testpublic void deleteByCondition() {//accountMapper.deleteByCondition(ACCOUNT.ID.ge(100));
//        int row = accountMapper.deleteByCondition(ACCOUNT.ID.in(67,69));int row = accountMapper.deleteByCondition(ACCOUNT.ID.eq(70));System.err.println("删除数量:" + row + "条");}6.deleteByQuery(queryWrapper):根据查询条件来删除数据。
    /*** deleteByQuery(queryWrapper):根据查询条件来删除数据。*/@Testpublic void deleteByQuery() {QueryWrapper queryWrapper = QueryWrapper.create();queryWrapper.where(ACCOUNT.ID.ge(66));//通过 queryWrapper 删除int row = accountMapper.deleteByQuery(queryWrapper);System.err.println("删除数量:" + row + "条");}三 更新数据
不忽略
null值。 就是允许有null忽略null 就是不允许有null
BaseMapper 的接口提供了 update、updateByMap、updateByQuery 方法,用于更新数据;
- update(entity):根据主键来更新数据,若实体类属性数据为- null,该属性不会更新到数据库。
- update(entity, ignoreNulls):根据主键来更新数据到数据库。
- updateByMap(entity, whereConditions):根据 Map 构建的条件来更新数据。
- updateByMap(entity, ignoreNulls, whereConditions):根据 Map 构建的条件来更新数据。
- updateByCondition(entity, whereConditions):根据查询条件来更新数据。
- updateByCondition(entity, ignoreNulls, whereConditions):根据查询条件来更新数据。
- updateByQuery(entity, queryWrapper):根据查询条件来更新数据。
- updateByQuery(entity, ignoreNulls, queryWrapper):根据查询条件来更新数据。
- updateNumberAddByQuery(fieldName, value, queryWrapper)- update table set field = field + 1 where ...的场景。
- updateNumberAddByQuery(column, value, queryWrapper)- update table set field = field + 1 where ...的场景。
- updateNumberAddByQuery(fn, value, queryWrapper)- update table set field = field + 1 where ...的场景。
1.update(entity):根据主键来更新数据,若实体类属性数据为 null,该属性不会更新到数据库。
 
    /*** update(entity):根据主键来更新数据,若实体类属性数据为 null,该属性不会更新到数据库。*/@Testpublic void update() {Account account = new Account();account.setUserName("小明");account.setAge(18);account.setBirthday(LocalDateTime.now());account.setId(72);int row = accountMapper.update(account);System.err.println("修改数量:" + row + "条");}2.update(entity, ignoreNulls):根据主键来更新数据到数据库。
    /*** update(entity, ignoreNulls):根据主键来更新数据到数据库。*/@Testpublic void update2() {Account account = new Account();account.setUserName("小明1");account.setId(72);int row = accountMapper.update(account,false);System.err.println("修改数量:" + row + "条");}3.updateByMap(entity, whereConditions):根据 Map 构建的条件来更新数据。不忽略NUll
    /*** updateByMap(entity, whereConditions):根据 Map 构建的条件来更新数据。* 不忽略NUll**/@Testpublic void updateByMap() {Account account = new Account();account.setUserName("小明2");Map<String, Object> map = new HashMap<>();map.put("id","72");int row = accountMapper.updateByMap(account,map);System.err.println("修改数量:" + row + "条");}4.updateByMap(entity, ignoreNulls, whereConditions):根据 Map 构建的条件来更新数据。 忽略null
    /*** updateByMap(entity, ignoreNulls, whereConditions):根据 Map 构建的条件来更新数据。* 忽略null*/@Testpublic void updateByMap2() {Account account = new Account();
//        account.setUserName("小明2");account.setUserName(null); //会报错Map<String, Object> map = new HashMap<>();map.put("id","72");int row = accountMapper.updateByMap(account,true,map);System.err.println("修改数量:" + row + "条");}5.updateByCondition(entity, whereConditions):根据查询条件来更新数据。 不忽略NUll
    /*** updateByCondition(entity, whereConditions):根据查询条件来更新数据。* 不忽略NUll*/@Testpublic void updateByCondition() {Account account = new Account();account.setUserName("剑南山2");
//        account.setUserName(null); //会报错//  update tb_account set user_name="剑南山2" , age=21   where id = 73
//        int row = accountMapper.updateByCondition(account,ACCOUNT.ID.eq("73"));//   update tb_account set user_name="剑南山2" , age=21   where id > 73int row = accountMapper.updateByCondition(account,ACCOUNT.ID.gt(73));System.err.println("修改数量:" + row + "条");}6.updateByCondition(entity, ignoreNulls, whereConditions):根据查询条件来更新数据。忽略NUll,传入空不修改
    /*** updateByCondition(entity, ignoreNulls, whereConditions):根据查询条件来更新数据。* 忽略NUll,传入空不修改*/@Testpublic void updateByCondition2() {Account account = new Account();account.setUserName(null); //默认不修改account.setAge(18);
//        account.setUserName(null); //会报错// update tb_account set  age=18  where id = 73
//        int row = accountMapper.updateByCondition(account,ACCOUNT.ID.eq("73"));// update tb_account set  age=18  where id > 73int row = accountMapper.updateByCondition(account,true,ACCOUNT.ID.gt(73));System.err.println("修改数量:" + row + "条");}7.updateByQuery(entity, queryWrapper):根据查询条件来更新数据。不忽略NUll
    /*** updateByQuery(entity, queryWrapper):根据查询条件来更新数据。* 不忽略NUll*/@Testpublic void updateByQuery() {Account account = new Account();account.setUserName("神奇的小鱼人");account.setAge(20);QueryWrapper queryWrapper = new QueryWrapper();// update tb_account set user_name="神奇的小鱼人" , age=21   where id = 73 or id = 74queryWrapper.where(ACCOUNT.ID.eq(73)).or(ACCOUNT.ID.eq(74));int row = accountMapper.updateByQuery(account,queryWrapper);System.err.println("修改数量:" + row + "条");}7.updateByQuery(entity, ignoreNulls, queryWrapper):根据查询条件来更新数据。 忽略NUll
    /*** updateByQuery(entity, ignoreNulls, queryWrapper):根据查询条件来更新数据。* 忽略NUll*/@Testpublic void updateByQuery2() {Account account = new Account();account.setUserName(null); //忽略NULL 不进行updateaccount.setAge(20);QueryWrapper queryWrapper = new QueryWrapper();// update tb_account set  age=21   where id = 73 or id = 74queryWrapper.where(ACCOUNT.ID.eq(73)).or(ACCOUNT.ID.eq(74));int row = accountMapper.updateByQuery(account,queryWrapper);System.err.println("修改数量:" + row + "条");}8.updateByQuery(entity, ignoreNulls, queryWrapper):根据查询条件来更新数据。 不忽略NUll
  /*** updateByQuery(entity, ignoreNulls, queryWrapper):根据查询条件来更新数据。* ignoreNulls:false  忽略NUll* ignoreNulls:true  不忽略NUll**/@Testpublic void updateByQuery2() {Account account = new Account();account.setUserName(null); //忽略NULL 不进行updateaccount.setAge(20);QueryWrapper queryWrapper = new QueryWrapper();// update tb_account set  age=21   where id = 73 or id = 74queryWrapper.where(ACCOUNT.ID.eq(73)).or(ACCOUNT.ID.eq(74));int row = accountMapper.updateByQuery(account,false,queryWrapper);System.err.println("修改数量:" + row + "条");}9. 部分字段更新 update
(1) 部分字段
在很多场景下,我们希望只更新部分字段,而更新的字段中,一些为 null,一些非 null。此时需要用到
UpdateEntity工具类,以下是示例代码:以下的示例中,会把 id (主键)为 100 这条数据中的 user_name 字段更新为 null,age 字段更新为 10,其他字段不会被更新。
也就是说,通过 UpdateEntity 创建的对象,只会更新调用了 setter 方法的字段,若不调用 setter 方法,不管这个对象里的属性的值是什么,都不会更新到数据库。
    /*** updateByQuery(entity, ignoreNulls, queryWrapper):根据查询条件来更新数据。* 不忽略NUll*/@Testpublic void UpdateEntity () {//update tb_account//set user_name = ?, age = ? where id = ?//#参数: null,10,100//写法1 不忽略NUll
//        Account account = UpdateEntity.of(Account.class, 100);//写法2 不忽略NUll
//        Account account = UpdateEntity.of(Account.class);
//        account.setId(74);//写法3 忽略null//update tb_account//set  age = ? where id = ?//#参数: 10,100Account account = new Account();account.setId(74);account.setUserName(null);account.setAge(11);int row = accountMapper.update(account);System.err.println("修改数量:" + row + "条");}(2) 部分字段更新(增强)
    /*** 部分字段更新(增强)*/@Testpublic void UpdateEntity2 () {Account account = UpdateEntity.of(Account.class, 100);account.setUserName(null);// 通过 UpdateWrapper 操作 account 数据UpdateWrapper wrapper = UpdateWrapper.of(account);wrapper.setRaw("age", "age + 1");accountMapper.update(account);// update tb_account//set user_name = null, age = age + 1 where id = 100int row = accountMapper.update(account);System.err.println("修改数量:" + row + "条");}(3) 更高级的用法
    /*** 更高级的用法2*/@Testpublic void UpdateEntity4 () {Account account = UpdateEntity.of(Account.class, 100);//        account.setUserName("Michael2");
// 通过 UpdateWrapper 操作 account 数据UpdateWrapper wrapper = UpdateWrapper.of(account);
//        wrapper.setRaw(ACCOUNT.AGE, "(select a.age from (select age from tb_account where id = 101) a )");wrapper.set(ACCOUNT.USER_NAME, "(select name from tb_account where id = 101)");accountMapper.update(account);int row = accountMapper.update(account);System.err.println("修改数量:" + row + "条");}10. 链式修改-UpdateChain
UpdateChain 是一个对
UpdateEntity、UpdateWrapper等进行封装的一个工具类,方便用户用于进行链式操作。假设我们要更新
Account的userName为 "张三",更新年龄在之前的基础上加 1,更新代码如下:
    /*** UpdateChain 是一个对 UpdateEntity、UpdateWrapper 等进行封装的一个工具类,方便用户用于进行链式操作。* 不忽略空* UPDATE `tb_account` SET `user_name` = '张三' , `age` = age + 1* WHERE `id` = 1** UPDATE `tb_account` SET `user_name` = null , `age` = age + 1* WHERE `id` = 1*/@Testpublic void testUpdateChain() {UpdateChain.of(Account.class)
//                .set(Account::getUserName, "张三").set(Account::getUserName, null).setRaw(Account::getAge, "age + 1").where(Account::getId).eq(1).update();}
    /*** 假设我们要更新 Account 的 userName 为 "张三",更新年龄在之前的基础上加 1,更新代码如下:* UPDATE `tb_account` SET `age` = `age` + 1* WHERE  `id` >= 100 AND `age` = 18**/@Testpublic void testUpdateChain2() {//更新数据UpdateChain.of(Account.class).set(Account::getAge, ACCOUNT.AGE.add(1)).where(Account::getId).ge(100).and(Account::getAge).eq(18).update();//查询所有数据并打印QueryChain.of(accountMapper).where(Account::getId).ge(100).and(Account::getAge).eq(18).list().forEach(System.out::println);}
四 基础查询
单条数据查询
在 MyBatis-Flex 的 BaseMapper 中,提供了如下的功能用于查询数据库的数据:
- selectOneById(id):根据主键查询数据。
- selectOneByEntityId(entity):根据实体主键查询数据,便于对复合主键实体类的查询。
- selectOneByMap(whereConditions):根据 Map 构建的条件来查询数据。
- selectOneByCondition(whereConditions):根据查询条件查询数据。
- selectOneByQuery(queryWrapper):根据查询条件来查询 1 条数据。
- selectOneByQueryAs(queryWrapper, asType):根据查询条件来查询 1 条数据。
- selectOneWithRelationsByMap(whereConditions):根据 Map 构建的条件来查询 1 条数据。
- selectOneWithRelationsByCondition(whereConditions):根据查询条件查询 1 条数据。
- selectOneWithRelationsByQuery(queryWrapper):根据查询条件来查询 1 条数据。
- selectOneWithRelationsByQueryAs(queryWrapper, asType):根据查询条件来查询 1 条数据。
- selectListByIds(ids):根据多个主键来查询多条数据。
- selectListByMap(whereConditions):根据 Map 来构建查询条件,查询多条数据。
- selectListByMap(whereConditions, count):根据 Map 来构建查询条件,查询多条数据。
- selectListByCondition(whereConditions):根据查询条件查询多条数据。
- selectListByCondition(whereConditions, count):根据查询条件查询多条数据。
- selectListByQuery(queryWrapper):根据查询条件查询数据列表。
- selectListByQuery(queryWrapper, consumers):根据查询条件查询数据列表。
- selectCursorByQuery(queryWrapper):根据查询条件查询游标数据,该方法必须在事务中才能正常使用,非事务下无法获取数据。
- selectRowsByQuery(queryWrapper):根据查询条件查询 Row 数据。
- selectListByQueryAs(queryWrapper, asType):根据查询条件查询数据列表,要求返回的数据为 asType。这种场景一般用在 left join 时,有多出了实体类本身的字段内容,可以转换为 dto、vo 等场景。
- selectListByQueryAs(queryWrapper, asType, consumers):根据查询条件查询数据列表,要求返回的数据为 asType 类型。
- selectListWithRelationsByQuery(queryWrapper):查询实体类及其 Relation 注解字段。
- selectListWithRelationsByQueryAs(queryWrapper, asType):查询实体类及其 Relation 注解字段。
- selectListWithRelationsByQueryAs(queryWrapper, asType, consumers):查询实体类及其 Relation 注解字段。
- selectAll():查询全部数据。
- selectAllWithRelations():查询全部数据,及其 Relation 字段内容。
- selectObjectByQuery(queryWrapper):查询第一列返回的数据,QueryWrapper 执行的结果应该只有 1 列,例如:- QueryWrapper.create().select(ACCOUNT.id).where(...);
- selectObjectByQueryAs(queryWrapper, asType):查询第一列返回的数据,QueryWrapper 执行的结果应该只有 1 列,例如:- QueryWrapper.create().select(ACCOUNT.id).where(...);
- selectObjectListByQuery(queryWrapper):查询第一列返回的数据集合,QueryWrapper 执行的结果应该只有 1 列,例如:- QueryWrapper.create().select(ACCOUNT.id).where(...);
- selectObjectListByQueryAs(queryWrapper, asType):查询第一列返回的数据集合,QueryWrapper 执行的结果应该只有 1 列,例如:- QueryWrapper.create().select(ACCOUNT.id).where(...);
- selectCountByQuery(queryWrapper):查询数据量。
- selectCountByCondition(whereConditions):根据条件查询数据总量。
1. selectOneById(id):根据主键查询数据。
    /*** selectOneById(id):根据主键查询数据。*/@Testpublic void testUpdateChain() {Account account = accountMapper.selectOneById(1L);System.err.println("==============");System.err.println(account);System.err.println("==============");}2.selectOneByEntityId(entity):根据实体主键查询数据,便于对复合主键实体类的查询。
 
- 只能根据主键查询
    /*** selectOneByEntityId(entity):根据实体主键查询数据,便于对复合主键实体类的查询。*/@Testpublic void selectOneByEntityId() {Account param = new Account();param.setId(1);Account account = accountMapper.selectOneByEntityId(param);System.err.println("==============");System.err.println(account);System.err.println("==============");Account param2 = new Account();param.setAge(18);param.setUserName("剑南山2");Account account2 = accountMapper.selectOneByEntityId(param2);System.err.println("==============");System.err.println(account2);System.err.println("==============");}
3.selectOneByMap(whereConditions):根据 Map 构建的条件来查询数据。
 
    /*** selectOneByMap(whereConditions):根据 Map 构建的条件来查询数据。* * selectOneByMap(Map<String, Object> whereConditions)*/@Testpublic void selectOneByMap() {Map<String,Object> param = new HashMap<>();param.put("id",2);param.put("user_name","李四");Account account = accountMapper.selectOneByMap(param);System.err.println("==============");System.err.println(account);System.err.println("==============");}
4.selectOneByCondition(whereConditions):根据查询条件查询数据。
    /*** selectOneByCondition(whereConditions):根据查询条件查询数据。** selectOneByCondition(QueryCondition whereConditions)*/@Testpublic void selectOneByCondition() {Account account = accountMapper.selectOneByCondition(ACCOUNT.ID.eq(2).or(ACCOUNT.ID.eq(1)));System.err.println("==============");System.err.println(account);System.err.println("==============");}
5. selectOneByQuery(queryWrapper):根据查询条件来查询 1 条数据。
    /*** selectOneByQuery(queryWrapper):根据查询条件来查询 1 条数据。* selectOneByQuery(QueryWrapper queryWrapper)*/@Testpublic void selectOneByQuery() {QueryWrapper param = new QueryWrapper();// where id = 1
//        param.where(ACCOUNT.ID.eq(1));//SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `user_name` LIKE ? LIMIT 1//Parameters: %李四%(String)param.where(ACCOUNT.USER_NAME.like("李四"));Account account = accountMapper.selectOneByQuery(param);System.err.println("==============");System.err.println(account);System.err.println("==============");}6. selectOneWithRelationsByMap(whereConditions):根据 Map 构建的条件来查询 1 条数据。
    /*** selectOneWithRelationsByMap(whereConditions):根据 Map 构建的条件来查询 1 条数据。** selectOneWithRelationsByMap(Map<String, Object> whereConditions)*/@Testpublic void selectOneWithRelationsByMap() {Map<String,Object> param = new HashMap<>();param.put("id",1);// Preparing:  SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `tb_account`.`id` = ? LIMIT 1// Parameters: 1(Integer)Account account = accountMapper.selectOneWithRelationsByMap(param);System.err.println("==============");System.err.println(account);System.err.println("==============");}7.selectOneWithRelationsByCondition(whereConditions):根据查询条件查询 1 条数据。
    /*** selectOneWithRelationsByCondition(whereConditions):根据查询条件查询 1 条数据。*/@Testpublic void selectOneWithRelationsByCondition() {  // Preparing:  SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `tb_account`.`id` = ? LIMIT 1// Parameters: 1(Integer)Account account = accountMapper.selectOneWithRelationsByCondition(ACCOUNT.ID.eq(1));System.err.println("==============");System.err.println(account);System.err.println("==============");}
8.selectOneWithRelationsByQuery(queryWrapper):根据查询条件来查询 1 条数据。
    /*** selectOneWithRelationsByQuery(queryWrapper):根据查询条件来查询 1 条数据。*/@Testpublic void selectOneWithRelationsByQuery() {// Preparing:  SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `tb_account`.`id` = ? LIMIT 1// Parameters: 1(Integer)QueryWrapper param = new QueryWrapper();param.where(ACCOUNT.ID.eq(1));Account account = accountMapper.selectOneWithRelationsByQuery(param);System.err.println("==============");System.err.println(account);System.err.println("==============");}9.selectOneWithRelationsByQueryAs(queryWrapper, asType):根据查询条件来查询 1 条数据。
    /*** selectOneWithRelationsByQueryAs(queryWrapper, asType):根据查询条件来查询 1 条数据。*/@Testpublic void selectOneWithRelationsByQueryAs() {// Preparing:  SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `tb_account`.`id` = ? LIMIT 1// Parameters: 1(Integer)QueryWrapper param = new QueryWrapper();param.where(ACCOUNT.ID.eq(1));Account account = accountMapper.selectOneWithRelationsByQueryAs(param,Account.class);System.err.println("==============");System.err.println(account);System.err.println("==============");}多条数据查询
1.selectListByIds(ids):根据多个主键来查询多条数据。
    /*** selectListByIds(ids):根据多个主键来查询多条数据。* selectListByIds(@Param("$$primaryValue") Collection<? extends Serializable> var1)*/@Testpublic void selectListByIds() {List<Integer> param = Arrays.asList(1, 2);//Preparing: SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `id` = ? OR `id` = ?//Parameters: 1(Integer), 2(Integer)//==============//Account(id=1, userName=null, age=20, birthday=2020-01-11T00:00)//Account(id=2, userName=李四, age=19, birthday=2021-03-21T00:00)//==============List<Account> accounts = accountMapper.selectListByIds(param);System.err.println("==============");accounts.forEach(System.err::println);System.err.println("==============");}
2.selectListByMap(whereConditions):根据 Map 来构建查询条件,查询多条数据。
    /*** selectListByMap(whereConditions):根据 Map 来构建查询条件,查询多条数据。*/@Testpublic void selectListByMap() {Map<String, Object> param = new HashMap<>();param.put("user_name","剑南山2");//Preparing: SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account` WHERE `tb_account`.`user_name` = ?//Parameters: 剑南山2(String)//==============//Account(id=99, userName=剑南山2, age=18, birthday=2023-12-29T18:17:31)//Account(id=101, userName=剑南山2, age=19, birthday=2020-01-11T00:00)//==============List<Account> accounts = accountMapper.selectListByMap(param);System.err.println("==============");accounts.forEach(System.err::println);System.err.println("==============");}