当前位置: 首页 > news >正文

MybatisPlus(1)

前言🍭

❤️❤️❤️SSM专栏更新中,各位大佬觉得写得不错,支持一下,感谢了!❤️❤️❤️

Spring + Spring MVC + MyBatis_冷兮雪的博客-CSDN博客

 MyBatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。它提供了一些常用的 CRUD 操作,以及分页、动态 SQL 等常用功能,同时也支持自定义 SQL 语句和存储过程。  

一、MybatisPlus简介🍭

MyBatis-Plus官网有两个,第一个域名是热心网友捐赠的(之前已经被申请过了),第二个是正牌官网(国人开发的,为中文)。

MyBatis-Plus 
MyBatis-Plus (baomidou.com)

我们可以跟着官网学,这个<快速开始>十分照顾新手。

1、 MybatisPlus特性🍉

  • 无侵入: 只做增强不做改变,不会对现有工程产生影响
  • 强大的 CRUD 操作: 内置通用 Mapper,少量配置即可实现单表CRUD 操作
  • 支持 Lambda: 编写查询条件无需担心字段写错
  • 支持主键自动生成
  • 内置分页插件
  • ......

详情可见官网:

总结:使用MybatisPlus几乎可以让你什么都不写,代码简化到极致。

2、MyBatis-Plus历史发展🍉

MyBatis-Plus是一个基于MyBatis的增强工具库,旨在简化和增强MyBatis的开发。下面是MyBatis-Plus的历史发展的总结:

  1. 2012年:MyBatis-Plus的前身是一个名为MyBatis-Plus-Generator的代码生成器,由Javen开发并在GitHub上发布。该代码生成器可以根据数据库表结构自动生成MyBatis的实体类、Mapper接口和XML映射文件。
  2. 2016年:MyBatis-Plus开始独立发展,并发布了第一个版本。它提供了一系列的增强功能,包括通用Mapper、分页插件、逻辑删除、自动填充等,简化了MyBatis的开发。
  3. 2017年:MyBatis-Plus发布了2.0版本,引入了更多的增强功能,例如性能分析插件、动态表名、多租户支持等。
  4. 2018年:MyBatis-Plus发布了3.0版本,引入了Lambda表达式查询、代码生成器的可视化界面等功能,进一步提升了开发效率。
  5. 2019年:MyBatis-Plus发布了3.1版本,增加了更多的增强功能和改进,包括多数据源支持、全局拦截器等。
  6. 2020年:MyBatis-Plus发布了3.2版本,引入了更多的增强功能,如多租户数据隔离、性能优化等。
  7. 2021年:MyBatis-Plus发布了3.3版本,进一步完善了功能,并修复了一些bug。

截至目前,MyBatis-Plus已经成为了一个功能强大、稳定可靠的开发工具库,广泛应用于Java项目中,极大地简化了MyBatis的开发工作。它的持续发展得益于社区的贡献和活跃的维护。

更具体的可以看MyBatis-Plus官网更新日志:mybatis-plus/CHANGELOG.md at 3.0 · baomidou/mybatis-plus · GitHub

 二、MyBatis-Plus入门案例🍭

1、新建项目🍉

 只选择MySQL Driver(暂时不使用SpringWeb),MyBatis-Plus配置文件需要自己手动添加。

        <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.1</version></dependency>

并且不再需要导入 mybatis和mybatis整合spring的jar包:

 还有一个druid jar包:

        <dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency>

2、连接数据库🍉

# 配置数据库的连接字符串
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/ku2022?characterEncoding=utf8username: rootpassword: "123456"driver-class-name: com.mysql.cj.jdbc.Driver

所使用的库中需要有与user实体类名字相同的表:

 3、UserDao接口🍉

之前的Mapper需要写方法:

package com.example.ssmdemo1.mapper;import com.example.ssmdemo1.entity.Userinfo;
import org.apache.ibatis.annotations.Mapper;@Mapper//需要添加 @Mapper 注解
public interface UserMapper {Userinfo getUserById(Integer id);
}

MyBatis-Plus之后:

package com.example.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.domain.User;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface UserDao extends BaseMapper<User> {
}

