java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > mybatisplus crud操作

MyBatis-Plus的基本CRUD操作大全

作者:空空kkk

本文介绍了MyBatis-Plus框架中BaseMapper和IService两大核心组件的使用,,通过示例代码演示了如何实现数据插入、删除、修改、查询等操作,并展示了条件构造器、分页查询、事务处理等实用功能,感兴趣的朋友跟随小编一起看看吧

一、BaseMapper

        MyBatis-Plus中的基本CRUD在内置的BaseMapper中都已得到了实现,我们可以直接使用,接口如

public interface BaseMapper<T> extends Mapper<T> {
    int insert(T entity);
    int deleteById(Serializable id);
    int deleteById(T entity);
    int deleteByMap(@Param("cm") Map<String, Object> columnMap);
    int delete(@Param("ew") Wrapper<T> queryWrapper);
    int deleteBatchIds(@Param("coll") Collection<?> idList);
    int updateById(@Param("et") T entity);
    int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
    T selectById(Serializable id);
    List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
    List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
    default T selectOne(@Param("ew") 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", new Object[0]);
            } else {
                return ts.get(0);
            }
        } else {
            return null;
        }
    }
    default boolean exists(Wrapper<T> queryWrapper) {
        Long count = this.selectCount(queryWrapper);
        return null != count && count > 0L;
    }
    Long selectCount(@Param("ew") Wrapper<T> queryWrapper);
    List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
    List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
    List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
    <P extends IPage<T>> P selectPage(P page, @Param("ew") Wrapper<T> queryWrapper);
    <P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param("ew") Wrapper<T> queryWrapper);
}

1. 插入

        注意:MyBatis-Plus在实现插入数据时,会默认基于雪花算法的策略生成id

    /**
     * 用户插入功能的方法。
     * 该方法创建一个User对象并将其插入数据库,验证插入操作是否成功,
     * 并输出受影响的行数以及自动生成的用户ID。
     */
    @Test
    public void testInsert(){
        // 创建一个新的User对象,ID为null表示由数据库自动生成
        User user = new User(null, "张三", 23, "zhangsan@qcby.com",null,null);
        // 执行插入操作,将用户数据保存到数据库中
        //INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
        int result = userMapper.insert(user);
        // 输出插入操作影响的行数,正常情况下应为1
        System.out.println("受影响行数:"+result);
        // 输出数据库自动生成的用户ID,MyBatis-Plus在实现插入数据时,会默认基于雪花算法的策略生成id
        //2020478658078167042
        System.out.println("id自动获取:"+user.getId());
    }

2. 删除

    /**
     * 通过ID删除用户信息的功能。
     * 该方法调用userMapper的deleteById方法,传入指定的用户ID进行删除操作,
     * 并输出受影响的行数以验证删除结果。
     */
    @Test
    public void testDeleteById(){
        //通过id删除用户信息
        //DELETE FROM user WHERE id=?
        int result = userMapper.deleteById(2020488576042545170L);
        System.out.println("受影响行数:"+result);
    }
    @Test
    public void testDeleteBatchIds(){
        //通过多个id批量删除
        //DELETE FROM user WHERE id IN ( ? , ? )
        List<Long> idList = Arrays.asList(1L, 3L);
        int result = userMapper.deleteBatchIds(idList);
        System.out.println("受影响行数:"+result);
    }
    /**
     * 通过Map条件删除用户记录的功能。
     * 该方法构造一个包含删除条件的Map对象,并调用userMapper的deleteByMap方法执行删除操作,
     * 最后输出受影响的行数。
     * @param map 包含删除条件的键值对集合,其中key为字段名,value为对应的条件值。。
     * @return 受影响的行数,表示成功删除的记录数量。
     */
    @Test
    public void testDeleteByMap(){
        //根据map集合中所设置的条件删除记录
        // 构造删除条件的Map集合
        //DELETE FROM user WHERE name = ? AND age = ?
        Map<String, Object> map = new HashMap<>();
        map.put("age", 23);
        map.put("name", "张三");
        int result = userMapper.deleteByMap(map);
        System.out.println("受影响行数:"+result);
    }

3. 修改

    /**
     * 通过ID更新用户信息的功能。
     * 该方法创建一个User对象,并调用userMapper的updateById方法来更新数据库中的对应记录。
     * 最后输出受影响的行数。
     */
    @Test
    public void testUpdateById(){
        //创建User对象,并设置要修改的属性,null不传参
        User user = new User(4L, "test", null, null, null, null);
        //UPDATE user SET name=?, age=? WHERE id=?
        int result = userMapper.updateById(user);
        System.out.println("受影响行数:"+result);
    }

