Generator automatic generation of MyBatis configuration and class information

Source: Internet
Author: User

Generator automatically generates mybatis XML configuration, model, map, and other information:
1, download Mybatis-generator-core-1.3.2.jar package.
Website: http://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DGenerator, Download Mybatis-generator-core-1.3.2-bundle.zip, unzip
Find the required jar package under LIB.
2, write the genertor XML file name: Generator.xml

<?XML version= "1.0" encoding= "UTF-8"?>      <!DOCTYPE generatorconfiguration Public "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en" "Htt P://mybatis.org/dtd/mybatis-generator-config_1_0.dtd ">            <generatorconfiguration>        <!--classpathentry: JDBC-driven jar package address for database -        <Classpathentry Location= "E:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />      <ContextID= "Db2tables"Targetruntime= "MyBatis3">        <Commentgenerator>          <!--whether to remove automatically generated comments true: Yes: false: No -          < Propertyname= "Suppressallcomments"value= "true" />          <!--Database connection Information: Driver class, connection address, user name, password -        </Commentgenerator>        <jdbcconnectionDriverclass= "Oracle.jdbc.driver.OracleDriver"Connectionurl= "JDBC:ORACLE:THIN:@198.17.1.1:1521:ORCL"userId= "Unuser"Password= "Password">        </jdbcconnection>          <!--by default, the JDBC decimal and NUMERIC types are parsed to Integer true, and the JDBC decimal and NUMERIC types are resolved to Java.math.BigDecimal  -         <Javatyperesolver>           < Propertyname= "Forcebigdecimals"value= "false" />        </Javatyperesolver>        <!--Targetproject: Where to generate code automatically -        <JavamodelgeneratorTargetpackage= "Com.soft.model"Targetproject= "E:\WebWorkSpace\workspace_js\downAttachdemo\src">            <!--enablesubpackages: Do you want the schema to be the suffix of the package -                 < Propertyname= "Enablesubpackages"value= "true" />          <!--The space returned from the database before and after the value is cleaned -             < Propertyname= "Trimstrings"value= "true" />        </Javamodelgenerator>                <SqlmapgeneratorTargetpackage= "Sqlmap"Targetproject= "E:\WebWorkSpace\workspace_js\downAttachdemo\conf">             < Propertyname= "Enablesubpackages"value= "false" />        </Sqlmapgenerator>                <Javaclientgeneratortype= "Xmlmapper"Targetpackage= "Com.soft.mapping"Targetproject= "E:\WebWorkSpace\workspace_js\downAttachdemo\src">          < Propertyname= "Enablesubpackages"value= "true" />        </Javaclientgenerator>        <!--tableName: A database table for automatic code generation; Domainobjectname: JavaBean class name that corresponds to a database table -        <TableSchema= "Untodo"TableName= "Mocha_t_app"Domainobjectname= "Mochatodoapp" >                </Table>      </Context>             </generatorconfiguration>  

Table Other properties:
Enablecountbyexample= "false"
Enableupdatebyexample= "false"
Enabledeletebyexample= "false"
Enableselectbyexample= "false"
Selectbyexamplequeryid= "false"
The schema is the database name, TableName is the corresponding database table, Domainobjectname is the entity class to be generated,
If you want the mapper configuration file to join the SQL where condition query, you can set enablecountbyexample and so on to true,
This generates a example class corresponding to the Domainobjectname, Enablecountbyexample, and so on, when set to false.
The corresponding example class is not generated.

If the property is not configured in the table, the default fields are born as class properties.
<ignorecolumn column= "FRED"/>//ignore field
<columnoverride column= "Long_varchar_field" jdbctype= "varchar"/>//no matter what type the field is, the generated class property is VARCHAR.

3, there are four kinds of operations: command Generation (simplest), Java generation, Ant generation, maven generation. There are two kinds of, interested in the rest can be mybatis on the official website to learn.

1), run-"Cmd->java-jar jar package file path-configfile generator.xml file path-overwrite command.
As follows:
Java code

Java-jar E:\Websoft\mybaits\mybatis-generator-core-1.3.2\lib\mybatis-generator-core-1.3.2.jar-configfile E:\ Webworkspace\workspace_js\downattachdemo\src\com\mochasoft\down\generator.xml-overwrite

Successful output: MyBatis Generator finished successfully.

2), Java Run key code:

list<string> warnings =NewArraylist<string>(); BooleanOverwrite =true; File ConfigFile=NewFile ("Generatorconfig.xml"); Configurationparser CP=Newconfigurationparser (warnings); Configuration Config=cp.parseconfiguration (configfile); Defaultshellcallback Callback=Newdefaultshellcallback (overwrite); Mybatisgenerator Mybatisgenerator=Newmybatisgenerator (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, after generating the code, according to their actual project schema, the generated code can be modified appropriately, such as the database management with spring and so on.

Generator automatic generation of MyBatis configuration and class information

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.