IntelliJ Idea 2016 Learning Series (iii) revise Mybatis-generator source code to generate Chinese annotation __idea

Source: Internet
Author: User
Tags generator log4j

If you enter this article, you also want to MyBatis generator automatically generate mapper XML model, automatically generate the database field notes for comment information. So as long as you finish reading this article, you can help you achieve what you want. The end of the article will have the source address and jar.

Download Mybatis-generator-core source Code Modification modify generate comments on the Model object class add the Remark field in the fullyqualifiedtable to save the memo information for the database table Modify the Databaseintrospector Calculateintrospectedtables method to add a section of code to get database comments Dg2commentgenerator Addclasscommentinnerclass innerclass introspectedtable Introspectedtable method to modify a comment on a field in the Model object Add the Remarks property to the Introspectedcolumn to save the memo information for a field in the database's corresponding table Settergetter modify the Addfieldcommentfield in Dg2commentgenerator Field introspectedtable introspectedtable Introspectedcolumn Introspectedcolumn method modifies the annotation getter method of Settergetter in model object Setter method Modify comments in Build mapper modify annotations in Mapper interface remove annotations in Mapperxml How to use the package in engineering to refer to the need to modify generatorconfigxml need to modify Pomxml source code and jar download

Download Mybatis-generator-core Source

The download process, you can Google, I have already tested the MAVEN project, you can download directly down, import your own IDE can be.
Maven Mybatis-generator-core Source code

Format after the import was successful:


If you are directly downloading the official source code, to modify a lot of things in the Pom.xml, you are interested in playing. I'm done here.
Pom.xml

<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 http:// Maven.apache.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <parent> <group Id>org.mybatis.generator</groupid> <artifactId>mybatis-generator</artifactId> <version& gt;1.3.2</version> </parent> <groupId>org.mybatis.generator</groupId> <artifactId> Mybatis-generator-core</artifactid> <!--1.3.2-fix was named after me, and also the name of the last Jar--> <version>1.3.2-fix</ version> <packaging>jar</packaging> <properties> &LT;PROJECT.BUILD.SOURCEENCODING&GT;UTF-8&L T;/project.build.sourceencoding> </properties> <build> <!--This build creates and installs Instrumented JAR file for use by the Systests projects- So we can gather consolidated coverage information--> <plugins> <plugin> & Lt;groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid
          > <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!--<plug In>--> <!--<groupId>org.apache.maven.plugins</groupId>--> <!--<artifactid&gt ;maven-site-plugin</artifactid>--> <!--<executions>--> <!--&LT;EXECUTION&GT;--&G
            T <!--<phase>prepare-package</phase>--> <!--<goals>--> <!--<go Al>site</goal>--> <!--</goals>--> <!--</execution>--> &L t;! --</execUtions>--> <!--</plugin>--> <!--<plugin>--> <!--<groupid>org.ap
        Ache.maven.plugins</groupid>--> <!--<artifactId>maven-source-plugin</artifactId>--> <!--<executions>--> <!--<execution>--> <!--<phase>prepare-pack Age</phase>--> <!--<goals>--> <!--<goal>jar-no-fork</goal>-- > <!--</goals>--> <!--</execution>--> <!--</executions>- -> <!--</plugin>--> <plugin> <groupid>org.apache.maven.plugins</groupid&
        Gt
            <artifactId>maven-javadoc-plugin</artifactId> <executions> <execution>
            <phase>prepare-package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <!--<plugin>--> <!--<groupId>org.apache.maven.plugins</groupId>--> <!--<artifactid>maven-jar-plu
            Gin</artifactid>--> <!--<configuration>--> <!--<archive>--> <!--<manifest>--> <!--&LT;MAINCLASS&GT;ORG.MYBATIS.GENERATOR.API.SHELLRUNNER&LT;/MAINCLASS&G T;--> <!--</manifest>--> <!--</archive>--> <!--</configurat Ion>--> <!--</plugin>--> <!--<plugin>--> <!--<groupid>org.apach
        E.maven.plugins</groupid>--> <!--<artifactId>maven-assembly-plugin</artifactId>--> <!--<configuration>--> <!--<descriptors>--> <!--<descriptor>${p Roject.basedir}/src/main/assembly/src.xml</descriptor>--> <!--</descriptors>--> <!--&LT;/CONFIGURATION&G T;--> <!--<executions>--> <!--<execution>--> <!--<id>bun
            Dle</id>--> <!--<goals>--> <!--<goal>single</goal>--> <!--</goals>--> <!--<phase>package</phase>--> <!--</exe Cution>--> <!--</executions>--> <!--</plugin>--> <!--<plugin>-- > <!--<groupId>org.apache.maven.plugins</groupId>--> <!--<artifactid>maven-r Elease-plugin</artifactid>--> <!--<configuration>--> <!--<arguments>-prele Ase</arguments>--> <!--</configuration>--> <!--</plugin>--> </plugin

S> </build>  <reporting> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jdepend-maven-plugin</artifactId> <version>2.0</version> </plugin > </plugins> </reporting> <dependencies> <dependency> &LT;GROUPID&GT;LOG4J&L t;/groupid> <artifactId>log4j</artifactId> <scope>provided</scope> <option al>true</optional> </dependency> <dependency> <groupid>org.apache.ant</groupid > <artifactId>ant</artifactId> <scope>provided</scope> &LT;OPTIONAL&GT;TRUE&L t;/optional> </dependency> <dependency> <groupId>junit</groupId> <artifa
      ctid>junit</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.hsqldb</groupid> <artifactId>hsqldb</artifactId> <scope>test</scope> </depen
 Dency> </dependencies> </project>
