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

怎么做金融营销网站汕头市城市建设开发总公司

怎么做金融营销网站,汕头市城市建设开发总公司,做网站是不是很麻烦,大庆医院网站建设文章目录 前言一、逻辑删除的工作原理二、支持的数据类型三、使用方法1.配置全局逻辑删除属性2.在实体类中使用 TableLogic 注解 四、常见问题解答1. 如何处理插入操作#xff1f;2. 删除接口自动填充功能失效怎么办#xff1f; 五、实战1. 全局配置2. 添加TableLogic3. 自动… 文章目录 前言一、逻辑删除的工作原理二、支持的数据类型三、使用方法1.配置全局逻辑删除属性2.在实体类中使用 TableLogic 注解 四、常见问题解答1. 如何处理插入操作2. 删除接口自动填充功能失效怎么办 五、实战1. 全局配置2. 添加TableLogic3. 自动填充4. 增删改查5. 结果 总结 前言 逻辑删除是一种优雅的数据管理策略它通过在数据库中标记记录为“已删除”而非物理删除来保留数据的历史痕迹同时确保查询结果的整洁性。MyBatis-Plus 提供了便捷的逻辑删除支持使得这一策略的实施变得简单高效。 一、逻辑删除的工作原理 MyBatis-Plus 的逻辑删除功能会在执行数据库操作时自动处理逻辑删除字段。以下是它的工作方式 插入逻辑删除字段的值不受限制。查找自动添加条件过滤掉标记为已删除的记录。更新防止更新已删除的记录。删除将删除操作转换为更新操作标记记录为已删除。 例如 删除update user set deleted1 where id 1 and deleted0查找select id,name,deleted from user where deleted0 二、支持的数据类型 逻辑删除字段支持所有数据类型但推荐使用 Integer、Boolean 或 LocalDateTime。如果使用 datetime 类型可以配置逻辑未删除值为 null已删除值可以使用函数如 now() 来获取当前时间。 三、使用方法 1.配置全局逻辑删除属性 在 application.yml 中配置 MyBatis-Plus 的全局逻辑删除属性 mybatis-plus:global-config:db-config:logic-delete-field: deleted # 全局逻辑删除字段名logic-delete-value: 1 # 逻辑已删除值logic-not-delete-value: 0 # 逻辑未删除值2.在实体类中使用 TableLogic 注解 在实体类中对应数据库表的逻辑删除字段上添加 TableLogic 注解 import com.baomidou.mybatisplus.annotation.TableLogic;public class User {// 其他字段...TableLogicprivate Integer deleted; }四、常见问题解答 注意事项 逻辑删除的本质逻辑删除的效果应等同于物理删除其目的是为了保留数据实现数据价值最大化。业务需求考量如果业务中仍需频繁查询这些“已删除”的数据应考虑是否真正需要逻辑删除。或许一个状态字段来控制数据的可见性更为合适。 1. 如何处理插入操作 方法一在数据库中为逻辑删除字段设置默认值。方法二在插入数据前手动设置逻辑删除字段的值。方法三使用 MyBatis-Plus 的自动填充功能。 2. 删除接口自动填充功能失效怎么办 方法一使用 deleteById 方法。方法二使用 update 方法并使用 UpdateWrapper.set(column, value)。方法三使用 update 方法并使用 UpdateWrapper.setSql(“columnvalue”)。方法四使用 Sql 注入器注入 com.baomidou.mybatisplus.extension.injector.methods.LogicDeleteByIdWithFill 并使用3.5.0版本已废弃推荐使用deleteById。 五、实战 1. 全局配置 mybatis-plus:global-config:db-config:logic-delete-field: deleted # 全局逻辑删除字段名logic-delete-value: 1 # 逻辑已删除值logic-not-delete-value: 0 # 逻辑未删除值2. 添加TableLogic package org.example.springboot3.mybatisplus.model;import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; import org.example.springboot3.mybatisplus.enums.GenderEnum; import org.example.springboot3.mybatisplus.enums.StatusEnum; import java.time.LocalDateTime;/*** Create by zjg on 2024/6/27*/ Getter Setter ToString TableName(user1) NoArgsConstructor public class User {TableId(type IdType.AUTO)private Long id;private String name;private Integer age;private String email;private GenderEnum gender;private StatusEnum status;TableField(fill FieldFill.INSERT)private LocalDateTime createTime;TableField(fill FieldFill.UPDATE)private LocalDateTime updateTime;TableLogicTableField(fill FieldFill.INSERT)private Integer deleted;public User(String name) {this.name name;}public User(Long id, String name) {this.id id;this.name name;}public User(String name, int age) {this.namename;this.ageage;}public User(long id, String name, int age) {this.idid;this.namename;this.ageage;} } 3. 自动填充 这里插入数据的时候,我们选择前面刚学习过的自动填充 package org.example.springboot3.mybatisplus.config;import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.reflection.MetaObject; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.time.LocalDateTime;/*** Create by zjg on 2024/7/2*/ Slf4j Component public class MyMetaObjectHandler implements MetaObjectHandler {Value(${mybatis-plus.global-config.db-config.logic-not-delete-value})private Integer deleted;Overridepublic void insertFill(MetaObject metaObject) {log.info(开始插入填充...);this.strictInsertFill(metaObject, createTime, LocalDateTime.class, LocalDateTime.now());this.strictInsertFill(metaObject, deleted, Integer.class, deleted);}Overridepublic void updateFill(MetaObject metaObject) {log.info(开始更新填充...);this.strictUpdateFill(metaObject, updateTime, LocalDateTime.class, LocalDateTime.now());} } 4. 增删改查 package org.example.springboot3.mybatisplus.controller;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import org.example.springboot3.mybatisplus.model.User; import org.example.springboot3.mybatisplus.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List;/*** Create by zjg on 2024/7/3*/ RestController RequestMapping(/table-logic/) public class TableLogicController {AutowiredUserService userService;RequestMapping(save)public void save() {// 假设有一个 User 实体对象User user new User();user.setName(John Doe);user.setEmail(john.doeexample.com);boolean result userService.save(user); // 调用 save 方法if (result) {System.out.println(User saved successfully.);} else {System.out.println(Failed to save user.);}}RequestMapping(list)public void list() {// 查询所有用户ListUser users userService.list(); // 调用 list 方法for (User user : users) {System.out.println(User: user);}}RequestMapping(update)public void update() {// 假设有一个 UpdateWrapper 对象设置更新条件为 name John Doe更新字段为 emailUpdateWrapperUser updateWrapper new UpdateWrapper();updateWrapper.eq(name, John Doe).set(email, john.doenewdomain.com);boolean result userService.update(updateWrapper); // 调用 update 方法if (result) {System.out.println(Record updated successfully.);} else {System.out.println(Failed to update record.);}}RequestMapping(remove)public void remove() {// 假设有一个 QueryWrapper 对象设置删除条件为 name John DoeQueryWrapperUser queryWrapper new QueryWrapper();queryWrapper.eq(name, John Doe);boolean result userService.remove(queryWrapper); // 调用 remove 方法if (result) {System.out.println(Record deleted successfully.);} else {System.out.println(Failed to delete record.);}} } 5. 结果 INSERT INTO user1 ( name, email, create_time, deleted ) VALUES ( ?, ?, ?, ? ) SELECT id,name,age,email,gender,status,create_time,update_time,deleted FROM user1 WHERE deleted0 UPDATE user1 SET email? WHERE deleted0 AND (name ?) UPDATE user1 SET deleted1 WHERE deleted0 AND (name ?)总结 回到顶部 通过以上步骤你可以轻松地在 MyBatis-Plus 中实现逻辑删除功能提高数据管理的灵活性和安全性。
http://www.tj-hxxt.cn/news/220719.html