4. 查询

    /**
     * 通过ID查询用户信息的功能。
     * 该方法调用userMapper的selectById方法,传入指定的用户ID进行查询,
     * 并将查询结果打印到控制台。
     */
    @Test
    public void testSelectById(){
        //根据id查询用户信息
        //SELECT id,name,age,email FROM user WHERE id=?
        User user = userMapper.selectById(4L);
        System.out.println(user);
    }
    /**
     * 通过多个ID批量查询用户信息的功能。
     * 该方法调用userMapper的selectBatchIds方法,传入指定的用户ID列表进行批量查询,
     * 并将查询结果打印到控制台。
     */
    @Test
    public void testSelectBatchIds(){
        //根据id批量查询
        //SELECT id,name,age,email FROM user WHERE id IN ( ? , ? )
        List<User> list = userMapper.selectBatchIds(Arrays.asList(4, 2));
        list.forEach(System.out::println);
    }
    /**
     * 通过Map条件查询用户信息列表的功能。
     * 该方法构造一个包含查询条件的Map对象,并调用userMapper的selectByMap方法执行查询操作,
     * 最后将查询结果打印到控制台。
     */
    @Test
    public void testSelectByMap(){
        //根据map查询用户信息,此时null传参
        //SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
        Map<String, Object> map = new HashMap<>();
        map.put("name", "Jack");
        map.put("age", 20);
        List<User> list = userMapper.selectByMap(map);
        list.forEach(System.out::println);
    }
    /**
     * 通过条件查询用户信息列表的功能。
     * 该方法创建一个User对象,并调用userMapper的selectOne方法来查询数据库中的对应记录。
     * 最后将查询结果打印到控制台。
     */
    @Test
    public void testSelectOne(){
        //查询用户信息,根据条件查询,最多只能查询一条
        //SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
        User user = new User();
        user.setName("Jack");
        user.setAge(20);
        User one = userMapper.selectOne(new QueryWrapper<>(user));
        System.out.println(one);
    }
    /**
     * 查询所有数据selectList()功能。
     * 该方法通过调用userMapper的selectList()方法查询数据,
     * 并将查询结果打印到控制台。
     * selectList()方法根据MyBatis-Plus内置的条件构造器查询一个list集合。
     * 传入null表示没有查询条件,即查询所有数据。
     */
    @Test
    public void testSelectList(){
        //selectList()根据MP内置的条件构造器查询一个list集合,null表示没有条件,即查询所有
        userMapper.selectList( null).forEach(System.out::println);
    }

5. 其他重要接口

    // selectPage(分页查询 + 复杂条件)
    @Test
    public void testSelectPage() {
        // 分页参数:第2页,每页3条
        Page<User> page = new Page<>(2, 3);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("age", 18) // 年龄>18
                .like("email", "@test.com") // 邮箱包含@test.com
                .orderByDesc("age"); // 按年龄降序
        IPage<User> userIPage = userMapper.selectPage(page, queryWrapper);
        System.out.println("selectPage-总条数:" + userIPage.getTotal());
        System.out.println("selectPage-当前页数据:");
        userIPage.getRecords().forEach(System.out::println);
    }
    // selectMapsPage(分页查询,返回Map集合,仅查指定字段)
    @Test
    public void testSelectMapsPage() {
        Page<Map<String, Object>> page = new Page<>(1, 5);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("uid", "name", "age") // 只查3个字段
                .between("age", 18, 30); // 年龄在18-30之间
        IPage<Map<String, Object>> mapIPage = userMapper.selectMapsPage(page, queryWrapper);
        System.out.println("selectMapsPage-总页数:" + mapIPage.getPages());
        System.out.println("selectMapsPage-当前页Map数据:");
        mapIPage.getRecords().forEach(System.out::println);
    }
    // selectObjs(仅查询指定字段,返回Object集合,适合单字段查询)
    @Test
    public void testSelectObjs() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name") // 只查name字段
                .eq("age", 20);
        List<Object> nameList = userMapper.selectObjs(queryWrapper);
        System.out.println("selectObjs-仅查询name字段结果:");
        nameList.forEach(System.out::println);
    }
    // update(UpdateWrapper条件更新,非ID更新)
    @Test
    public void testUpdateWithWrapper() {
        User user = new User();
        user.setEmail("update_wrapper@test.com"); // 要更新的字段
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("name", "张三")
                .gt("age", 18); // 条件:name=张三 且 age>18
        int rows = userMapper.update(user, updateWrapper);
        System.out.println("testUpdateWithWrapper-影响行数:" + rows);
    }
    // update(链式UpdateWrapper,直接设置更新字段,无需new User)
    @Test
    public void testUpdateForSet() {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uid", 1L)
                .set("age", 28) // 直接设置要更新的字段和值
                .set("email", "update_set@test.com");
        int rows = userMapper.update(null, updateWrapper);
        System.out.println("testUpdateForSet-影响行数:" + rows);
    }
    // delete(QueryWrapper条件删除,支持复杂条件)
    @Test
    public void testDeleteWithWrapper() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", "测试删除")
                .lt("age", 18); // 条件:name含测试删除 且 age<18
        int rows = userMapper.delete(queryWrapper);
        System.out.println("testDeleteWithWrapper-影响行数:" + rows);
    }

