MyBatis Basics of Entry (ix)----reverse engineering

Source: Internet
Author: User

first, what is reverse engineering

Mybaits requires programmers to write their own SQL statements,MyBatis The official reverse engineering can be automatically generated for a single table mybatis Execute the required code ( Mapper.java,mapper.xml , po.. )

In practical development, the common reverse engineering method is to generate Java code from the table of the database.

Second, download reverse engineering

To the official website to download reverse engineering, as of today the latest version is 1.3.3. Official Reverse engineering documentation

I'm using 1.3.2 version of: Mybatis_generator jar package. zip

MyBatis Learning materials (Chinese and English documents, common parameters configuration, etc.). rar

Iii. methods of Use

1. Generating a code configuration file

Configuration of the Generatorconfig.xml

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE generatorconfiguration3 Public "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en"4 "Http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">5 6 <generatorconfiguration>7     <ContextID= "Testtables"Targetruntime= "MyBatis3">8         <Commentgenerator>9             <!--whether to remove automatically generated comments true: Yes: false: No -Ten             < Propertyname= "Suppressallcomments"value= "true" /> One         </Commentgenerator> A         <!--Database connection Information: Driver class, connection address, user name, password, this configuration is MySQL, of course, can also configure databases such as Oracle - -         <jdbcconnectionDriverclass= "Com.mysql.jdbc.Driver" - Connectionurl= "Jdbc:mysql://localhost:3306/mybatis"userId= "root" the Password= "root"> -         </jdbcconnection> -  -         <!--by default, the JDBC decimal and NUMERIC types are resolved to Integer, true when the JDBC decimal + and NUMERIC types resolved to Java.math.BigDecimal - -         <Javatyperesolver> +             < Propertyname= "Forcebigdecimals"value= "false" /> A         </Javatyperesolver> at  -         <!--Targetproject: Where to generate the PO class - -         <JavamodelgeneratorTargetpackage= "Com.mybatis.entity" - Targetproject= ". \src"> -             <!--enablesubpackages: Do you want the schema to be the suffix of the package - -             < Propertyname= "Enablesubpackages"value= "false" /> in             <!--The space returned from the database before and after the value is cleaned - -             < Propertyname= "Trimstrings"value= "true" /> to         </Javamodelgenerator> +         <!--Targetproject:mapper The location of the map file generation - -         <SqlmapgeneratorTargetpackage= "Com.mybatis.mapper" the Targetproject= ". \src"> *             <!--enablesubpackages: Do you want the schema to be the suffix of the package - $             < Propertyname= "Enablesubpackages"value= "false" />Panax Notoginseng         </Sqlmapgenerator> -         <!--Targetpackage:mapper the location generated by the interface - the         <Javaclientgeneratortype= "Xmlmapper" + Targetpackage= "Com.mybatis.mapper"Targetproject= ". \src"> A             <!--enablesubpackages: Do you want the schema to be the suffix of the package - the             < Propertyname= "Enablesubpackages"value= "false" /> +         </Javaclientgenerator> -         <!--Specify database Tables - $         <TableTableName= "Items"></Table> $         <TableTableName= "Orders"></Table> -         <TableTableName= "OrderDetail"></Table> -         <TableTableName= "T_user"></Table> the     </Context> - </generatorconfiguration>

2. Executing the BUILD Program

Generatorsqlmap.java's Code:

1 ImportJava.io.File;2 Importjava.util.ArrayList;3 Importjava.util.List;4 5 ImportOrg.mybatis.generator.api.MyBatisGenerator;6 Importorg.mybatis.generator.config.Configuration;7 ImportOrg.mybatis.generator.config.xml.ConfigurationParser;8 ImportOrg.mybatis.generator.internal.DefaultShellCallback;9 Ten  Public classGeneratorsqlmap { One  A      Public voidGenerator ()throwsexception{ -  -list<string> warnings =NewArraylist<string>(); the         BooleanOverwrite =true; -         //Specifying a reverse engineering configuration file -File ConfigFile =NewFile ("Generatorconfig.xml");  -Configurationparser CP =Newconfigurationparser (warnings); +Configuration config =cp.parseconfiguration (configfile); -Defaultshellcallback callback =Newdefaultshellcallback (overwrite); +Mybatisgenerator Mybatisgenerator =Newmybatisgenerator (config, A callback, warnings); atMybatisgenerator.generate (NULL); -  -     }  -      Public Static voidMain (string[] args)throwsException { -         Try { -Generatorsqlmap Generatorsqlmap =NewGeneratorsqlmap (); in generatorsqlmap.generator (); -}Catch(Exception e) { to e.printstacktrace (); +         } -          the     } *  $}

The above code in the official document code, which supports a lot of languages, we can go to the official look. The following picture is from the official intercept

Once configured, running the code directly generates the corresponding entity and mapper as well as the Mapper.xml file in the configuration's corresponding directory. (like hibernate entity mappings), of course, in addition to this method generation, you can also download the Mybatis_generator plugin generation.

When you're done, refresh the project to see the resulting file.

3. Reverse Engineering considerations

If the Mapper.xml file already exists, the Mapper.xml file content is not overwritten but the content is incremented, resulting in mybatis resolution failure.

Workaround: Delete the original mapper.xml file that has been generated to regenerate.

Mybais automatically generated entity entities and Mapper.java files are not appended to the content but are directly overwritten without problems.

4. Project Catalogue

MyBatis Basics of Entry (ix)----reverse engineering

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.