Comprehensive analysis of the use of MyBatis generator _java

Source: Internet
Author: User
Tags generator numeric

I. Core document Generator.xml

Specify key information such as the location of the database jar package, database connection information, location of the build package, table name, and so on. The file is placed in any location.

<! DOCTYPE generatorconfiguration Public "-//mybatis.org//dtd mybatis generator Configuration" "1.0//en Mybatis.org/dtd/mybatis-generator-config_1_0.dtd "> <generatorConfiguration> <!--database jdbc-driven jar package address-- > <classpathentry location= "F:\xy\jars\mysql-connector-java-5.0.7-bin.jar"/> <context id= "Db2tables" Targetruntime= "MyBatis3" > <!--whether to remove automatically generated annotations--> <commentGenerator> <property name= " Suppressallcomments "value=" true "/> </commentGenerator> <!--database connection information--> <jdbcconnection driverclass= "Com.mysql.jdbc.Driver" connectionurl= "Jdbc:mysql://localhost:3306/db_mybatistest" userid= "root" password= "Mysqltest" > </jdbcConnection> <!--false:jdbc DECIMAL, numeric type resolution to Integer, default mode--> <!-- True:jdbc DECIMAL, numeric type resolution to Java.math.BigDecimal--> <javaTypeResolver> <property name= " Forcebigdecimals "value= false"/> </javaTypeResolver> <!--build the package name and location of the model--> <javamodelgenerator TargetpaCkage= "Com.xy.model" targetproject= "F:\xy\mybatis-generator\src" > <!--let schema be the suffix of the package--> Name= "Enablesubpackages" value= "true"/> <!--the space before and after the value returned from the database is cleaned--> <property name= "trimstrings" value= " True "/> </javaModelGenerator> <!--generate the package name and location of the mapping file--> <sqlmapgenerator targetpackage=" com.xy.mapping "targetproject=" F:\xy\mybatis-generator\src "> <property name=" enablesubpackages "value=" false "/> </ Sqlmapgenerator> <!--generate the package name and location of DAO--> <javaclientgenerator type= "Xmlmapper" targetpackage= "Com.xy.dao" targetproject= "F:\XY\MYBATIS-GENERATOR\SRC" > <property name= "enablesubpackages" value= "true"/> </ Javaclientgenerator> <!--tablename: Database table--> <!--domainobjectname: JavaBean class name corresponding to database table--> <table Tablename= "T_student" domainobjectname= "Student" enablecountbyexample= "false" enableupdatebyexample= "false" Enabledeletebyexample= "false" enableselectbyexample= "false" Selectbyexamplequeryid= "false"; <!--ignore this field (can be omitted)--> <ignorecolumn column= "name"/> </table> </context> </ Generatorconfiguration>

Second, table tag resolution

① Property

Schema is the database name, TableName is the corresponding database table, Domainobjectname is the entity class to be generated.

To generate an example, you can set Enablecountbyexample to true, and a example class that corresponds to Domainobjectname is generated, false is not generated, and the default policy is true.

Similarly, there are enableupdatebyexample, Enabledeletebyexample, Enableselectbyexample, Selectbyexamplequeryid properties.

② Child Label

To manipulate some database fields, you can add the following label to the table label

1. Ignore a field

<ignorecolumn column= "Name"/>

2, regardless of the database field is what type, generated class properties are varchar

<columnoverride column= "Long_varchar_field" jdbctype= "VARCHAR"/>

Third, generate

Mybatis-generator-core-1.3.2.jar is a core jar package that can be downloaded on the web. The Command window executes the statement, and the execution succeeds and the code is found in the location specified in the Generator.xml file.

Java-jar F:\xy\jars\mybatis-generator-core-1.3.2.jar-configfile F:\xy\generator.xml-overwrite

Iv. Summary

Using MyBatis generator requires

① two jar packages--mybatis-generator-core-1.3.2.jar and database jar packs

② a configuration file Generator.xml

③ Execution Statement

V. Matters of attention

①generator.xml format: Must be encoded in UTF-8 without a BOM format, with notepad++ conversion.

② Note the availability of the database package, invalid database package conversion will be an error.

The above is a small set to introduce the MyBatis generator the use of comprehensive analysis, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.