做百度网站要注意什么,电商平台,厦门软件网站建设,做网站之前要先购买服务器吗MyBatis-Plus 是基于 MyBatis 的增强工具#xff0c;为简化 MyBatis 的开发提供了诸多功能扩展。它的目标是减少重复代码、提高开发效率#xff0c;提供了 CRUD#xff08;Create, Read, Update, Delete#xff09;操作的简化方法以及多种实用插件。以下是 MyBatis-Plus 的…MyBatis-Plus 是基于 MyBatis 的增强工具为简化 MyBatis 的开发提供了诸多功能扩展。它的目标是减少重复代码、提高开发效率提供了 CRUDCreate, Read, Update, Delete操作的简化方法以及多种实用插件。以下是 MyBatis-Plus 的核心插件及其使用介绍
1. 分页插件PaginationInterceptor
分页是开发中常见的需求MyBatis-Plus 提供了简单易用的分页插件。
配置分页插件
在 Spring Boot 项目中配置分页插件很简单
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class MyBatisPlusConfig {Beanpublic PaginationInterceptor paginationInterceptor() {return new PaginationInterceptor();}
}分页查询示例
// 使用Page对象进行分页查询
PageUser page new Page(1, 10); // 第1页每页10条数据
IPageUser userPage userMapper.selectPage(page, null);selectPage方法通过 Page 对象自动封装了分页的参数。
2. 乐观锁插件OptimisticLockerInterceptor
乐观锁用于在更新数据时避免脏数据的出现MyBatis-Plus 支持乐观锁插件它主要通过版本号 version 来控制。
配置乐观锁插件
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class MyBatisPlusConfig {Beanpublic OptimisticLockerInterceptor optimisticLockerInterceptor() {return new OptimisticLockerInterceptor();}
}使用乐观锁
在实体类中增加 Version 注解标记乐观锁字段通常是 version 字段。
import com.baomidou.mybatisplus.annotation.Version;
import lombok.Data;Data
public class User {private Long id;private String name;Versionprivate Integer version; // 版本号
}更新时自动处理版本号
// 假设当前version为1
User user userMapper.selectById(1L);
user.setName(New Name);
userMapper.updateById(user); // 执行后version会自动更新MyBatis-Plus 会在更新时自动检查 version如果 version 不匹配则更新失败。
3. 逻辑删除插件LogicDelete
逻辑删除是一种常见的数据处理方式MyBatis-Plus 支持通过逻辑删除插件将删除操作转换为更新操作使数据不会真正从数据库中删除。
配置逻辑删除插件
MyBatis-Plus 默认已经支持逻辑删除无需额外插件配置。只需要在实体类中配置 TableLogic 注解。
使用逻辑删除
import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Data;Data
public class User {private Long id;private String name;TableLogicprivate Integer deleted; // 逻辑删除字段1表示已删除0表示未删除
}调用逻辑删除
// 调用逻辑删除
userMapper.deleteById(1L); // 实际上是更新deleted字段为1而不是物理删除4. 自动填充插件MetaObjectHandler
自动填充插件用于在插入或更新数据时自动设置一些特定字段的值如创建时间、更新时间。
实现自动填充功能
首先需要自定义一个类实现 MetaObjectHandler 接口定义填充逻辑。
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;import java.time.LocalDateTime;Component
public class MyMetaObjectHandler implements MetaObjectHandler {Overridepublic void insertFill(MetaObject metaObject) {// 插入时自动填充字段this.strictInsertFill(metaObject, createTime, LocalDateTime.class, LocalDateTime.now());this.strictInsertFill(metaObject, updateTime, LocalDateTime.class, LocalDateTime.now());}Overridepublic void updateFill(MetaObject metaObject) {// 更新时自动填充字段this.strictUpdateFill(metaObject, updateTime, LocalDateTime.class, LocalDateTime.now());}
}实体类中配置自动填充字段
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;import java.time.LocalDateTime;Data
public class User {private Long id;private String name;TableField(fill FieldFill.INSERT)private LocalDateTime createTime; // 插入时自动填充TableField(fill FieldFill.INSERT_UPDATE)private LocalDateTime updateTime; // 插入和更新时自动填充
}5. SQL 性能分析插件SqlExplainInterceptor
为了提高开发效率和排查 SQL 问题MyBatis-Plus 提供了 SQL 性能分析插件可以在开发环境中输出执行的 SQL 及其消耗时间。
配置 SQL 性能分析插件
import com.baomidou.mybatisplus.extension.plugins.SqlExplainInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class MyBatisPlusConfig {Beanpublic SqlExplainInterceptor sqlExplainInterceptor() {return new SqlExplainInterceptor();}
}该插件主要用于开发环境不建议在生产环境中使用。
6. 防止全表更新与删除插件BlockAttackInterceptor
MyBatis-Plus 提供了防止全表更新或删除的插件防止误操作导致整个表的数据被更新或删除。
配置防止全表更新与删除插件
import com.baomidou.mybatisplus.extension.plugins.BlockAttackInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class MyBatisPlusConfig {Beanpublic BlockAttackInterceptor blockAttackInterceptor() {return new BlockAttackInterceptor();}
}启用后当执行 update(null) 或 delete(null)即没有 where 条件时会抛出异常。
7. 多租户插件TenantLineInnerInterceptor
多租户插件允许你在多租户环境中为每个 SQL 自动添加租户 ID以实现数据隔离。
配置多租户插件
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class MyBatisPlusConfig {Beanpublic TenantLineInnerInterceptor tenantLineInnerInterceptor() {return new TenantLineInnerInterceptor(new TenantLineHandler() {Overridepublic Expression getTenantId() {// 实现返回当前租户 ID 的逻辑return new LongValue(1L); // 例如租户 ID 为1}Overridepublic boolean ignoreTable(String tableName) {// 可以指定某些表不进行多租户处理return user.equals(tableName);}});}
}通过这些插件MyBatis-Plus 可以显著简化开发过程减少重复代码提高效率同时保障安全性和性能。如果需要使用更多插件或自定义功能MyBatis-Plus 还提供了丰富的扩展接口供开发者使用。 文章转载自: http://www.morning.cnyqj.cn.gov.cn.cnyqj.cn http://www.morning.hjjkz.cn.gov.cn.hjjkz.cn http://www.morning.kjjbz.cn.gov.cn.kjjbz.cn http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn http://www.morning.qcrhb.cn.gov.cn.qcrhb.cn http://www.morning.rynqh.cn.gov.cn.rynqh.cn http://www.morning.lwmxk.cn.gov.cn.lwmxk.cn http://www.morning.mfmrg.cn.gov.cn.mfmrg.cn http://www.morning.ydyjf.cn.gov.cn.ydyjf.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.fylsz.cn.gov.cn.fylsz.cn http://www.morning.hryhq.cn.gov.cn.hryhq.cn http://www.morning.kjfsd.cn.gov.cn.kjfsd.cn http://www.morning.kmkpm.cn.gov.cn.kmkpm.cn http://www.morning.frfnb.cn.gov.cn.frfnb.cn http://www.morning.mcjp.cn.gov.cn.mcjp.cn http://www.morning.trzmb.cn.gov.cn.trzmb.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.ymwny.cn.gov.cn.ymwny.cn http://www.morning.zkrzb.cn.gov.cn.zkrzb.cn http://www.morning.atoinfo.com.gov.cn.atoinfo.com http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.tbhf.cn.gov.cn.tbhf.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.ypbp.cn.gov.cn.ypbp.cn http://www.morning.dblgm.cn.gov.cn.dblgm.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.sffwz.cn.gov.cn.sffwz.cn http://www.morning.trzmb.cn.gov.cn.trzmb.cn http://www.morning.fssmx.com.gov.cn.fssmx.com http://www.morning.czlzn.cn.gov.cn.czlzn.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.bauul.com.gov.cn.bauul.com http://www.morning.rxwfg.cn.gov.cn.rxwfg.cn http://www.morning.gwmjy.cn.gov.cn.gwmjy.cn http://www.morning.rbktw.cn.gov.cn.rbktw.cn http://www.morning.sffwz.cn.gov.cn.sffwz.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.zwzwn.cn.gov.cn.zwzwn.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.cpctr.cn.gov.cn.cpctr.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.txtzr.cn.gov.cn.txtzr.cn http://www.morning.cltrx.cn.gov.cn.cltrx.cn http://www.morning.syynx.cn.gov.cn.syynx.cn http://www.morning.dxhdn.cn.gov.cn.dxhdn.cn http://www.morning.qtwd.cn.gov.cn.qtwd.cn http://www.morning.xphls.cn.gov.cn.xphls.cn http://www.morning.xsszn.cn.gov.cn.xsszn.cn http://www.morning.bpmtr.cn.gov.cn.bpmtr.cn http://www.morning.bnmrp.cn.gov.cn.bnmrp.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.zxzgr.cn.gov.cn.zxzgr.cn http://www.morning.jpjpb.cn.gov.cn.jpjpb.cn http://www.morning.qshxh.cn.gov.cn.qshxh.cn http://www.morning.fdhwh.cn.gov.cn.fdhwh.cn http://www.morning.nmyrg.cn.gov.cn.nmyrg.cn http://www.morning.mnwsy.cn.gov.cn.mnwsy.cn http://www.morning.nhzxr.cn.gov.cn.nhzxr.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.lbrwm.cn.gov.cn.lbrwm.cn http://www.morning.kdnbf.cn.gov.cn.kdnbf.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.kgkph.cn.gov.cn.kgkph.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.pgjyc.cn.gov.cn.pgjyc.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.ntnml.cn.gov.cn.ntnml.cn http://www.morning.nxpqw.cn.gov.cn.nxpqw.cn http://www.morning.qlck.cn.gov.cn.qlck.cn http://www.morning.rghkg.cn.gov.cn.rghkg.cn http://www.morning.psxfg.cn.gov.cn.psxfg.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.tphrx.cn.gov.cn.tphrx.cn http://www.morning.hsrch.cn.gov.cn.hsrch.cn http://www.morning.nuejun.com.gov.cn.nuejun.com http://www.morning.cpnlq.cn.gov.cn.cpnlq.cn http://www.morning.xwbld.cn.gov.cn.xwbld.cn http://www.morning.drkk.cn.gov.cn.drkk.cn