其他分享
首页 > 其他分享> > MyBatis-Plus提供的默认Mapper能力

MyBatis-Plus提供的默认Mapper能力

作者:互联网

MyBatis-Plus提供的BaseMapper接口,内置了很多单表CRUD功能,我们只需要定义一个接口去继承它,就能瞬间拥有这些能力。

官方文档 传送门

下面,详细整理BaseMapper提供的能力。

1. 传入实体Entity对象,添加一条记录

int insert(T entity);

例子:

    @Test
    public void testInsert() {
        UserEntity user = new UserEntity();
        user.setId(6L);
        user.setName("Michael");
        user.setAge(28);
        user.setEmail("test6@baomidou.com");
        userMapper.insert(user);
    }

控制台打印出来的日志:

==>  Preparing: INSERT INTO tb_user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
==> Parameters: 6(Long), Michael(String), 28(Integer), test6@baomidou.com(String)
<==    Updates: 1

1. 传入主键ID的值,根据主键ID删除一条记录

int deleteById(Serializable id);

例子:

    @Test
    public void testDeleteById() {
        userMapper.deleteById(6L);
    }

控制台打印出来的日志:

==>  Preparing: DELETE FROM tb_user WHERE id=?
==> Parameters: 6(Long)
<==    Updates: 1

2. 传入实体Entity对象,根据实体中的主键ID删除一条数据

int deleteById(T entity);

例子:

    @Test
    public void testDeleteById2() {
        UserEntity user = new UserEntity();
        user.setId(6L);
        userMapper.deleteById(user);
    }

设置主键ID就可以了。

控制台打印出来的日志:

==>  Preparing: DELETE FROM tb_user WHERE id=?
==> Parameters: 6(Long)
<==    Updates: 1

3. 根据 columnMap 条件,删除记录

int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

columnMap中传入where条件字段和值。

支持AND、等值匹配。

例子:

    @Test
    public void testDeleteByMap() {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("name", "Billie");
        columnMap.put("age", 24);
        userMapper.deleteByMap(columnMap);
    }

控制台打印出来的日志:

==>  Preparing: DELETE FROM tb_user WHERE name = ? AND age = ?
==> Parameters: Billie(String), 24(Integer)
<==    Updates: 1

4. 根据wrapper条件删除记录

int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

queryWrapper封装Entity,作为where条件。

支持非等值匹配。

例子:

    @Test
    public void testDeleteByWrapper() {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.likeRight("name", "J");
        userMapper.delete(wrapper);
    }

控制台打印出来的日志:

==>  Preparing: DELETE FROM tb_user WHERE (name LIKE ?)
==> Parameters: J%(String)
<==    Updates: 2

5. 根据主键ID集合批量删除记录

int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

例子:

    @Test
    public void testDeleteBatchIds() {
        List<Long> ids = Arrays.asList(3L, 4L);
        userMapper.deleteBatchIds(ids);
    }

控制台打印出来的日志:

==>  Preparing: DELETE FROM tb_user WHERE id IN ( ? , ? )
==> Parameters: 3(Long), 4(Long)
<==    Updates: 2

1. 根据主键ID修改记录

int updateById(@Param(Constants.ENTITY) T entity);

例子:

    @Test
    public void testUpdateById() {
        UserEntity user = new UserEntity();
        user.setId(5L);
        user.setAge(30);
        user.setEmail("test5update@baomidou.com");
        userMapper.updateById(user);
    }

控制台打印出来的日志:

==>  Preparing: UPDATE tb_user SET age=?, email=? WHERE id=?
==> Parameters: 30(Integer), test5update@baomidou.com(String), 5(Long)
<==    Updates: 1

2. 根据wrapper条件更新记录

int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);

例子:

    @Test
    public void testUpdateByWrapper() {
        UserEntity user = new UserEntity();
        user.setAge(35);
        user.setEmail("wrapperUpdate@baomidou.com");
        UpdateWrapper<UserEntity> wrapper = new UpdateWrapper<>();
        wrapper.ge("age", "28");
        userMapper.update(user, wrapper);
    }

