Speed up development with mybatis-generator tools

Source: Internet
Author: User
Tags manual writing

When using MyBatis, it is necessary to write a large number of mapping mapping files, manual writing is heavy and error prone. Fortunately, Mybatis-generator can be used to help us automatically generate these files, greatly improving the efficiency of development.

1. Preparatory work

Download the Mybatis-generator jar package from https://github.com/mybatis/generator/releases;

Download the database driver package from http://www.grepcode.com.

Create the following structure directory:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/80/26/wKiom1c5ZvSyZcv4AACJu0KIDoM213.png "title=" Qq20160516141847.png "alt=" Wkiom1c5zvsyzcv4aacju0kidom213.png "/>

2.generator.xml Introduction

Before generating related files, you need to configure the configuration file name arbitrarily and suffix XML. (attribute-related interpretations have comments in XML, which are confusing to try to see how the effect is different)

<?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>
<!--database-driven
<classpathentry location= "Mysql-connector-java-5.1.36.jar"/>
<context id= "Db2tables" targetruntime= "MyBatis3" >
<commentGenerator>
<property name= "Suppressdate" value= "true"/>
<property name= "Suppressallcomments" value= "true"/>
</commentGenerator>
<!--database link Address account password--
<jdbcconnection driverclass= "Com.mysql.jdbc.Driver" connectionurl= "Jdbc:mysql://localhost/mybatis" userid= " Root "password=" Yxc. " >
<!--Oracle Database
<jdbcconnection driverclass= "Oracle.jdbc.driver.OracleDriver"
Connectionurl= "Jdbc:oracle:thin: @localhost: 1521:orcl"
Userid= "username"
password= "Password" >
</jdbcConnection>
-
</jdbcConnection>
<!--default False to parse the JDBC DECIMAL and NUMERIC types into integers;
True to parse the JDBC DECIMAL and NUMERIC types into Java.math.BigDecimal
-
<javaTypeResolver>
<property name= "Forcebigdecimals" value= "false"/>
</javaTypeResolver>
<!--generate model class storage location-
<javamodelgenerator targetpackage= "Yb.model" targetproject= "src" >
<!--enablesubpackages: Do you want the schema to be a suffix of the package--
<property name= "Enablesubpackages" value= "true"/>
<!--the value returned from the database is cleared before and after the space-
<property name= "Trimstrings" value= "true"/>
</javaModelGenerator>
<!--generate Map file storage location--
<sqlmapgenerator targetpackage= "yb.mapping" targetproject= "src" >
<property name= "Enablesubpackages" value= "true"/>
</sqlMapGenerator>
<!--generate a DAO class storage location-
<javaclientgenerator type= "Xmlmapper" targetpackage= "Yb.dao" targetproject= "src" >
<property name= "Enablesubpackages" value= "true"/>
</javaClientGenerator>
<!--generate the corresponding table and class name--
<!--tableName: A database table for automatic code generation; Domainobjectname: JavaBean class name corresponding to a database table
To generate an example to set Enablecountbyexample to True, a example class corresponding to Domainobjectname is generated, false is not generated, and the default policy is true.
Similar to the Enableupdatebyexample, Enabledeletebyexample, Enableselectbyexample, and Selectbyexamplequeryid properties.
-
<table tablename= "Phone" domainobjectname= "Phone" enablecountbyexample= "false" enableupdatebyexample= "false" Enabledeletebyexample= "false" enableselectbyexample= "false" Selectbyexamplequeryid= "false" ></table>
</context>
</generatorConfiguration>

3. Running

There are four types of operations: command Generation (simplest), Java generation, ant build, and maven build. Here say the first two kinds, interested in the rest can be on the MyBatis official website to learn.

1) Open cmd Command window, CD to working directory, run the following command

Java-jar mybatis-generator The file path of the package-configfile generator.xml the file path-overwrite command.

This example is:

Java-jar Mybatis-generator-core-1.3.2.jar-configfile Generator.xml-overwrite

Successful output: MyBatis Generator finished successfully.

2) Java generation

list<string> warnings = new arraylist<string> ();
Boolean overwrite = true;
File ConfigFile = new file ("Generatorconfig.xml");
Configurationparser cp = new Configurationparser (warnings);
Configuration config = cp.parseconfiguration (configfile);
Defaultshellcallback callback = new Defaultshellcallback (overwrite);
Mybatisgenerator mybatisgenerator = new Mybatisgenerator (config, callback, warnings);

Mybatisgenerator.generate (NULL);

In fact, Java operation, Subdivision can be divided into two, there is a way to study on the official website.

4. Effects

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/80/23/wKioL1c5a7vi79A2AACVIFv3us0207.png "title=" Qq20160516143659.png "alt=" Wkiol1c5a7vi79a2aacvifv3us0207.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/80/24/wKioL1c5bQbwvtmwAAA-fHwN4yU910.png "style=" float: none; "title=" Qq20160516144214.png "alt=" Wkiol1c5bqbwvtmwaaa-fhwn4yu910.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/80/24/wKioL1c5bQejpsVPAADUqRr-h6o142.png "style=" float: none; "title=" Qq20160516144224.png "alt=" Wkiol1c5bqejpsvpaaduqrr-h6o142.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/80/24/wKioL1c5bQeT31-PAABZHp5ls8U801.png "style=" float: none; "title=" Qq20160516144233.png "alt=" Wkiol1c5bqet31-paabzhp5ls8u801.png "/>

After generating the code, depending on your actual project schema, you can make the appropriate modifications to the generated code, such as bringing the database management to spring and so on.

5. Attention Points

1) generator.xml format: Must be in UTF-8 no BOM format encoding, with notepad++ conversion.
2) Note the availability of the database package, invalid database package conversion will be error.

Speed up development with mybatis-generator tools

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.