Work knowledge points, mybaties reverse generation of Pojo and Mapper

Source: Internet
Author: User

Save time writing XML files such as JavaBean and Mapper. This requires writing the Java method and the corresponding XML, and then running the method to get the desired result.

Create a Web project to add the jar packages required for reverse engineering in the LIB package of the Web project

Create a Generatorconfig.xml file

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE generatorconfigurationpublic "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en" "/HTTP/ Mybatis.org/dtd/mybatis-generator-config_1_0.dtd "><generatorconfiguration>    <ContextID= "Testtables"Targetruntime= "MyBatis3">        <Commentgenerator>            <!--whether to remove automatically generated comments true: Yes: false: No -            < Propertyname= "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= "1234" > </jdbcConnection> -        <jdbcconnectionDriverclass= "Oracle.jdbc.driver.OracleDriver"Connectionurl= "JDBC:ORACLE:THIN:@127.0.0.1:1521:FASPGPORCL"userId= "Cs_qinghai"Password= "1">        </jdbcconnection>        <!--by default, the JDBC decimal and NUMERIC types are parsed to Integer, and the JDBC decimal and NUMERIC types are resolved to Java.math.BigDecimal if True 
     -        <Javatyperesolver>            < Propertyname= "Forcebigdecimals"value= "false" />        </Javatyperesolver>        <!--Targetproject: Where to generate the PO class -        <JavamodelgeneratorTargetpackage= "Cn.edu.hpu.ssm.po"Targetproject= ". \src">            <!--enablesubpackages: Do you want the schema to be the suffix of the package -            < Propertyname= "Enablesubpackages"value= "false" />            <!--The space returned from the database before and after the value is cleaned -            < Propertyname= "Trimstrings"value= "true" />        </Javamodelgenerator>        <!--Targetproject:mapper The location of the map file generation -        <SqlmapgeneratorTargetpackage= "Cn.edu.hpu.ssm.mapper"Targetproject= ". \src">            <!--enablesubpackages: Do you want the schema to be the suffix of the package -            < Propertyname= "Enablesubpackages"value= "false" />        </Sqlmapgenerator>        <!--Targetpackage:mapper the location generated by the interface -        <Javaclientgeneratortype= "Xmlmapper"Targetpackage= "Cn.edu.hpu.ssm.mapper"Targetproject= ". \src">            <!--enablesubpackages: Do you want the schema to be the suffix of the package -            < Propertyname= "Enablesubpackages"value= "false" />        </Javaclientgenerator>        <!--Specify database Tables -        <TableTableName= "Gp_contract_pay"></Table>        <TableTableName= "Gp_contract"></Table>        <!--<table schema= "" Tablename= "Sys_user" ></table> <table schema= "" Tablename= "Sys_role" ></ table> <table schema= "" Tablename= "Sys_permission" ></table> <table schema= "" Tablename= "Sys_ User_role "></table> <table schema=" "Tablename=" Sys_role_permission "></table> -        <!--some table fields need to specify the Java type <table schema= "" Tablename= "" > <columnoverride column= "" javatype= ""/> < /table> -    </Context></generatorconfiguration>

Write the Java method to read the Generatorconfig.xml file and put the XML file directly into the root directory

Write Java class

 Packagetest;ImportJava.io.File;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.mybatis.generator.api.MyBatisGenerator;Importorg.mybatis.generator.config.Configuration;ImportOrg.mybatis.generator.config.xml.ConfigurationParser;ImportOrg.mybatis.generator.internal.DefaultShellCallback; Public classGeneratorsqlmap { Public voidGenerator ()throwsException {List<String> warnings =NewArraylist<string>(); BooleanOverwrite =true; //Specifying a reverse engineering configuration fileFile 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); }     Public Static voidMain (string[] args)throwsException {Try{generatorsqlmap Generatorsqlmap=NewGeneratorsqlmap ();        Generatorsqlmap.generator (); } Catch(Exception e) {e.printstacktrace (); }    }}

Run the main method in Java, wait for a while, then refresh the project and you will see the resulting file

Stick the required files in your project.

Work knowledge points, mybaties reverse generation of Pojo and Mapper

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.