Spring+springmvc+mybatis (3)--Reverse engineering automatic code generation

Source: Internet
Author: User
Tags generator numeric

1.what.
MyBatis need programmers to write their own SQL statements, MyBatis officially provide reverse engineering, can be automatically generated for the form MyBatis execution required code (MAPPER.JAVA,MAPPER.XML,POJO)

In the actual development of enterprises, the reverse engineering method is used to generate code from the database table.

2.how.
First we need to download the reverse engineering code:

Mybatis-generator-core-1.3.2-bundle.jar

How to use:

(1) Command line
(2) Ant
(3) Maven
(4) XML
(5) Java based
(6) Java plugin
XML is recommended because plug-ins rely on development tools

Import Project:

Then write the reverse engineering configuration file:
Generatorconfig.xml

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE generatorconfiguration Public "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en" "Http://mybatis.or G/dtd/mybatis-generator-config_1_0.dtd "> <generatorConfiguration> <context id=" Testtables "
            Targetruntime= "MyBatis3" > <commentGenerator> <!--whether to remove auto-generated comments true: Yes: false: no-- <property name= "Suppressallcomments" value= "true"/> </commentGenerator> <!--database connection Info: Driver class, connection address, user name, password--<jdbcconnection driverclass= "Com.mysql.jdbc.Driver" connectionurl= "jdbc:m Ysql://localhost:3306/mybatis "userid=" root "password=" MySQL "> </jdbcConnection> < !--<jdbcconnection driverclass= "Oracle.jdbc.OracleDriver" Connectionurl= "jdbc:oracle:thin:@127.0.0.1:1521:

     YYCG "userid=" YYCG "password=" YYCG "> </jdbcConnection>--   <!--default False to parse the JDBC decimal and NUMERIC types to Integer, and to resolve the JDBC decimal and NUMERIC types to Java.math.BigDec when True
        iMAL-<javaTypeResolver> <property name= "Forcebigdecimals" value= "false"/> </javaTypeResolver> <!--targetproject: Generate POS Class Location--<javamodelgenerator targetpackage= "com
            . Ddd.ssm.po "targetproject=". \src "> <!--enablesubpackages: Do you want the schema to be the suffix of the package-- <property name= "Enablesubpackages" value= "false"/> <!--the value returned from the database is cleared before and after the space-to-&LT;PR Operty name= "Trimstrings" value= "true"/> </javaModelGenerator> <!--targetproject:mapper Mapping file Raw
            <sqlmapgenerator targetpackage= "Com.ddd.ssm.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= "Com.ddd.ssm.mapper" targetproject= ". \src" > <!--enablesubpackages: Do you want the schema to be
        Suffix to package--<property name= "Enablesubpackages" value= "false"/> </javaClientGenerator> <!--Specify database table--<table tablename= "items" ></table> <table tablename= "Orders" > </table> <table tablename= "OrderDetail" ></table> <table tablename= "user" ></tabl e> <!--<table schema= "" Tablename= "Sys_user" ></table> <table schema= "" Tablename= "sy S_role "></table> <table schema=" "Tablename=" Sys_permission "></table> <table Schem A= "" Tablename= "Sys_user_role" ></table> <table schema= "" Tablename= "Sys_role_permission" ></table > <! --some table fields need to specify Java type <table schema= "" Tablename= "" > <columnoverride column= "" Javatype= "/&G
        T

 </table> </context> </generatorConfiguration>

Executing the Build program:


Import Java.io.File;
Import java.io.IOException;
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.exception.XMLParserException;

Import Org.mybatis.generator.internal.DefaultShellCallback; public class Generatorsqlmap {public void Generator () throws exception{list<string> warnings = new A
        Rraylist<string> ();
        Boolean overwrite = true; 
        Specifies the reverse engineering profile, 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);  public static void Main (string[] args) throws Exception {try {generatorsqlmap Generatorsqlmap
            = new Generatorsqlmap ();
        Generatorsqlmap.generator ();
        } catch (Exception e) {e.printstacktrace ();
 }

    }

}

Refresh after execution

Use after Build:

You need to copy the generated code to your project. You can use it.
But it's not recommended to add anything in reverse engineering code.
Criteria is the construction of a query condition. Specifically we will know in the future works.

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.