二、IServce

        MyBatis-Plus中有一个接口 IService和其实现类 ServiceImpl,封装了常见的业务层逻辑。采用get查询单行、remove删除、list查询集合、page分页前缀命名方式,区分mapper层,避免混淆。接口如下

public interface IService<T> {
    int DEFAULT_BATCH_SIZE = 1000;
    default boolean save(T entity) {
        return SqlHelper.retBool(this.getBaseMapper().insert(entity));
    }
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean saveBatch(Collection<T> entityList) {
        return this.saveBatch(entityList, 1000);
    }
    boolean saveBatch(Collection<T> entityList, int batchSize);
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean saveOrUpdateBatch(Collection<T> entityList) {
        return this.saveOrUpdateBatch(entityList, 1000);
    }
    boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
    default boolean removeById(Serializable id) {
        return SqlHelper.retBool(this.getBaseMapper().deleteById(id));
    }
    default boolean removeById(Serializable id, boolean useFill) {
        throw new UnsupportedOperationException("不支持的方法!");
    }
    default boolean removeById(T entity) {
        return SqlHelper.retBool(this.getBaseMapper().deleteById(entity));
    }
    default boolean removeByMap(Map<String, Object> columnMap) {
        Assert.notEmpty(columnMap, "error: columnMap must not be empty", new Object[0]);
        return SqlHelper.retBool(this.getBaseMapper().deleteByMap(columnMap));
    }
    default boolean remove(Wrapper<T> queryWrapper) {
        return SqlHelper.retBool(this.getBaseMapper().delete(queryWrapper));
    }
    default boolean removeByIds(Collection<?> list) {
        return CollectionUtils.isEmpty(list) ? false : SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(list));
    }
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean removeByIds(Collection<?> list, boolean useFill) {
        if (CollectionUtils.isEmpty(list)) {
            return false;
        } else {
            return useFill ? this.removeBatchByIds(list, true) : SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(list));
        }
    }
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean removeBatchByIds(Collection<?> list) {
        return this.removeBatchByIds(list, 1000);
    }
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean removeBatchByIds(Collection<?> list, boolean useFill) {
        return this.removeBatchByIds(list, 1000, useFill);
    }
    default boolean removeBatchByIds(Collection<?> list, int batchSize) {
        throw new UnsupportedOperationException("不支持的方法!");
    }
    default boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill) {
        throw new UnsupportedOperationException("不支持的方法!");
    }
    default boolean updateById(T entity) {
        return SqlHelper.retBool(this.getBaseMapper().updateById(entity));
    }
    default boolean update(Wrapper<T> updateWrapper) {
        return this.update((Object)null, updateWrapper);
    }
    default boolean update(T entity, Wrapper<T> updateWrapper) {
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
    }
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean updateBatchById(Collection<T> entityList) {
        return this.updateBatchById(entityList, 1000);
    }
    boolean updateBatchById(Collection<T> entityList, int batchSize);
    boolean saveOrUpdate(T entity);
    default T getById(Serializable id) {
        return this.getBaseMapper().selectById(id);
    }
    default List<T> listByIds(Collection<? extends Serializable> idList) {
        return this.getBaseMapper().selectBatchIds(idList);
    }
    default List<T> listByMap(Map<String, Object> columnMap) {
        return this.getBaseMapper().selectByMap(columnMap);
    }
    default T getOne(Wrapper<T> queryWrapper) {
        return this.getOne(queryWrapper, true);
    }
    T getOne(Wrapper<T> queryWrapper, boolean throwEx);
    Map<String, Object> getMap(Wrapper<T> queryWrapper);
    <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
    default long count() {
        return this.count(Wrappers.emptyWrapper());
    }
    default long count(Wrapper<T> queryWrapper) {
        return SqlHelper.retCount(this.getBaseMapper().selectCount(queryWrapper));
    }
    default List<T> list(Wrapper<T> queryWrapper) {
        return this.getBaseMapper().selectList(queryWrapper);
    }
    default List<T> list() {
        return this.list(Wrappers.emptyWrapper());
    }
    default <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper) {
        return this.getBaseMapper().selectPage(page, queryWrapper);
    }
    default <E extends IPage<T>> E page(E page) {
        return this.page(page, Wrappers.emptyWrapper());
    }
    default List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper) {
        return this.getBaseMapper().selectMaps(queryWrapper);
    }
    default List<Map<String, Object>> listMaps() {
        return this.listMaps(Wrappers.emptyWrapper());
    }
    default List<Object> listObjs() {
        return this.listObjs(Function.identity());
    }
    default <V> List<V> listObjs(Function<? super Object, V> mapper) {
        return this.listObjs(Wrappers.emptyWrapper(), mapper);
    }
    default List<Object> listObjs(Wrapper<T> queryWrapper) {
        return this.listObjs(queryWrapper, Function.identity());
    }
    default <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
        return (List)this.getBaseMapper().selectObjs(queryWrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList());
    }
    default <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<T> queryWrapper) {
        return this.getBaseMapper().selectMapsPage(page, queryWrapper);
    }
    default <E extends IPage<Map<String, Object>>> E pageMaps(E page) {
        return this.pageMaps(page, Wrappers.emptyWrapper());
    }
    BaseMapper<T> getBaseMapper();
    Class<T> getEntityClass();
    default QueryChainWrapper<T> query() {
        return ChainWrappers.queryChain(this.getBaseMapper());
    }
    default LambdaQueryChainWrapper<T> lambdaQuery() {
        return ChainWrappers.lambdaQueryChain(this.getBaseMapper());
    }
    default KtQueryChainWrapper<T> ktQuery() {
        return ChainWrappers.ktQueryChain(this.getBaseMapper(), this.getEntityClass());
    }
    default KtUpdateChainWrapper<T> ktUpdate() {
        return ChainWrappers.ktUpdateChain(this.getBaseMapper(), this.getEntityClass());
    }
    default UpdateChainWrapper<T> update() {
        return ChainWrappers.updateChain(this.getBaseMapper());
    }
    default LambdaUpdateChainWrapper<T> lambdaUpdate() {
        return ChainWrappers.lambdaUpdateChain(this.getBaseMapper());
    }
    default boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper) {
        return this.update(entity, updateWrapper) || this.saveOrUpdate(entity);
    }
}

