内容简介:逻辑删除在项目中也是很常见的,比较常见的方式是添加一个逻辑比较简单,就是在Mapper接口和SqlMap文件中添加方法和对应的sql语句就可以了。将插件添加到项目中,并且运行完MybatisGenerator,我们来查看下生成的相关文件:
逻辑删除在项目中也是很常见的,比较常见的方式是添加一个 deleted
的标记位,当然也有其他的实现方式。本章节的插件针对的是添加标记位的方式来实现逻辑删除的。如果说没有和 deleted
标记位一起的唯一联合索引,那是很容易实现的,标志位只要两个值:0、1,然后通过 updateByExampleSelective
修改就可以了。但是往往我们表中会有唯一索引在,比如说名字不能重复之类的。这个时候我们采取的简单的处理方式是 set deleted = deleted + 1
来避免删除后不能再次添加同样数据的问题。索性这个插件就提供了两个方法: logicDelete
和 logicDeleteWithUIndex
。
修改Mapper接口和SqlMap文件
public class LogicDeletePluginextends PluginAdapter{ @Override public boolean validate(List<String> list){ return true; } /** * 修改Mapper类 */ @Override public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable){ addLogicDeleteMethod(interfaze); return true; } private void addLogicDeleteMethod(Interface interfaze){ // 方法的返回值 FullyQualifiedJavaType returnTypeInt = FullyQualifiedJavaType.getIntInstance(); Method logicDeleteMethod = new Method(); // 1.设置方法可见性 logicDeleteMethod.setVisibility(JavaVisibility.PUBLIC); // 2.设置返回值类型 int类型 logicDeleteMethod.setReturnType(returnTypeInt); // 3.设置方法名 logicDeleteMethod.setName("logicDelete"); // 4.设置参数列表 FullyQualifiedJavaType paramType = PrimitiveTypeWrapper.getLongInstance(); logicDeleteMethod.addParameter(new Parameter(paramType, "id")); interfaze.addMethod(logicDeleteMethod); Method logicDeleteWithUIndexMethod = new Method(); // 1.设置方法可见性 logicDeleteWithUIndexMethod.setVisibility(JavaVisibility.PUBLIC); // 2.设置返回值类型 int类型 logicDeleteWithUIndexMethod.setReturnType(returnTypeInt); // 3.设置方法名 logicDeleteWithUIndexMethod.setName("logicDeleteWithUIndex"); // 4.设置参数列表 FullyQualifiedJavaType paramTypeSelective = PrimitiveTypeWrapper.getLongInstance(); logicDeleteWithUIndexMethod.addParameter(new Parameter(paramTypeSelective, "id", "@Param(\"id\")")); logicDeleteWithUIndexMethod.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "uIndexes", "@Param(\"uIndexes\")", true)); interfaze.addMethod(logicDeleteWithUIndexMethod); } /** * 修改Mapper.xml */ @Override public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable){ addLogicDeleteXml(document, introspectedTable); addLogicDeleteWithUIndexXml(document, introspectedTable); return true; } private void addLogicDeleteXml(Document document, IntrospectedTable introspectedTable){ XmlElement logicDeleteElement = new XmlElement("update"); logicDeleteElement.addAttribute(new Attribute("id", "logicDelete")); logicDeleteElement.addAttribute(new Attribute("parameterType", "java.lang.Long")); logicDeleteElement.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())); logicDeleteElement.addElement(new TextElement("set deleted = 1, update_time = REPLACE(unix_timestamp(current_timestamp(3)),'.','') where id = #{id,jdbcType=BIGINT}")); document.getRootElement().addElement(logicDeleteElement); } private void addLogicDeleteWithUIndexXml(Document document, IntrospectedTable introspectedTable){ XmlElement logicDeleteWithUIndexElement = new XmlElement("update"); logicDeleteWithUIndexElement.addAttribute(new Attribute("id", "logicDeleteWithUIndex")); logicDeleteWithUIndexElement.addAttribute(new Attribute("parameterType", "map")); logicDeleteWithUIndexElement.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime() + " A,")); logicDeleteWithUIndexElement.addElement(new TextElement("(select C.deleted from " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime() + " C,")); logicDeleteWithUIndexElement.addElement(new TextElement("(select")); XmlElement foreachElement = new XmlElement("foreach"); foreachElement.addAttribute(new Attribute("collection", "uIndexes")); foreachElement.addAttribute(new Attribute("index", "index")); foreachElement.addAttribute(new Attribute("item", "column")); foreachElement.addAttribute(new Attribute("separator", ",")); foreachElement.addElement(new TextElement("${column}")); logicDeleteWithUIndexElement.addElement(foreachElement); logicDeleteWithUIndexElement.addElement(new TextElement("from " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime() + " where id = #{id,jdbcType=BIGINT}) D")); XmlElement whereElement = new XmlElement("where"); XmlElement whereForachElement = new XmlElement("foreach"); whereForachElement.addAttribute(new Attribute("collection", "uIndexes")); whereForachElement.addAttribute(new Attribute("index", "index")); whereForachElement.addAttribute(new Attribute("item", "column")); whereForachElement.addElement(new TextElement(" and C.${column} = D.${column}")); whereElement.addElement(whereForachElement); logicDeleteWithUIndexElement.addElement(whereElement); logicDeleteWithUIndexElement.addElement(new TextElement("order by C.deleted desc limit 1) B")); logicDeleteWithUIndexElement.addElement(new TextElement("set A.deleted = B.deleted + 1, A.update_time = REPLACE(unix_timestamp(current_timestamp(3)),'.','')")); logicDeleteWithUIndexElement.addElement(new TextElement("where A.id = #{id,jdbcType=BIGINT}")); logicDeleteWithUIndexElement.addElement(new TextElement("and A.deleted = 0")); document.getRootElement().addElement(logicDeleteWithUIndexElement); } }
逻辑比较简单,就是在Mapper接口和SqlMap文件中添加方法和对应的 sql 语句就可以了。
查看生成文件
将插件添加到项目中,并且运行完MybatisGenerator,我们来查看下生成的相关文件:
public interface AdministratorMapper{ ... int logicDelete(Long id); int logicDeleteWithUIndex(@Param("id")Long id, @Param("uIndexes")String ... uIndexes); }
<updateid="logicDelete"parameterType="java.lang.Long"> update administrator set deleted = 1, update_time = REPLACE(unix_timestamp(current_timestamp(3)),'.','') where id = #{id,jdbcType=BIGINT} </update> <updateid="logicDeleteWithUIndex"parameterType="map"> update administrator A, (select C.deleted from administrator C, (select <foreachcollection="uIndexes"index="index"item="column"separator=","> ${column} </foreach> from administrator where id = #{id,jdbcType=BIGINT}) D <where> <foreachcollection="uIndexes"index="index"item="column"> and C.${column} = D.${column} </foreach> </where> order by C.deleted desc limit 1) B set A.deleted = B.deleted + 1, A.update_time = REPLACE(unix_timestamp(current_timestamp(3)),'.','') where A.id = #{id,jdbcType=BIGINT} and A.deleted = 0 </update>
如何使用
使用方式如下:
这里同样用到了之前添加到Model类中的表字段枚举。
administratorMapper.logicDeleteWithUIndex(id, Administrator.COLUMNS.MOBILE.getColumn()
本章节就到这里了。以后要用插件来实现一些特殊的Sql,应该可以举一反三了。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- xrkmonitor 字符云监控系统 v2.7 插件一键部署逻辑改进
- centos创建逻辑卷和扩容逻辑卷
- AI「王道」逻辑编程的复兴?清华提出神经逻辑机,已入选ICLR
- 内聚代码提高逻辑可读性,用MCVP接续你的大逻辑
- 逻辑式编程语言极简实现(使用C#) - 1. 逻辑式编程语言介绍
- 什么是逻辑数据字典?
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
啊哈C语言!逻辑的挑战(修订版)
啊哈磊 / 电子工业出版社 / 2017-1 / 49
《啊哈C语言!逻辑的挑战(修订版)》是一本非常有趣的编程启蒙书,《啊哈C语言!逻辑的挑战(修订版)》从中小学生的角度来讲述,没有生涩的内容,取而代之的是生动活泼的漫画和风趣幽默的文字。配合超萌的编程软件,《啊哈C语言!逻辑的挑战(修订版)》从开始学习与计算机对话到自己独立制作一个游戏,由浅入深地讲述编程的思维。同时,与计算机展开的逻辑较量一定会让你觉得很有意思。你可以在茶余饭后阅读《啊哈C语言!逻......一起来看看 《啊哈C语言!逻辑的挑战(修订版)》 这本书的介绍吧!