Mybatis-generator-maven-plugin plug-in extensions add a custom method

Source: Internet
Author: User

After using Mybatis-generator-maven-plugin for code generation, it may be found that some common methods are not generated, such as delete operations, the data in the database is meaningful, delete as far as possible is the logical delete operation, If you use the code generator's Delete method, then the data is physically deleted, this method is not what we need, then we need to add a method Deletelogicbyids in the code generation process, so that all the code generation process, if you can automatically generate this method, then, The amount of code we have is pretty low.


After we created the table structure, the process of generating the code through the MVN command automatically generated one of our custom methods, what a wonderful thing.


First Git:http://git.oschina.net/alexgaoyh/mutimodule-parent.

Which, in the Generatorconfig.xml file, adds

<plugin type= "com. MutiModule.common.mybatis.plugin.PaginationPlugin "></plugin><plugin type=" com. MutiModule.common.mybatis.plugin.DeleteLogicByIdsPlugin "></plugin>



Package com. Mutimodule.common.mybatis.plugin;import Java.util.list;import Org.mybatis.generator.api.commentgenerator;import Org.mybatis.generator.api.introspectedtable;import Org.mybatis.generator.api.pluginadapter;import Org.mybatis.generator.api.dom.java.field;import Org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; Import Org.mybatis.generator.api.dom.java.javavisibility;import Org.mybatis.generator.api.dom.java.method;import Org.mybatis.generator.api.dom.java.parameter;import Org.mybatis.generator.api.dom.java.toplevelclass;import Org.mybatis.generator.api.dom.xml.attribute;import Org.mybatis.generator.api.dom.xml.textelement;import org.mybatis.generator.api.dom.xml.xmlelement;/** * Mybais MySQL paging related, extended Mybatis-generator-maven-plugin plugin feature, raw content page related * @ Author Alexgaoyh * */public class Paginationplugin extends Pluginadapter {@Overridepublic Boolean modelexampleclassgenerated (Toplevelclass toplevelclass,introspectedtable introspectedtable) {//Add field, getter, setter//for LImit clauseaddpage (Toplevelclass, introspectedtable, "page"); return super.modelexampleclassgenerated (TopLevelClass , introspectedtable);} @Overridepublic boolean sqlmapselectbyexamplewithoutblobselementgenerated (XmlElement element, introspectedtable introspectedtable) {XmlElement page = new XmlElement ("if");p Age.addattribute (New Attribute ("Test", "page! = null")); Page.addelement (New TextElement ("Limit #{page.begin}, #{page.length}"); element.addelement (page); return Super.sqlmapupdatebyexamplewithoutblobselementgenerated (element,introspectedtable);} /** * @param toplevelclass * @param introspectedtable * @param name */private void AddPage (Toplevelclass toplevelclass,int Rospectedtable introspectedtable, String name) {Toplevelclass.addimportedtype (new Fullyqualifiedjavatype ("com. MutiModule.common.vo.mybatis.pagination.Page ")); Commentgenerator commentgenerator = Context.getcommentgenerator (); Field field = new Field (); field.setvisibility (javavisibility.protected); Field.settype (New FullyqualifiEdjavatype ("com. MutiModule.common.vo.mybatis.pagination.Page ")); Field.setname (name); Commentgenerator.addfieldcomment (field, introspectedtable); Toplevelclass.addfield (field); char c = name.charat (0); String camel = Character.touppercase (c) + name.substring (1); Method = new Method (); method.setvisibility (javavisibility.public); Method.setname ("set" + Camel); Method.addparameter (New Parameter ("New Fullyqualifiedjavatype" ("com. MutiModule.common.vo.mybatis.pagination.Page "), name), Method.addbodyline (" this. "+ name +" = "+ name +"; "); Commentgenerator.addgeneralmethodcomment (method, introspectedtable); Toplevelclass.addmethod (method); method = new Method (); method.setvisibility (javavisibility.public); Method.setreturntype (new Fullyqualifiedjavatype ("com. MutiModule.common.vo.mybatis.pagination.Page ")); Method.setname (" Get "+ camel); Method.addbodyline (" return "+ Name +" ;"); Commentgenerator.addgeneralmethodcomment (method, introspectedtable); Toplevelclass.addmethod (method);} /** * This plugin is ALWAys valid-no Properties is required */public Boolean validate (list<string> warnings) {return true;}} 

Package com. Mutimodule.common.mybatis.plugin;import Java.util.list;import Org.mybatis.generator.api.introspectedtable;import Org.mybatis.generator.api.pluginadapter;import Org.mybatis.generator.api.dom.java.fullyqualifiedjavatype;import Org.mybatis.generator.api.dom.java.interface;import Org.mybatis.generator.api.dom.java.method;import Org.mybatis.generator.api.dom.java.parameter;import Org.mybatis.generator.api.dom.java.toplevelclass;import Org.mybatis.generator.api.dom.xml.attribute;import Org.mybatis.generator.api.dom.xml.document;import Org.mybatis.generator.api.dom.xml.textelement;import Org.mybatis.generator.api.dom.xml.xmlelement;public Class Deletelogicbyidsplugin extends Pluginadapter {/** * {@inheritDoc} */public Boolean validate (list<string> warnings ) {return true;} /** * {@inheritDoc} */@Overridepublic boolean clientselectbyexamplewithblobsmethodgenerated (Method method,interface Interfaze, introspectedtable introspectedtable) {Interfaze.addmethod (generatedeletelogicbyiDS (Method,introspectedtable)); return true;} /** * {@inheritDoc} */@Overridepublic Boolean clientselectbyexamplewithoutblobsmethodgenerated (method, Interface interfaze,introspectedtable introspectedtable) {Interfaze.addmethod (Generatedeletelogicbyids (method, introspectedtable)); return true;} /** * {@inheritDoc} */@Overridepublic Boolean clientselectbyexamplewithblobsmethodgenerated (method, Toplevelclass Toplevelclass, introspectedtable introspectedtable) {Toplevelclass.addmethod ( Generatedeletelogicbyids (method,introspectedtable)); return true;} /** * {@inheritDoc} */@Overridepublic Boolean clientselectbyexamplewithoutblobsmethodgenerated (method, Toplevelclass toplevelclass,introspectedtable introspectedtable) {Toplevelclass.addmethod ( Generatedeletelogicbyids (method,introspectedtable)); return true;} @Overridepublic Boolean sqlmapdocumentgenerated (document document, Introspectedtable introspectedtable) {String TableName = Introspectedtable.getaliasedfullyqualifiedtablenameatRuntime ();//database table name XmlElement parentelement = Document.getrootelement ();//Generate pagination statement first half XmlElement Deletelogicbyidselement = new XmlElement ("Update");d Eletelogicbyidselement.addattribute (new Attribute ("id", " Deletelogicbyids "));d eletelogicbyidselement.addelement (New TextElement (" Update "+ TableName +" Set Deleteflag = #{ Deleteflag,jdbctype=integer} where ID in "+" <foreach item=\ "item\" index=\ "index\" collection=\ "Ids\" open=\ "(\" Sep Arator=\ ", \" close=\ ") \" >#{item}</foreach> ");p arentelement.addelement (deletelogicbyidselement); return Super.sqlmapdocumentgenerated (document, introspectedtable);} Private method Generatedeletelogicbyids (method, Introspectedtable introspectedtable) {Method M = new Method (" Deletelogicbyids "); M.setvisibility (Method.getvisibility ()); M.setreturntype ( Fullyqualifiedjavatype.getintinstance ()); M.addparameter (New Parameter (Fullyqualifiedjavatype.getintinstance (), " Deleteflag "," @Param (\ "deleteflag\")); M.addparameter (new Parameter (New fullyqualIfiedjavatype ("integer[]"), "IDs", "@Param (\" Ids\ ")"); Context.getcommentgenerator (). Addgeneralmethodcomment (M, introspectedtable); return m;}}






Mybatis-generator-maven-plugin plug-in extensions add a custom method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.