点进BaseMapper中去,可以看到它自带了非常多的方法:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package com.baomidou.mybatisplus.core.mapper;import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;public interface BaseMapper<T> extends Mapper<T> {int insert(T entity);int deleteById(Serializable id);int deleteByMap(@Param("cm") Map<String, Object> columnMap);int delete(@Param("ew") Wrapper<T> queryWrapper);int deleteBatchIds(@Param("coll") Collection<? extends Serializable> 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);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);
}

就是这么简单,可以看到我什么都没写却仍然有很多方法可以使用:

4、单元测试🍉

package com.example.mybatisplus;import com.example.dao.UserDao;
import com.example.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
class MybatisPlusApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid textGetAll() {List<User> list=userDao.selectList(null);System.out.println(list);}}

输出数据库中的两条数据: 

 从上面入门案例我们可以很清楚了解到MyBatisPlus的方便性

三、标准数据层CRUD制作🍭

下面这些方法差不多将我们日常的需求都给覆盖了,而在MybatisPlus中也都有对应的方法,只不过换了个名字而已。

功能自定义接口MP接口
新增boolean save(T t)int insert(T t)
删除boolean delete(int id)int deleteById(Serializable id)
修改boolean update(T t)int updateById(T t)
根据id查询getById(int id)T selectById(Serializable id)
查询全部List<T> getAll()List<T> selectList()
分页查询PageInfo<T> getAll(int page,int size)IPage<T> selectPage(IPage<T> page)
按条件查询List<T> getAll(Condition condition)IPage<T> selectPage(Wrapper<T〉 queryWrapper)

 1、新增数据🍉

@Testvoid textSave(){User user=new User();user.setName("张三");user.setPassword("123456");user.setAge(19);user.setTel("123456789");userDao.insert(user);}

添加成功,只不过id是它给你指点的一个id,这样子是能够添加用户进去的。 

2、删除数据🍉

@Testvoid testDelete(){userDao.deleteById(1694537075610619906L);}

王五的数据就被删除了 

 3、更新数据🍉

@Testvoid testUpdate(){User user=new User();user.setId(1L);user.setName("张三");userDao.updateById(user);}

 将id为1的name更新为张三,但是我们可以发现其他没有赋值的数据都没有发生改变,不是为空

4、查找数据🍉

 入门案例中的就是查找数据,这不过是全部查询出来,那能不能查询单个数据?

@Testvoid testSelect(){User user=userDao.selectById(1L);System.out.println(user);}

可以发现也是可以的: 

http://www.lryc.cn/news/140621.html

相关文章:

  • 探索未来世界,解密区块链奥秘!
  • win10 下运行 npm run watch-poll问题
  • Android平台RTMP|RTSP直播播放器功能进阶探讨
  • Centos7安装Telnet服务
  • 【C++】GCC对应C++的版本支持
  • 前端面试:【算法】排序、查找、递归、动态规划
  • RK3399 开机自启一个shell脚本,一直起不来BUG
  • [MyBatis系列④]核心配置文件
  • 系统架构设计高级技能 · 层次式架构设计理论与实践
  • Nuxt3打包部署到Linux(node+pm2安装和运行步骤+nginx代理)
  • 一维数组传参
  • 七层、四层和五层网络模型区别和联系
  • RH1288V3 - 初识物理服务器
  • excel中如果A列中某项有多条记录,针对A列中相同的项,将B列值进行相加合并统计
  • 开发智能应用的新范式:大数据、AI和云原生如何构建智能软件
  • 淘宝免费爬虫数据 商品详情数据 商品销售额销量API
  • Markdown初级使用指南
  • 国际版阿里云/腾讯云CDN装备运用教程:加快网站拜访速度
  • 面试之快速学习计算机网络-http
  • 2023水果编曲软件fl studio 21.1.0 .3713官方中文直装破解版
  • 【微信小程序】页面路由跳转函数之间的区别
  • Ubuntu inotify
  • 开始MySQL之路——MySQL的DataGrip图形化界面
  • C++ STL 标准模板库
  • C#-集合小例子
  • git保存删除的文件
  • 【golang】go语句执行规则(goroutine)(下)
  • websocket 接收消息无法获取用户id
  • springboot通过sharding-dbc按年、月分片
  • 基于静电放电算法优化的BP神经网络(预测应用) - 附代码