Java mybatis-plus详解
作者:--流星。 时间:2023-03-28 19:49:04
目录
1、简介
2、适用情况
3、mybatis-plus前期准备(工程将以 H2 作为默认数据库进行演示)
1、使用 Spring Initializer快速初始化一个 Spring Boot 工程
2、导入mybatis-plus依赖
3、yml文件中添加相关配置
4、在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹
5、编写实体类和Mapper类
6、service继承IService
7、serviceImpl继承ServiceImpl
4、mybatis-plus的sql操作(Service层)
1、Save:插入
2、SaveOrUpdate:修改插入
3、Remove:删除
4、Update:更新
5、Get:单体查询
6、List:多条查询
7、Page:分页查询(需要导入相关的配置或依赖)
8、Count:记录数据个数
5、详细资料
总结
1、简介
MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在。
2、适用情况
1、对于只进行单表操作来说,mybatis-plus代码量比mybatis的代码量少很多,极大的提高了开发效率
2、对于多表操作来说,更推荐mybatis,因为mybatis-plus的方法比较难以理解,用起来不太方便,不如自己写sql语句的逻辑那么清晰明了
3、mybatis-plus前期准备(工程将以 H2 作为默认数据库进行演示)
1、使用 Spring Initializer快速初始化一个 Spring Boot 工程
2、导入mybatis-plus依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>spring-latest-version</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>Latest Version</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
3、yml文件中添加相关配置
# DataSource Config
spring:
datasource:
driver-class-name: org.h2.Driver
schema: classpath:db/schema-h2.sql
data: classpath:db/data-h2.sql
url: jdbc:h2:mem:test
username: root
password: test
4、在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
5、编写实体类和Mapper类
//entity
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
//Mapper
public interface UserMapper extends BaseMapper<User> {
}
6、service继承IService
public interface UserService extends IService<User>
7、serviceImpl继承ServiceImpl
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService
4、mybatis-plus的sql操作(Service层)
1、Save:插入
// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
// 插入(批量)
boolean saveBatch(Collection<T> entityList);
// 插入(批量)
boolean saveBatch(Collection<T> entityList, int batchSize);
2、SaveOrUpdate:修改插入
// TableId 注解存在更新记录,否插入一条记录
boolean saveOrUpdate(T entity);
// 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
3、Remove:删除
// 根据 entity 条件,删除记录
boolean remove(Wrapper<T> queryWrapper);
// 根据 ID 删除
boolean removeById(Serializable id);
// 根据 columnMap 条件,删除记录
boolean removeByMap(Map<String, Object> columnMap);
// 删除(根据ID 批量删除)
boolean removeByIds(Collection<? extends Serializable> idList);
4、Update:更新
// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
boolean update(Wrapper<T> updateWrapper);
// 根据 whereWrapper 条件,更新记录
boolean update(T updateEntity, Wrapper<T> whereWrapper);
// 根据 ID 选择修改
boolean updateById(T entity);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList, int batchSize);
5、Get:单体查询
// 根据 ID 查询
T getById(Serializable id);
// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
T getOne(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
// 根据 Wrapper,查询一条记录
Map<String, Object> getMap(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper)
6、List:多条查询
// 查询所有
List<T> list();
// 查询列表
List<T> list(Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
Collection<T> listByIds(Collection<? extends Serializable> idList);
// 查询(根据 columnMap 条件)
Collection<T> listByMap(Map<String, Object> columnMap);
// 查询所有列表
List<Map<String, Object>> listMaps();
// 查询列表
List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
// 查询全部记录
List<Object> listObjs();
// 查询全部记录
<V> List<V> listObjs(Function<? super Object, V> mapper);
// 根据 Wrapper 条件,查询全部记录
List<Object> listObjs(Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录
<V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
7、Page:分页查询(需要导入相关的配置或依赖)
// 无条件分页查询
IPage<T> page(IPage<T> page);
// 条件分页查询
IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
// 无条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page);
// 条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
8、Count:记录数据个数
// 查询总记录数
int count();
// 根据 Wrapper 条件,查询总记录数
int count(Wrapper<T> queryWrapper);
5、详细资料
由于篇幅有限,先写到这里
具体内容请看
1、mybatis-plus官网:https://mp.baomidou.com/
2、MyBatis-Plus 通用IService使用详解
来源:https://blog.csdn.net/weixin_45974176/article/details/120269903
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java面向对象之猜拳游戏
![](https://img.aspxhome.com/file/2023/8/80208_0s.jpg)
java LeetCode刷题稍有难度的贪心构造算法
![](https://img.aspxhome.com/file/2023/9/79209_0s.png)
Java注解与反射原理说明
winform 实现选择文件和选择文件夹对话框的简单实例
JavaWeb入门:HttpResponse和HttpRequest详解
![](https://img.aspxhome.com/file/2023/7/82997_0s.png)
Android应用开发SharedPreferences存储数据的使用方法
![](https://img.aspxhome.com/file/2023/0/82410_0s.jpg)
javafx tableview鼠标触发更新属性详解
Java建造者模式构建复杂对象的最佳实践
![](https://img.aspxhome.com/file/2023/8/61808_0s.png)
C#导出数据到CSV文件的通用类实例
C#检测是否有危险字符的SQL字符串过滤方法
Java基础之异常处理详解
![](https://img.aspxhome.com/file/2023/8/88518_0s.webp)
浅谈Java获得多线程的返回结果方式(3种)
SpringMVC记录我遇到的坑_AOP注解无效,切面不执行的解决
全面解析JTA 深度历险
ReentrantLock源码详解--公平锁、非公平锁
![](https://img.aspxhome.com/file/2023/1/69691_0s.png)
C#正则表达式分解和转换IP地址实例(C#正则表达式大全 c#正则表达式语法)
Spring Security 实现短信验证码登录功能
![](https://img.aspxhome.com/file/2023/6/72326_0s.jpg)
Android开发中的9个常见错误和解决方法
解决在Unity中使用FairyGUI遇到的坑
![](https://img.aspxhome.com/file/2023/2/122732_0s.png)