Reverse engineering of 12.MyBatis

Source: Internet
Author: User

The following directly can be downloaded to use, only need to modify the configuration file path just fine

What is reverse engineering

Simply put, the Java is automatically generated by a single table in the database code.

Mybatis officially provides reverse engineering to automatically generate Mybatis code for a single table (mapper.java\mapper.xml\pojo class )

in enterprise development, reverse engineering is a very common tool.


Download Reverse Engineering

https://github.com/mybatis/generator/releases/tag/mybatis-generator-1.3.2


How to use

1, modify The generator configuration file;

2, the use of Java class to perform reverse engineering;

3 . Copy the generated code into the project.


First step: Modify the generator configuration file

<?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>    <context id= "Testtables"  targetRuntime= " MyBatis3 ">        <commentGenerator>             <!--  Whether to remove automatically generated annotations  true: Yes  : false: no   -->            <property name= " Suppressallcomments " value=" true " />        </ commentgenerator>        <!--Database connection information: Driver class, connection address, user name, password  -- >        <jdbcconnection driverclass= "CoM.mysql.jdbc.driver "            connectionurl=" Jdbc:mysql://localhost:3306/mybatis " userid=" root "             password= "Root" >        </jdbcConnection>         <!-- <jdbcconnection driverclass= " Oracle.jdbc.OracleDriver "            connectionurl = "JDBC:ORACLE:THIN:@127.0.0.1:1521:YYCG"               userid= "YYCG"             password= " YYCG ">        </jdbcConnection> -->         <!--  Default false to resolve jdbc decimal  and  NUMERIC  types to   Integer, for  true when the jdbc decimal&nbsp, and              NUMERIC  type resolves to java.math.bigdecimal -->        <javatyperesolver>             <property name= " Forcebigdecimals " value=" false " />        </ javatyperesolver>        <!-- targetproject: The location of the PO class is generated  - ->        <javamodelgenerator targetpackage= " Cn.mybatis.sm.domain "            targetproject=". \ src ">            <!-- enablesubpackages : Do you want the schema to be the suffix of the package  -->            < Property name= "Enablesubpackages"  value= "false"  />            <!--  Spaces returned from the database before and after cleaning  -- >            <property name= " Trimstrings " value=" true " />        </ javamodelgenerator>        <!-- targetproject: Location of Mapper Map file generation  -->        <sqlMapGenerator  Targetpackage= "Cn.mybatis.sm.mapper"               targetproject= ". \src" >            <!--  enablesubpackages: Do you want the schema to be the suffix of the package  -->             <property name= "Enablesubpackages"  value= "false"  />         </sqlmapgenerator>        <!-- targetpackage:mapper Interface generated location  -->         <javaclientgenerator type= "Xmlmapper"              targetpackage= "Cn.mybatis.sm.mapper"               targetproject= ". \src" >             <!-- enablesubpackages: Do you want the schema to be the suffix of the package  -->             <property name= "EnableSubPackages"  value= " False " />        </javaClientGenerator>         <!--  Specify database tables  -->         <table tablename= "Items" ></table>        < Table tablename= "Orders"; </table>        <table tablename= "OrderDetail" ></ Table>        <table tablename= "User" ></table>             </context></ Generatorconfiguration>

Step two: Use Java classes to perform reverse engineering

package generator;import java.io.file;import java.util.arraylist;import java.util.list; import org.mybatis.generator.api.mybatisgenerator;import org.mybatis.generator.config.configuration; import org.mybatis.generator.config.xml.configurationparser;import  org.mybatis.generator.internal.defaultshellcallback;/** *  Reverse engineering. Create  pojo,mapper interfaces and mapper mappings through the database  *  @author   Liu Zedong  *  @date  2015 June 22   PM 9 : 21:04 */public class generator {    public static void  main (STRING[]&NBSP;ARHS)  throws exception{        list <String> warnings = new ArrayList<String> ();         boolean overwrite = true;        file  configfile = new file ("Src/generator.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);     }}


Step three: Copy the generated code into the project

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/C9/wKioL1WIMbmxJ8tOAAFczBNe6T8554.jpg "title=" { Iuv1pf_l00s{c]7znw_{71.png "alt=" Wkiol1wimbmxj8toaafczbne6t8554.jpg "/>


Precautions

Mapper.xml when a file is already present, if you regenerate it Mapper.xml file, content is not overwritten but content is appended, resulting in MyBatis parsing failed.

Workaround: Delete the previously generated Mapper XML file to be generated again.

Mybatis Auto-generated PO and Mapper.java the file is not content but is directly overwritten without this problem.



Reverse engineering of 12.MyBatis

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.