MyBatis code generator (idea, Maven) and configuration details (partial configuration you should not know)

Source: Internet
Author: User

In the process of using MyBatis , when handwritten JavaBean and XML are written more and more, they are increasingly agreeing to make mistakes. This repetitive work, of course, we do not want to do so much.

Fortunately, MyBatis provides us with a powerful code generation--mybatisgenerator.

With a simple configuration, we can generate various types of entity classes, mapper interfaces, mapperxml files, example objects, and so on. Through these generated files, we can easily carry out a single table for the operation of the increase and deletion check.

The following tools are used by idea

1 Creating the Code generator 1.1 creating a Maven Project

1.1.1 Menu, select New Item

File | New | Project

1.1.2 Select the leftMaven

Since we are just creating a normal project, click here Next .

1.1.3 Input GroupId andArtifactId

    • In my project,

GroupId Filling Com.homejim.mybatis

Artifactid Filling Mybatis-generator

Click Next .

1.1.4Finish

With the above steps, a common Maven project is created.

1.2 Configuring Generator.xml

In fact, the name does not matter, as long as the following pom.xml file in the corresponding on the good.

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE generatorconfiguration Public "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en" "/http Mybatis.org/dtd/mybatis-generator-config_1_0.dtd "><generatorConfiguration> <!--the full path of the local database driver jar Package-- > <classpathentry location= "C:\Users\\Administrator\\.m2\repository\\mysql\\mysql-connector-java\\8.0.12\\ Mysql-connector-java-8.0.12.jar "/> <context id=" context "targetruntime=" MyBatis3 "> <commentgenerator > <property name= "suppressallcomments" value= "false"/> <property name= "Suppressdate" Val Ue= "true"/> </commentGenerator> <!--related configuration of the database--<jdbcconnection Dr iverclass= "Com.mysql.jdbc.Driver" connectionurl= "Jdbc:mysql://localhost:3306/mybatis" userId = "root" password= "jim777"/> <javaTypeResolver> <property name= "fOrcebigdecimals "value=" false "/> </javaTypeResolver> <!--entity class generated and <javamodelge            Nerator targetpackage= "com.homejim.mybatis.entity" targetproject= ". \src\main\java" > <property name= "Enablesubpackages" value= "false"/> <property name= "trimstrings" value= "true"/&gt        ;                </javaModelGenerator> < location of!--*mapper.xml file sqlmapgenerator--> <sqlmapgenerator Targetpackage= "Mybatis/mapper" targetproject= ". \src\main\resources" > <property name= "E Nablesubpackages "value=" false "/> </sqlMapGenerator> < location of the Mapper interface files--<javac                             Lientgenerator type= "Xmlmapper" targetpackage= "Com.homejim.mybatis.mapper" Targetproject= ". \src\main\java" > <property name= "enablesubpackages" value= "false"/> &L T;/javaclientgeNerator> <!--related table configuration-<table tablename= "blog"/> </context></generatorconfigur Ation>

Some things need to be changed:

    1. The full path of the local database driver jar package ( must be changed ).
    2. Related configuration of database ( must be changed )
    3. Configuration of related tables ( must be changed )
    4. The entity class generates the stored location.
    5. Mapperxml the location where the generated files are stored.
    6. The location where the Mapper interface is stored.

If you do not know how to change, please look at the following configuration detailed .

1.3 Configuring Pom.xml

Add some content on the original basis.

<?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> <groupId> Com.homejim.mybatis</groupid> <artifactId>mybatis-generator</artifactId> <version>1.0- Snapshot</version> <!--Add these on the original basis--<build> <finalname>mybatis-generator</fina lname> <plugins> <plugin> <groupid>org.mybatis.generator</groupid > <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.                   7</version> <configuration> <!--here to note that the file matches the file above-- <configurationfile>src/main/resources/generator.xmL</configurationfile> <verbose>true</verbose> <overwrite>tru e</overwrite> </configuration> <executions> <executi                            on> <id>generate MyBatis artifacts</id> <goals> <goal>generate</goal> </goals> </execut                        ion> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactid>mybatis-ge Nerator-core</artifactid> <version>1.3.7</version> </depend ency> </dependencies> </plugin> </plugins> </build> < !--to this end--></projecT> 

It is important to note that the files in ConfigurationFile refer to generator.xml. So the path is written to the relative path of the file, and the name is the same as the file.

Here, Mybatis-generator can be used.

1.4 Use and testing

1.4.1 Open Maven Projects view

On idea, open:

View | Tools | Windwos | Maven Projects

1.4.2 Maven Projects, double-click Mybatis-generator

On the right you can see Maven Projects now. Locate the Mybatis-generator plugin.

Mybatis-generator | Plugins | Mybatis-generator | Mybatis-generator

1.4.3 Double-click Run

After running correctly, generate the code to get the following structure

2 Detailed XML Configuration

Just the simple use above is not cool enough. Then we can make the configuration by changing the way the Generator.xml configuration file is built.

2.1 Priority

It is recommended to view official documents.

English is good: official website.

Chinese Translation version: translated website

2.2 2.2.1 Property tags not in the official website

The label on the official website is only used to specify the attributes of the element, as to how to use no detailed explanation.

2.2.1.1 Separator Correlation
<property name="autoDelimitKeywords" value="true"/><property name="beginningDelimiter" value="`"/><property name="endingDelimiter" value="`"/>

The above configuration corresponds to MySQL, where the delimiter is used when the fields in the database are the same as the keywords in the database.

For example, our data column is delete , according to the above configuration, where it appears, it becomes `delete` .

2.2.1.2 encoding

The default is to use the encoding of the current system environment, which can be configured as GBK or UTF-8.

<property name="javaFileEncoding" value="UTF-8"/>

I think the project is UTF-8, if you specify generate GBK, the automatically generated Chinese is garbled.

2.2.1.3 formatting
<!--格式化生成的 Java 代码--><property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/><!--格式化生成的 XML--><property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>

These are clearly customizable implementations.

2.2.2 Plugins label

The plugins tag is used to extend or modify code generated by the code generator.

In the generated XML, there is no <cache> this tag. The tag is configured for caching.

If we want to generate this tag, we can configure it in plugins .

<plugin type="org.mybatis.generator.plugins.CachePlugin" >            <property name="cache_eviction" value="LRU"/></plugin>

For example, you want to generate the JavaBean to implement the Serializable interface itself.

<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

You can also customize the plugin.

These plug-ins are quite useful, and feel that follow-up can be specifically opened an article to explain.

2.2.3 Commentgenerator Label

Look at the name, you know it is used to generate comments.

Default configuration:

    <commentGenerator >        <property name="suppressAllComments" value="false"/>        <property name="suppressDate" value="false"/>        <property name="addRemarkComments" value="false"/>    </commentGenerator>

Suppressallcomments: Prevents comments from being generated, the default value is False.

Suppressdate: The comment that prevents the build contains a timestamp, which defaults to false.

Addremarkcomments: Note Adds a comment to the database, which defaults to false.

Another is that we can specify our own custom annotation implementation class through the type attribute, generating the annotations we want . The custom implementation class needs to be implemented org.mybatis.generator.api.CommentGenerator .

2.2.4 to Be Continued

You want to add.

Github

The Mybatis-generator in the Mybatis-examples project is all the code that this article uses.

MyBatis code generator (idea, Maven) and configuration details (partial configuration you should not know)

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.