1. First introduced in dependence file pom
Version own definition
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
2. Reverse engineering block
File configFile = new File ( "mybatisg.xml "); //mybatisg.xml is reverse engineering profile, you can define their own
ConfigurationParser cp = new new ConfigurationParser (Represents warnings);
the Configuration config = cp.parseConfiguration (configFile);
DefaultShellCallback callback = DefaultShellCallback new new (Overwrite);
MyBatisGenerator myBatisGenerator = new new MyBatisGenerator (config, the callback, Represents warnings);
myBatisGenerator.generate (null);
3. Configure Reverse engineering profile (omitted)
4. custom builder
Our concern is how to generate self-rule definition file
First, let us plug adapter class inherits Mybatis-generator provided
Our main concern these override method
1. Insert the inlet (used to generate customized PO) program
2. Insert the inlet (custom xml file) program
3. Insert the inlet (custom Example) program
4. Insert the inlet (custom mapper Interface) program
First look at the first one, customize our PO
1 . PO rules to generate our class definition, need to inherit JavaSrcGenerator official, and the rewriting generate method
In generate overridden method, we can do whatever they want to PO class, practice fraud (false
API naming is very clear. Make a point, IDE auto-completion of many ways of usage are obvious.
2 .XML file Custom
First, the XML file that contains the rules of inheritance first official abstract class XML node generator
Then, in this method rewrite custom plug-in class, the XML root node as a parameter addElements throw our method, we can manipulate the XML file in addElements overridden method in
In fact, come to essentially modify the document based on the incoming information table (introspectedTable), but in order to function modular, Generator and complete our custom logic with our method is better addElements
I wrote it myself, then there is no logic to achieve direct rule in addElements direct method, but rather to another layer of objects
The real logic operation only root node
We want a Select the XML file in Riga, the Select the name (also id, also mapper file corresponding method name) is selectByExampleSelectiveOptimized
Is intended to write select * from A a inner join (select id from A where col = xxx) as b on a.id = b.id limit m, n
Such auxiliary col column index on the situation, different select * from A where col = xxx limit m, n
We can prevent innodb in the trees clustered index scan a bunch of useless records, make full use of the cache memory of the page
Renderings:
Custom core idea of reverse engineering:
In fact, in Riga node node (XmlElement), nested layers
Each node is represented by <XXX> </ XXX> this structure, in the node where the plus (the addElement method), then the node is the BBB <XXX> <BBB> </ BBB> </ XXX>
The above node can have attributes (the Attribute) aaa has attributes such as XXX: <XXX aaa = ""> </ XXX>
Another node is a text node (TextElement), such insertion (the addElement Method) XXX text node in the MMM: <XXX aaa = ""> <BBB> </ BBB> MMM </ XXX>
(BBB inserted first, so that later MMM BBB)
Finally, the root node into XXX. root.addElement (XXX). File will have a <XXX aaa = ""> <BBB> </ BBB> MMM </ XXX> this sentence.
Note that without inserting MMM and BBB in the root because they are attached to the XXX
Two parameters in the mapper, a List <String> type, i.e. param2 is a List <String> Type
Set here all parameters and removed intentions are:
1. If these two parameters are empty, with select * from ...
2. If all is not empty, if there are A, B, C String all of these then that select A, B, C from ...
3. If all empty, removed is not empty, if the table has the fields A, B, C, D, E, F and removed has A, B, C of these three fields is then select D, E, F from. ..
Example Mapper and custom rules similar to the above PO (free future will perfect this article