代码编织梦想

SpringCloud 大型系列课程正在制作中,欢迎大家关注与提意见。
程序员每天的CV 与 板砖,也要知其所以然,本系列课程可以帮助初学者学习 SpringBooot 项目开发 与 SpringCloud 微服务系列项目开发

1 项目准备

创建SpringBoot基础项目
SpringBoot项目集成mybatis
SpringBoot 集成 Druid 数据源【SpringBoot系列3】
SpringBoot MyBatis 实现分页查询数据【SpringBoot系列4】

官网地址:https://baomidou.com/

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

在 SpringBoot 项目的 pom.xml 中添加 Mybatis-plus 依赖

<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3.1</version>
</dependency>

2 Mybatis-plus 的基本使用

在项目中的 UserMapper,java 中 继承 BaseMapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.biglead.demo.pojo.UserInfo;
import org.mybatis.spring.annotation.MapperScan;
import java.util.List;

@MapperScan
public interface UserMapper extends BaseMapper<UserInfo> {
    List<UserInfo> selectList();
    
    /**
     * 分页查询用户
     * @return
     */
    List<UserInfo> selectPage();
}

BaseMapper 是 Mybatis-plus 中提供的一个基类,其中封装了基础的增删改查的功能 :

public interface BaseMapper<T> extends Mapper<T> {
    //插入
    int insert(T entity);
    
	//通过主键删除
    int deleteById(Serializable id);
	//通过Map字段对应值删除
    int deleteByMap(@Param("cm") Map<String, Object> columnMap);
	//通过条件Wrapper删除
    int delete(@Param("ew") Wrapper<T> wrapper);
	//批量删除
    int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);

    //更新 通过ID匹配
    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);
	//查询通过Map
    List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
	//通过查询条件构造器查询,返回实体
    T selectOne(@Param("ew") Wrapper<T> queryWrapper);
	//通过查询条件构造器查询行数
    Integer 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);
	//分页查询
    <E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);

    <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);
}

然后使用 MyBatis-Plus 可以简便的完成数据的增删改查


@SpringBootTest
class UserServiceTests {

    @Resource
    UserMapper userMapper;

    @Test
    void testInsertData() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName("测试用户");
        userInfo.setUserAge(33);
        //插入数据
        userMapper.insert(userInfo);
    }

}

还可以使 UserServiceImpl 继承 ServiceImpl 来实现便捷的增删改查功能

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,UserInfo> implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public UserInfo newUser(UserInfo info) {
        //保存用户
        this.save(info);
        return info;
    }
    @Override
    public UserInfo updateUser(UserInfo info) {
        //根据ID来更新用户信息
        this.updateById(info);
        return info;
    }
    @Override
    public UserInfo delete(UserInfo info) {
        //根据ID来删除用户信息
        this.delete(info);
        return info;
    }
  }

项目源码在这里 :https://gitee.com/android.long/spring-boot-study/tree/master/biglead-api-05-mybatis-plus
有兴趣可以关注一下公众号:biglead

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zl18603543572/article/details/129538116

springboot集成mybatis-plus-爱代码爱编程

1、mybatis-plus简介 MyBatis-Plus (简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。 官网:https://mp.baomidou.com/ github: https://github.com/baomidou/mybatis-plus   2、新

SpringBoot集成Mybatis-Plus框架-爱代码爱编程

SpringBoot集成Mybatis-Plus框架 MyBatis-Plus(简称 MP)是一个MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。 具体可以参考MyBatis-Plus官网:MyBatis-Plus官网 目录 SpringBoot集成Mybatis-Plus框架环境数据库表Ja

SpringBoot集成Mybatis-Plus及Mybatis-Plus简单操作-爱代码爱编程

一、SpringBoot集成Mybatis-Plus方法 创建springboot项目 这里可以选择MySQL坐标,也可以不选然后自己手动添加 添加依赖 <!--mybatis-plus--> <dependency> <groupId>com.baomi

springboot集成mybatis-plus实现分页_evenboy的博客-爱代码爱编程

1、依赖导入 <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter --> <dependency> <groupId>com.baomidou</groupId> <artifac

springboot集成mybatis-plus + mybatis-plus代码生成器+swagger接口文档_骑猪兜风~的博客-爱代码爱编程

目录 创建一个新的spring web项目 配置.pom文件,引入jar依赖包 MyBatis-Plus依赖包 MyBatis-Plus代码生成器依赖包 删除.properties文件,或者将其修改为.yaml文件,配置.yaml文件 主程序入口进行注解配置 创建CodeGenerator代码生成器 配置Swagger 添加Swagger