控制台打印出来的日志:

==>  Preparing: UPDATE tb_user SET age=?, email=? WHERE (age >= ?)
==> Parameters: 35(Integer), wrapperUpdate@baomidou.com(String), 28(String)
<==    Updates: 2

1. 根据 ID 查询

T selectById(Serializable id);

例子:

    @Test
    public void testSelectById() {
        UserEntity user = userMapper.selectById(1L);
        System.out.println(user);
    }

控制台打印出来的日志:

==>  Preparing: SELECT id,name,age,email FROM tb_user WHERE id=?
==> Parameters: 1(Long)
<==    Columns: id, name, age, email
<==        Row: 1, Jone, 18, test1@baomidou.com
<==      Total: 1

2. 根据 ID集合 批量查询

List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

例子:

    @Test
    public void testSelectBatchIds() {
        List<Long> ids = Arrays.asList(1L, 3L);
        List<UserEntity> users = userMapper.selectBatchIds(ids);
        System.out.println(users);
    }

控制台打印出来的日志:

==>  Preparing: SELECT id,name,age,email FROM tb_user WHERE id IN ( ? , ? )
==> Parameters: 1(Long), 3(Long)
<==    Columns: id, name, age, email
<==        Row: 1, Jone, 18, test1@baomidou.com
<==        Row: 3, Tom, 35, wrapperUpdate@baomidou.com
<==      Total: 2

3. 根据 columnMap 条件批量查询

List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

例子:

    @Test
    public void testSelectByMap() {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("age", 35);
        List<UserEntity> users = userMapper.selectByMap(columnMap);
        System.out.println(users);
    }

控制台打印出来的日志:

==>  Preparing: SELECT id,name,age,email FROM tb_user WHERE age = ?
==> Parameters: 35(Integer)
<==    Columns: id, name, age, email
<==        Row: 3, Tom, 35, wrapperUpdate@baomidou.com
<==        Row: 5, Billie, 35, wrapperUpdate@baomidou.com
<==      Total: 2

4. 根据 Wrapper 条件查询一条记录

    default T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
        List<T> ts = this.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(ts)) {
            if (ts.size() != 1) {
                throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records");
            }
            return ts.get(0);
        }
        return null;
    }

注:如果查出来有多条,会报错。

例子:

    @Test
    public void testSelectOne() {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("name", "Tom");
        UserEntity user = userMapper.selectOne(wrapper);
        System.out.println(user);
    }

控制台打印出来的日志:

==>  Preparing: SELECT id,name,age,email FROM tb_user WHERE (name = ?)
==> Parameters: Tom(String)
<==    Columns: id, name, age, email
<==        Row: 3, Tom, 35, wrapperUpdate@baomidou.com
<==      Total: 1

5. 根据 Wrapper 条件,查询总记录数

Long selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

例子:

    @Test
    public void testSelectCount() {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("age", 35);
        long count = userMapper.selectCount(wrapper);
        System.out.println(count);
    }

控制台打印出来的日志:

==>  Preparing: SELECT COUNT( * ) FROM tb_user WHERE (age = ?)
==> Parameters: 35(Integer)
<==    Columns: COUNT( * )
<==        Row: 2
<==      Total: 1

6. 根据 Wrapper 条件,批量查询Entity集合

List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

例子:

    @Test
    public void testSelectList() {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("age", 35);
        List<UserEntity> users = userMapper.selectList(wrapper);
        System.out.println(users);
    }

控制台打印出来的日志:

==>  Preparing: SELECT id,name,age,email FROM tb_user WHERE (age = ?)
==> Parameters: 35(Integer)
<==    Columns: id, name, age, email
<==        Row: 3, Tom, 35, wrapperUpdate@baomidou.com
<==        Row: 5, Billie, 35, wrapperUpdate@baomidou.com
<==      Total: 2

7. 根据 Wrapper 条件,批量查询Map集合

List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

例子:

    @Test
    public void testSelectMaps() {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("age", 35);
        List<Map<String, Object>> users = userMapper.selectMaps(wrapper);
        System.out.println(users);
    }

