Java中mybatis-plus怎么用
发表于:2024-10-10 作者:千家信息网编辑
千家信息网最后更新 2024年10月10日,这篇文章主要为大家展示了"Java中mybatis-plus怎么用",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Java中mybatis-plus怎么用"
千家信息网最后更新 2024年10月10日Java中mybatis-plus怎么用1、使用 Spring Initializer快速初始化一个 Spring Boot 工程
2、SaveOrUpdate:修改插入
这篇文章主要为大家展示了"Java中mybatis-plus怎么用",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Java中mybatis-plus怎么用"这篇文章吧。
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依赖
org.springframework.boot spring-boot-starter-parent spring-latest-version org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test com.baomidou mybatis-plus-boot-starter Latest Version com.h3database h3 runtime
3、yml文件中添加相关配置
# DataSource Configspring: datasource: driver-class-name: org.h3.Driver schema: classpath:db/schema-h3.sql data: classpath:db/data-h3.sql url: jdbc:h3:mem:test username: root password: test
4、在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
5、编写实体类和Mapper类
//entity@Datapublic class User { private Long id; private String name; private Integer age; private String email;}//Mapperpublic interface UserMapper extends BaseMapper{}
6、service继承IService
public interface UserService extends IService
7、serviceImpl继承ServiceImpl
public class UserServiceImpl extends ServiceImplimplements UserService
4、mybatis-plus的sql操作(Service层)
1、Save:插入
// 插入一条记录(选择字段,策略插入)boolean save(T entity);// 插入(批量)boolean saveBatch(CollectionentityList);// 插入(批量)boolean saveBatch(Collection entityList, int batchSize);
2、SaveOrUpdate:修改插入
// TableId 注解存在更新记录,否插入一条记录boolean saveOrUpdate(T entity);// 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法boolean saveOrUpdate(T entity, WrapperupdateWrapper);// 批量修改插入boolean saveOrUpdateBatch(Collection entityList);// 批量修改插入boolean saveOrUpdateBatch(Collection entityList, int batchSize);
3、Remove:删除
// 根据 entity 条件,删除记录boolean remove(WrapperqueryWrapper);// 根据 ID 删除boolean removeById(Serializable id);// 根据 columnMap 条件,删除记录boolean removeByMap(Map columnMap);// 删除(根据ID 批量删除)boolean removeByIds(Collection extends Serializable> idList);
4、Update:更新
// 根据 UpdateWrapper 条件,更新记录 需要设置sqlsetboolean update(WrapperupdateWrapper);// 根据 whereWrapper 条件,更新记录boolean update(T updateEntity, Wrapper whereWrapper);// 根据 ID 选择修改boolean updateById(T entity);// 根据ID 批量更新boolean updateBatchById(Collection entityList);// 根据ID 批量更新boolean updateBatchById(Collection entityList, int batchSize);
5、Get:单体查询
// 根据 ID 查询T getById(Serializable id);// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")T getOne(WrapperqueryWrapper);// 根据 Wrapper,查询一条记录T getOne(Wrapper queryWrapper, boolean throwEx);// 根据 Wrapper,查询一条记录Map getMap(Wrapper queryWrapper);// 根据 Wrapper,查询一条记录 V getObj(Wrapper queryWrapper, Function super Object, V> mapper)
6、List:多条查询
// 查询所有Listlist();// 查询列表List list(Wrapper queryWrapper);// 查询(根据ID 批量查询)Collection listByIds(Collection extends Serializable> idList);// 查询(根据 columnMap 条件)Collection listByMap(Map columnMap);// 查询所有列表List
7、Page:分页查询(需要导入相关的配置或依赖)
// 无条件分页查询IPagepage(IPage page);// 条件分页查询IPage page(IPage page, Wrapper queryWrapper);// 无条件分页查询IPage > pageMaps(IPage page);// 条件分页查询IPage > pageMaps(IPage page, Wrapper queryWrapper);
8、Count:记录数据个数
// 查询总记录数int count();// 根据 Wrapper 条件,查询总记录数int count(WrapperqueryWrapper);
以上是"Java中mybatis-plus怎么用"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
查询
条件
更新
内容
篇文章
代码
功能
工程
效率
数据
文件
方法
注解
学习
帮助
开发
选择
配置
个数
前期
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库技术的sql
戴尔服务器纽扣电池如何取下
演讲稿网络安全高中生
苏州戴尔优秀r840服务器
c 多线程 数据库类
服务器可以设置资料拷贝权限吗
三维物体扫描软件开发的重要性
关于网络安全的条例
网络安全模式下怎么还原系统
网络技术员岗位做什么
手机软件开发前端的前景
中山市网络安全工作会议
学软件开发要有什么基础知识
热拉找不到服务器
迅雷代理服务器网址
启明星辰的网络安全设计方案
电脑端口进入服务器代码
表单提交是到数据库庅
山东弘扬网络技术有限公司
云服务器租赁的安全措施
数据库字段git管理
数据库中as的判断用法
天气预报无法联接到服务器
新点软件开发考核题目
qc归属地数据库
idea 离线安装数据库驱动
一个系统分别在两个服务器中部署
mysql数据库同步
网络安全监察是什么
工业网络安全实训室