相关文章:

  • 企业内部网站建设网站seo门户网价格是多少钱
  • 江西新农村建设权威网站郴州网站建设方案策划
  • 网站的建设公司网站后台管理入口
  • 做网站前端深圳物流公司联系电话
  • 杭州手机网站制作公司珠江新城越秀金融大厦
  • 佛山论坛建站模板增塑剂网站建设
  • 宁波seo推广优化公司天津seo建站
  • 做网站要付哪些钱建设项目网站备案申请表
  • wordpress网站维护教程东莞短视频seo制作
  • 建设好网站能赚到钱吗漯河企业网站建设
  • 丽水专业网站建设公司襄阳公司网站建设
  • 国外注册机网站wordpress 小工具添加图片
  • 宝安网站建设 名匠市场调研报告800字
  • 帮人做推广的网站如何搭建个人博客
  • 城乡建设招投标网站闸北区网站建设网页设计
  • 网站的系统建设方式有哪些方面教育机构做网站素材
  • 公众平台微信公众号官网wordpress仿seowhy基础指南模板
  • 佛山网站制作在线百度首页推广
  • 网站开发要学多久深圳十大建筑设计公司
  • 网站备案做优惠券怎么在网站上做排名
  • 能不能用自己的主机做网站如何自己做视频网站
  • 但是网站相关内容和程序并没有建设完_其次网站公司给我公司的深圳坪山网站建设
  • 网站设计建设公司怎么做易推客app拉新平台
  • 做导航网站把别人的网址链接过来要经过允许吗公关公司如何处理危机
  • 0797 网站制作高端网站建设服务商上海雍熙
  • 千图网素材免费西安网站优化
  • 免费注册一个网站事件营销成功案例
  • 加大整合力度网站集约建设怎么将微信同步到wordpress
  • php外贸网站制作怎么办?
  • 怎么设置网站关键词网站运营内容