Modify the source code of mybatis-generator-1.3.2 to realize the detailed explanation of custom code generation (1)

I believe that many people have used mybatis-generator-core to generate pojo and xml files, but the last version of this tool is version 1.3.2 , which has not been updated since July 2012. At the same time, because this thing is written by a foreigner, it may be It is inconsistent with some of the habits of our Chinese people, so it will always feel uncomfortable in use. Elephant also has this experience, so I have made some changes to the source code, and now I will share these summaries with friends who need it. First of all, the elephant has to say that I don't have a deep understanding of mybatis-generator-core . I just use it as a tool, but I feel that it is not easy to use, so I learned a little bit and changed it by the way. Therefore, if you know about the places that this article does not cover, please share more. Elephant is grateful. In addition, my modification to the source code is mainly for the MySQL database, which will be mentioned in the second part. The project itself is on top of googlecode , but there is a "wall", you know. So have to change the way to get the source code. First create a new maven project, add the following dependencies, and use maven 's Download Sources to obtain the source code of mybatis-generator-core .
    
    
    

< dependency >
     < groupId > org.mybatis.generator </ groupId >
         < artifactId > mybatis-generator-core </ artifactId >
         < version > 1.3.2 </ version >
</ dependency >

    Unzip the source code into the project, and then you can start modifying. Remember to remove the previous dependencies! I mainly explain which source code to change, and the role. org.mybatis.generator.api.dom.OutputUtilities In mybatis-generator , I think the first thing that should be changed is the OutputUtilities class. There is an xmlIndent method in it to control the indentation of spaces in the generated xml file. The default is two A space, but four spaces have been deeply rooted for us, so they must be changed. Just add two spaces in sb.append( " " ) . The org.mybatis.generator.api.dom.java.InnerClass class is used to control the generation of pojo classes and formatting. I mainly modify the line breaks between attributes and methods. Source file: Lines 178-180 add a newline after each attribute. Elephants like compact code, so the code in the red box is removed. Look at the figure below, pay attention to line 172 of the red line, adding a newline here is to make a blank line between the first property generated and the declaration of the class. If you think getters and setters
    
    
    

    
    
    
    

    
    

    The method does not want to have blank lines. You can remove this code from lines 206-208 of the source file. You can follow your own habits. Elephant removes it here. org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3Impl has a getGeneratedXmlFiles() method on line 210 of this class . Pay attention to the true attribute of the red line in the figure below. It is used to control whether to merge and generate xml files. Obviously, merge is the default here. , For us, modifying the database table structure is a frequent occurrence. Can't we delete it manually every time it is regenerated? So here we must resolutely change it to false Now we change true to false , the problem is solved, but where does it work? See line 252 of the org.mybatis.generator.api.MyBatisGenerator class , which is contained in the generate method. The first if will judge whether to merge or not. The above figure has shown that isMergeable is true
    
    
    

    
    

    , so of course it is merged, but after we change it to false , we must add the overwrite parameter to the executed command line , so that the shellCallback .isOverwriteEnabled() can take effect, so as to realize the overwrite generation of our xml file. In addition to merging xml files by default, mybatis-generator also generates a pojo with the Example suffix by default , which is somewhat similar to Hibernate 's Criteria object. In addition, it will generate a bunch of things with id="xxxByExample" in xml by default , which all use the Example class, not only that, but also a bunch of related <sql> tags in order to be used together These are not needed in the eyes of elephants. Don't we use MyBatis for its simplicity and convenience? So I don't want to see it, what needs to be changed? It can be modified in the configuration file. Adding these property settings will filter out what I said before, and the Example class will not appear again. Oh, the world is finally cleaner.
    
    

    
    These properties are set to the org.mybatis.generator.config.TableConfiguration class, which has the set method corresponding to these properties, so where is the boolean value in the configuration file set? They are done when the parsing configuration file is initialized. The class that completes it is: org.mybatis.generator.config.xml.MyBatisGeneratorConfigurationParser This code is in parseTable , tc is TableConfiguration , now you understand? Another unpleasant part of mybatis-generator is that it generates a bunch of English comments in pojo and xml . This thing is completely useless. I want to remove it and add this paragraph to the configuration file like the following. Seeing that there are children's shoes here, I will have an opinion, Mumei's, isn't this a one-size-fits-all. I just don't want to generate annotations in xml , and I want to add custom annotations in the pojo class. This can be done. The next elephant will talk about this. This article is original for Pineapple Elephant, if you want to reprint, please indicate the source. http://www.blogjava.net/bolo _
    
    

    
    
    
    
    

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327044786&siteId=291194637