revision of the source code

I do not modify the default class Defaultcommentgenerator here, but create a new one of my own class dg2commentgenerator. According to their own needs to rewrite some methods, mainly generate Mapper interface notes information, generate model object annotation.

Package org.mybatis.generator.internal;
Import Org.mybatis.generator.api.IntrospectedColumn;
Import org.mybatis.generator.api.IntrospectedTable;
Import Org.mybatis.generator.api.dom.java.Field;
Import Org.mybatis.generator.api.dom.java.InnerClass;
Import Org.mybatis.generator.api.dom.java.Method;
Import Org.mybatis.generator.api.dom.java.Parameter;
Import org.mybatis.generator.api.dom.xml.TextElement;
Import org.mybatis.generator.api.dom.xml.XmlElement;

Import org.mybatis.generator.config.MergeConstants;

Import java.util.List; /** * Custom Annotation Generation * * @author It_donggua * @version V1.0 * @create 2016-09-02 PM 06:28 * * public class Dg2comment Generator extends defaultcommentgenerator{public void Addgeneralmethodcomment (method method, Introspectedtable Intr
        ospectedtable) {StringBuilder sb = new StringBuilder (); Method.addjavadocline ("/**"); $NON-nls-1$//Method.addjavadocline ("* This method is generated by MyBatis generator."); $NON-nls-1$//Sb.append ("* This method is corresponds to the database table");
        $NON-nls-1$ sb.append ("*");
        if (Method.isconstructor ()) {sb.append ("construct query condition");
        String method_name = Method.getname ();
        if ("Setorderbyclause". Equals (Method_name)) {sb.append ("Set sort field");
        else if ("setdistinct". Equals (Method_name)) {sb.append ("Set filter Duplicate data");
        else if ("Getoredcriteria". Equals (Method_name)) {sb.append ("Get the current query condition instance");
        else if ("isdistinct". Equals (Method_name)) {Sb.append ("Filter Duplicate data");
        else if ("Getorderbyclause". Equals (Method_name)) {sb.append ("Get sort Field");
        else if ("Createcriteria". Equals (Method_name)) {sb.append ("Create a query condition");
        else if ("createcriteriainternal". Equals (Method_name)) {sb.append ("Internal build Query Condition object");
        else if ("clear". Equals (Method_name)) {sb.append ("purge query condition"); } else if ("Countbyexample". Equals (Method_name)) {sb.append ("Get the number of database records according to a specified condition");
        else if ("Deletebyexample". Equals (Method_name)) {sb.append ("delete database eligible records based on specified conditions");
        else if ("Deletebyprimarykey". Equals (Method_name)) {sb.append ("delete database records based on primary Key");
        else if ("Insert". Equals (Method_name)) {sb.append ("New write Database Record");
        else if ("insertselective". Equals (Method_name)) {sb.append ("dynamic field, write database record");
        else if ("Selectbyexample". Equals (Method_name)) {sb.append ("query eligible database records based on specified criteria");
        else if ("Selectbyprimarykey". Equals (Method_name)) {sb.append ("Get a database record based on a specified primary key");
        else if ("updatebyexampleselective". Equals (Method_name)) {sb.append ("Update eligible database records dynamically based on specified conditions");
        else if ("Updatebyexample". Equals (Method_name)) {sb.append ("update eligible database records according to specified conditions"); else if ("updatebyprimarykeyselective". Equals (Method_name)){sb.append ("dynamic field, updating eligible database records according to primary Key");
        else if ("Updatebyprimarykey". Equals (Method_name)) {sb.append ("update eligible database records according to primary Key");
        } sb.append (",");
        Sb.append (Introspectedtable.getfullyqualifiedtable ());

        Method.addjavadocline (Sb.tostring ());
        Final list<parameter> parameterlist = Method.getparameters ();
            if (!parameterlist.isempty ()) {Method.addjavadocline ("*");
            if ("or". Equals (Method_name)) {sb.append ("Add or query conditions for construction or query");
            } else {if ("or". Equals (Method_name)) {sb.append ("Create a new or query condition");
        } String Paramtername;
            for (Parameter parameter:parameterlist) {sb.setlength (0); Sb.append ("* @param");
            $NON-nls-1$ paramtername = Parameter.getname ();
            Sb.append (Paramtername);
if ("Orderbyclause". Equals (Paramtername)) {                Sb.append ("sorted fields");
            $NON-nls-1$} else if ("distinct". Equals (Paramtername)) {Sb.append ("Filter Duplicate data");
            else if ("Criteria". Equals (Paramtername)) {Sb.append ("filter Condition instance");
        } method.addjavadocline (Sb.tostring ());

        }//Addjavadoctag (method, False); Method.addjavadocline ("* *"); $NON-nls-1$} @Override public void Addclasscomment (Innerclass innerclass, introspectedtable INTROSPECTEDTA BLE) {//class annotation, regardless of String shortname = Innerclass.gettype

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.