打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SpringBoot MyBatis Plus 集成 【SpringBoot系列5】

1 项目准备

SpringBoot 搭建项目 【SpringBoot系列1】

SpringBoot 集成 MyBatis 框架 【SpringBoot系列2】

SpringBoot 集成 Druid 数据源【SpringBoot系列3】

SpringBoot MyBatis 实现分页查询数据【SpringBoot系列4】


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

在实际业务开发中,使用MyBatis Plus 来实现一些基本数据的增删改查是明显可以提高开发速度的。

在 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>

然后在项目中的 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;
@MapperScanpublic 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 可以简便的完成数据的增删改查


@SpringBootTestclass 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;@Servicepublic 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;    }  }

特别说明 :

需要在启动类上配置 @MapperScan 扫描包路径

@SpringBootApplication@MapperScan(basePackages = "com.biglead.demo.mapper")public class DemoApplication {
public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args);  }}

这里配置的就是 UserMapper,java 所在的包路径

application.yml 兼容配置

将 mybatis 的包扫描路径删除 ,使用mybatis-plus 的,比如我这里的

mybatis:  configuration:    log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl#mybatismybatis-plus:  mapper-locations: classpath*:**/sqlmap/*.xml  #实体扫描,多个package用逗号或者分号分隔  typeAliasesPackage: com.biglead.demo.pojo  configuration:    map-underscore-to-camel-case: true    #缓存    cache-enabled: false    call-setters-on-nulls: true   #配置日志  log-impl:日志实现    log-impl : org.apache.ibatis.logging.stdout.StdOutImpl

项目源码在这里:(也可以点击查看原文)

https://gitee.com/android.long/spring-boot-study/tree/master/biglead-api-05-mybatis-plus
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
MyBatis-Plus配置日志与CRUD
5.mybatisPlus自定义SQL_mybatisplus 自定义sql
熟练掌握 mybatis-plus,一篇就够!
LambdaQueryWrapper&QueryWrapper增删改CURD使用教程案例
Mybatis-Plus3.0入门手册
Spring Boot 整合 MyBatis
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服