控制台打印出来的日志:

==>  Preparing: SELECT id,name,age,email FROM tb_user WHERE (age = ?)
==> Parameters: 35(Integer)
<==    Columns: id, name, age, email
<==        Row: 3, Tom, 35, wrapperUpdate@baomidou.com
<==        Row: 5, Billie, 35, wrapperUpdate@baomidou.com
<==      Total: 2

8. 根据 Wrapper 条件,批量查询Object集合

List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

例子:

    @Test
    public void testSelectObjs() {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("age", 35);
        List<Object> users = userMapper.selectObjs(wrapper);
        System.out.println(users);
    }

控制台打印出来的日志:

==>  Preparing: SELECT id,name,age,email FROM tb_user WHERE (age = ?)
==> Parameters: 35(Integer)
<==    Columns: id, name, age, email
<==        Row: 3, Tom, 35, wrapperUpdate@baomidou.com
<==        Row: 5, Billie, 35, wrapperUpdate@baomidou.com
<==      Total: 2

9. 根据 Page 和 Wrapper 条件,分页查询Entity记录

<P extends IPage<T>> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

例子:

    @Test
    public void testSelectPage() {
        // 查询第1页,每页显示1条记录
        Page<UserEntity> page = new Page<>(1, 1);
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("age", 35);
        Page<UserEntity> pageResult = userMapper.selectPage(page, wrapper);
        System.out.println(JSON.toJSONString(pageResult));
    }

控制台打印出来的日志:

==>  Preparing: SELECT COUNT(*) FROM tb_user WHERE (age = ?)
==> Parameters: 35(Integer)
<==    Columns: COUNT(*)
<==        Row: 2
<==      Total: 1
==>  Preparing: SELECT id,name,age,email FROM tb_user WHERE (age = ?) LIMIT ?
==> Parameters: 35(Integer), 1(Long)
<==    Columns: id, name, age, email
<==        Row: 3, Tom, 35, wrapperUpdate@baomidou.com
<==      Total: 1

查询结果:

{
	"current": 1,
	"optimizeCountSql": true,
	"orders": [],
	"pages": 2,
	"records": [
		{
			"age": 35,
			"email": "wrapperUpdate@baomidou.com",
			"id": 3,
			"name": "Tom"
		}
	],
	"searchCount": true,
	"size": 1,
	"total": 2
}

注意:必须配置数据库方言,分页查询才会生效。

数据库方言配置:

@Configuration
public class MyBatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 配置自己使用的数据库,这里我选择MYSQL
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

10. 根据 Page 和 Wrapper 条件,分页查询Map记录

<P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

例子:

    @Test
    public void testSelectMapsPage() {
        Page<Map<String, Object>> page = new Page<>(1, 1);
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("age", 35);
        Page<Map<String, Object>> pageResult = userMapper.selectMapsPage(page, wrapper);
        System.out.println(JSON.toJSONString(pageResult));
    }

注意:定义Page的时候,泛型传Map,不要传Entity。

控制台打印出来的日志:

==>  Preparing: SELECT COUNT(*) FROM tb_user WHERE (age = ?)
==> Parameters: 35(Integer)
<==    Columns: COUNT(*)
<==        Row: 2
<==      Total: 1
==>  Preparing: SELECT id,name,age,email FROM tb_user WHERE (age = ?) LIMIT ?
==> Parameters: 35(Integer), 1(Long)
<==    Columns: id, name, age, email
<==        Row: 3, Tom, 35, wrapperUpdate@baomidou.com
<==      Total: 1

查询结果:

{
	"current": 1,
	"optimizeCountSql": true,
	"orders": [],
	"pages": 2,
	"records": [
		{
			"name": "Tom",
			"id": 3,
			"age": 35,
			"email": "wrapperUpdate@baomidou.com"
		}
	],
	"searchCount": true,
	"size": 1,
	"total": 2
}

标签:Mapper,age,wrapper,Plus,user,MyBatis,tb,public,Preparing
来源: https://blog.csdn.net/hbtj_1216/article/details/120250692