1. 创建Service接口和实现类

/**
* UserService继承IService模板提供的基础功能
*/
public interface UserService extends IService<User> {}
/**
* ServiceImpl实现了IService,提供了IService中基础功能的实现
* 若ServiceImpl无法满足业务需求,则可以使用自定的UserService定义方法,并在实现类中实现 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {}

2. 测试查询记录数

    @Autowired
    private UserService userService;
    @Test
    public void testGetCount(){
        long count = userService.count();
        System.out.println("总记录数:" + count);
    }

3. 测试批量插入

    /**
     * 批量插入
     */
    @Test
    public void testSaveBatch(){
        // SQL长度有限制,海量数据插入单条SQL无法实行,
        // 因此MP将批量插入放在了通用Service中实现,而不是通用Mapper
        ArrayList<User> users = new ArrayList<>();
        // 未传参的字段默认为null,但是id雪花自增
        for (int i = 0; i < 5; i++) {
            User user = new User();
            // 字符串拼接
            user.setName("kk" + i);
            // 加法运算
            user.setAge(20 + i);
            users.add(user);
        }
        //SQL:INSERT INTO user ( username, age ) VALUES ( ?, ? )
        userService.saveBatch(users);
    }

4. 其他常用接口

    // 测试saveOrUpdate(新增或更新:有ID则更新,无则新增)
    @Test
    public void testSaveOrUpdate() {
        // 场景1:无ID → 新增
        User user1 = new User();
        user1.setName("新增或更新1");
        user1.setAge(23);
        user1.setEmail("saveOrUpdate1@test.com");
        userService.saveOrUpdate(user1);
        // 场景2:有ID → 更新
        User user2 = new User();
        user2.setId(1L);
        user2.setName("新增或更新2");
        user2.setAge(24);
        userService.saveOrUpdate(user2);
    }
    // 测试saveOrUpdateBatch(批量新增或更新)
    @Test
    public void testSaveOrUpdateBatch() {
        User user1 = new User();
        user1.setId(1L); // 有ID → 更新
        user1.setName("批量更新");
        user1.setAge(25);
        User user2 = new User(); // 无ID → 新增
        user2.setName("批量新增");
        user2.setAge(26);
        List<User> userList = Arrays.asList(user1, user2);
        boolean result = userService.saveOrUpdateBatch(userList);
        System.out.println("批量新增/更新是否成功:" + result);
    }
    // 测试updateBatchById(批量更新(按ID))
    @Test
    public void testUpdateBatchById() {
        User user1 = new User();
        user1.setId(1L);
        user1.setAge(30);
        User user2 = new User();
        user2.setId(2L);
        user2.setAge(31);
        List<User> userList = Arrays.asList(user1, user2);
        boolean result = userService.updateBatchById(userList);
        System.out.println("批量更新是否成功:" + result);
    }
    // 测试removeById(根据ID删除)
    @Test
    public void testRemoveById() {
        boolean result = userService.removeById(1L);
        System.out.println("删除是否成功:" + result);
    }
    // 测试removeByIds(批量删除)
    @Test
    public void testRemoveByIds() {
        List<Long> idList = Arrays.asList(7L, 8L);
        boolean result = userService.removeByIds(idList);
        System.out.println("批量删除是否成功:" + result);
    }
    // 测试remove(按条件删除)
    @Test
    public void testRemove() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getName, "测试删除").eq(User::getAge, 0);
        boolean result = userService.remove(queryWrapper);
        System.out.println("条件删除是否成功:" + result);
    }
    // 测试getById(根据ID查询)
    @Test
    public void testGetById() {
        User user = userService.getById(1L);
        System.out.println(user);
    }
    // 测试listByIds(批量查询)
    @Test
    public void testListByIds() {
        List<Long> idList = Arrays.asList(1L, 2L);
        List<User> userList = userService.listByIds(idList);
        userList.forEach(System.out::println);
    }
    // 测试listPage(分页查询)
    @Test
    public void testListPage() {
        Page<User> page = new Page<>(1, 5);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(User::getAge, 18);
        IPage<User> userIPage = userService.page(page, queryWrapper);
        System.out.println("总条数:" + userIPage.getTotal());
        userIPage.getRecords().forEach(System.out::println);
    }
    // 测试count(统计总数)
    @Test
    public void testCount() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(User::getAge, 20);
        long count = userService.count(queryWrapper);
        System.out.println("年龄大于20的用户数:" + count);
    }
    // 测试链式查询(LambdaQueryChainWrapper)
    @Test
    public void testLambdaQueryChain() {
        List<User> userList = new LambdaQueryChainWrapper<>(userService.getBaseMapper())
                .eq(User::getAge, 20)
                .like(User::getName, "张三")
                .list();
        userList.forEach(System.out::println);
    }
    // 测试事务性批量操作(@Transactional)
    @Test
    @Transactional // 测试完成后回滚,避免污染数据
    public void testTransactionalBatch() {
        // 批量新增
        List<User> saveList = Arrays.asList(
                new User(null, "事务测试1", 28, "tx1@test.com", SexEnum.MALE, null),
                new User(null, "事务测试2", 29, "tx2@test.com", SexEnum.FEMALE,null)
        );
        userService.saveBatch(saveList);
        // 批量更新
        List<User> updateList = Arrays.asList(
                new User(2026L, "事务更新1", 30, null, SexEnum.MALE, null),
                new User(2027L, "事务更新2", 31, null, SexEnum.FEMALE, null)
        );
        userService.updateBatchById(updateList);
        // 验证(事务内可查,事务外回滚)
        System.out.println("事务内查询:" + userService.getById(2026L));
    }

到此这篇关于MyBatis-Plus的基本CRUD的文章就介绍到这了,更多相关mybatisplus crud操作内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文