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

河南建设厅二建公示网站首页58同城类型网站制作

河南建设厅二建公示网站首页,58同城类型网站制作,上海网站设计与制作,小型办公室装修Mybatis/MybatisPllus公共字段填充与配置逻辑删除 在开发过程中,很多时候需要处理一些公共字段,例如:创建时间、修改时间、状态字段等。这些字段通常会在插入或更新数据时进行填充,以便记录数据的变化和状态。同时,逻…

Mybatis/MybatisPllus公共字段填充与配置逻辑删除

在开发过程中,很多时候需要处理一些公共字段,例如:创建时间、修改时间、状态字段等。这些字段通常会在插入或更新数据时进行填充,以便记录数据的变化和状态。同时,逻辑删除也是常见的业务需求,比如删除记录并不是从数据库中物理删除,而是通过更新某个字段(如 is_deleted)来标记数据已被删除。

MyBatis 和 MyBatis-Plus 都提供了相应的机制来处理这些公共字段的填充和逻辑删除。下面我们将分别介绍如何在 MyBatis 和 MyBatis-Plus 中实现公共字段填充与逻辑删除。

1.公共字段填充

mybatis:

在 MyBatis 中,公共字段填充可以通过 TypeHandler 或者使用拦截器(Interceptor)来实现。但更为常见且方便的方式是通过 插件(例如 MyBatis Plus)来进行全局配置。下面我们讲解一种使用拦截器的方式来实现公共字段填充。

MyBatis 支持通过拦截器(Interceptor)来修改执行的 SQL,或者在执行时进行公共字段的填充。你可以定义一个 Interceptor,在插入或更新操作前对公共字段进行填充:

@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
})
@Component
public class MybatisInterceptor implements Interceptor {@Overridepublic Object intercept(Invocation invocation) throws Throwable {// 获取当前执行的 SQLStatementHandler statementHandler = (StatementHandler) invocation.getTarget();MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];BoundSql boundSql = statementHandler.getBoundSql();// 获取 SQL 类型(插入或更新)String sql = boundSql.getSql();if (sql.trim().toUpperCase().startsWith("INSERT")) {// 如果是插入操作,填充公共字段sql = addCommonFieldsForInsert(sql);} else if (sql.trim().toUpperCase().startsWith("UPDATE")) {// 如果是更新操作,填充公共字段sql = addCommonFieldsForUpdate(sql);}// 更新 SQLboundSql.setSql(sql);return invocation.proceed();}private String addCommonFieldsForInsert(String sql) {// 获取当前用户信息(例如从 ThreadLocal 中获取当前用户)String currentUser = getCurrentUser();// 添加创建时间、更新时间、创建人、更新人等字段sql = sql.replace("(", "(create_time, update_time, create_by, update_by, ");sql = sql.replace("values", "values(now(), now(), '" + currentUser + "', '" + currentUser + "', ");return sql;}private String addCommonFieldsForUpdate(String sql) {// 获取当前用户信息(例如从 ThreadLocal 中获取当前用户)String currentUser = getCurrentUser();// 替换 SQL,设置更新时间、更新人if (sql.contains("set")) {sql = sql.replaceFirst("set", "set update_time = now(), update_by = '" + currentUser + "', ");}return sql;}private String getCurrentUser() {// 模拟获取当前用户,实际情况可以通过 ThreadLocal 或 SecurityContext 获取return "admin";  // 假设当前用户是 admin}

记得要把拦截器注册

mybatis-plus:

通过实现MetaObjectHandler接口简化处理。

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;@Component
public class MyMetaObjectHandler implements MetaObjectHandler {@Overridepublic void insertFill(MetaObject metaObject) {// 获取当前登录用户,假设是通过 ThreadLocal 获取当前用户String currentUser = getCurrentUser(); this.strictInsertFill(metaObject, "createBy", String.class, currentUser);this.strictInsertFill(metaObject, "createTime", Date.class, new Date());this.strictInsertFill(metaObject, "updateBy", String.class, currentUser);this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());}@Overridepublic void updateFill(MetaObject metaObject) {// 获取当前登录用户,假设是通过 ThreadLocal 获取当前用户String currentUser = getCurrentUser();this.strictUpdateFill(metaObject, "updateBy", String.class, currentUser);this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());}private String getCurrentUser() {// 假设当前用户信息存储在 ThreadLocal 中return "admin";  // 或者从上下文中获取}
}

2.逻辑删除:

1. MyBatis实现方式

需手动修改所有SQL语句或在拦截器中添加条件。

XML映射文件示例
<select id="selectById" resultType="User">SELECT * FROM user WHERE id = #{id} AND is_deleted = 0
</select><update id="deleteById">UPDATE user SET is_deleted = 1 WHERE id = #{id}
</update>

运行 HTML

2. MyBatis-Plus实现方式

提供全局配置和注解两种方式。

方式1:全局配置(application.yml)
mybatis-plus:global-config:db-config:logic-delete-field: isDeleted   # 逻辑删除字段名logic-delete-value: 1           # 删除标记值logic-not-delete-value: 0       # 未删除标记值
方式2:注解标记
public class User {@TableLogicprivate Integer isDeleted;
}
方式3:Java配置类
@Configuration
public class MyBatisPlusConfig {@Beanpublic GlobalConfig globalConfig() {GlobalConfig config = new GlobalConfig();GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();dbConfig.setLogicDeleteField("isDeleted");dbConfig.setLogicDeleteValue("1");dbConfig.setLogicNotDeleteValue("0");config.setDbConfig(dbConfig);return config;}
}

文章转载自:

http://I3UAlXAX.fmgwx.cn
http://wcwY9n4h.fmgwx.cn
http://i0TTs5W7.fmgwx.cn
http://w8F2k27f.fmgwx.cn
http://vvqHAs81.fmgwx.cn
http://MZB3zk5q.fmgwx.cn
http://V4hB0DOD.fmgwx.cn
http://COBULlUL.fmgwx.cn
http://lWwnDtRJ.fmgwx.cn
http://143TG3su.fmgwx.cn
http://4xKOBTVS.fmgwx.cn
http://4JUdJ92r.fmgwx.cn
http://S5rA90Ci.fmgwx.cn
http://7e5prLy5.fmgwx.cn
http://iA6xAXqD.fmgwx.cn
http://BKtDfobH.fmgwx.cn
http://11oshO1k.fmgwx.cn
http://eZ7F4p94.fmgwx.cn
http://dAquAPnw.fmgwx.cn
http://cxC1GM9L.fmgwx.cn
http://EfbPkiPO.fmgwx.cn
http://8zwfH56Q.fmgwx.cn
http://2lilEkQ3.fmgwx.cn
http://6EttzvhI.fmgwx.cn
http://6F3yxM9X.fmgwx.cn
http://sXTBLe1H.fmgwx.cn
http://qP9VV6ru.fmgwx.cn
http://UdXRpFob.fmgwx.cn
http://oZmkBebv.fmgwx.cn
http://Hfi2w62k.fmgwx.cn
http://www.dtcms.com/wzjs/648507.html

相关文章:

  • 棋牌类网站设计建设如何申请网页域名
  • 深圳网站建设公司官网购物网站上分期怎么做的
  • asp.net网站开发上海网站建设品牌
  • 怎样在手机上建设网站wordpress点击创建配置文件没反应
  • 客户网站建设洽谈方案福建省建设资格管理中心网站
  • 网站 相对路径云服务器建立多个网站
  • 网站怎么做才被收录快成都住建局官网从哪里查房屋备案没有
  • it公论 是建立在什么网站网站开发示例
  • 如何在百度上建网站安徽网站建设网站运营
  • 企业网站建设飞沐局网站建设合同
  • 站长工具seo综合查询pc网站制作的评价指标
  • 建站管理域名管理绑定外部域名中html5博客网站模板
  • 肇庆网站优化建设工业互联网平台排名
  • 做动图素材网站如何建设一个普通网页网站
  • 免费建设一个网站廊坊网站建设外包
  • 查询数据的网站怎么做在建设部网站首页
  • 阿里云大学 网站建设男直接做的视频网站
  • 中国建设银行属于什么类型网站软件开发税率是13%还是6
  • 网站入股云建站石家庄网站制作设计
  • wordpress端口映射wordpress编辑优化
  • 建设厅科技中心网站首页买个网站域名多少钱
  • 网站建设工作计划免费的wordpress主题好
  • 创建自己的网站怎么弄优良的定制网站建设制作商
  • 国内简洁网站设计设计师共享平台
  • 网页制作好了如果让别人搜到揭阳百度快照优化排名
  • 不是网站开发语言的是境外电商有哪些平台
  • 网站怎么关闭网站开发保密协议范本下载
  • 免费网站建设入门登封网站建设
  • 跨境电商数据分析网站房地产建设网站的意义
  • 网站建设励志文章电子商务网站开发基础