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

discuz轉wordpress石家庄seo扣费

discuz轉wordpress,石家庄seo扣费,合肥网站建设网站推广津学院,智慧团建客服MyBatis-Plus 是基于 MyBatis 的增强工具,为简化 MyBatis 的开发提供了诸多功能扩展。它的目标是减少重复代码、提高开发效率,提供了 CRUD(Create, Read, Update, Delete)操作的简化方法以及多种实用插件。以下是 MyBatis-Plus 的…

MyBatis-Plus 是基于 MyBatis 的增强工具,为简化 MyBatis 的开发提供了诸多功能扩展。它的目标是减少重复代码、提高开发效率,提供了 CRUD(Create, 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对象进行分页查询
Page<User> page = new Page<>(1, 10);  // 第1页,每页10条数据
IPage<User> 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.ds6.com.cn/news/116531.html

相关文章:

  • 婚庆网站模板免费下载品牌推广方案范文
  • jsp购物网站开发环境滴滴友链
  • 云南网站建设优选平台南宁网站seo排名优化
  • 河东天津网站建设网站推广该怎么做
  • 西安做网站的公司有seo自学网官网
  • 做告状网站百度竞价投放
  • 重庆秀山网站建设报价网络营销策略分析
  • 网站要怎么做的web网站模板
  • 深圳专业极速网站建设公司域名注册查询
  • wordpress怎么用模版山西seo推广
  • 哈尔滨餐饮网站建设网站推广是做什么的
  • 成都专业网站建设aso优化重要吗
  • 网站制作方案怎么写百度第三季度财报2022
  • 网站如何做更新小红书推广渠道
  • 网站怎么申请微博登录沧州网络推广公司
  • 建设一个视频网站的成本世界搜索引擎大全
  • 校园网网站建设规划书网站统计分析工具的主要功能
  • 北京网络网站建设价格幽默软文经典案例300
  • b站播放量自助下单百度热议排名软件
  • 商丘做网站的电话企业为何选择网站推广外包?
  • 网站的客服一般怎么做百度网址大全
  • wordpress 全站ajaxqq空间秒赞秒评网站推广
  • 湖北公司网站建设多少钱互联网营销师培训费用是多少
  • 合肥网站设计制作怎么让某个关键词排名上去
  • 阿里巴巴做网站找谁最新战争新闻事件今天
  • 网站建设质量保证清远新闻最新
  • 怎样网站seo谷歌排名查询
  • 高校网站建设自查报告舆情信息报送
  • 环球网广东疫情最新消息免费seo公司
  • 为什么要给企业建设网站外贸互联网推广的