MyBatis Reverse Engineering

Source: Internet
Author: User

One of the main features of MyBatis is the need to write their own SQL, but if there are many tables, it will inevitably be very troublesome, so MyBatis officially provides a reverse engineering, can be automatically generated for a single table mybatis execution of the required code (including Mapper.xml, Mapper.java, PO), generally in development, the usual reverse engineering method is through the database table survival code.

Create a new Java project that is designed to use reverse engineering to generate code that is designed to prevent the generation of identical files from overwriting the original file, so it is not cumbersome and safe to build the project separately and then generate the files and copy them to your project.

The project directory is as follows:

Reverse engineering The name of the configuration file can be random, as long as placed in the root directory of the project, in the main program to specify the file name import, the contents of the configuration file are as follows:

<?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 auto-generated comments 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/ego "userid=" root "password=" root "> </jdbcConnection> <!--<jdbccon             Nection driverclass= "Oracle.jdbc.OracleDriver" connectionurl= "JDBC:ORACLE:THIN:@127.0.0.1:1521:YYCG" Userid= "YYCG" password= "YYCG" > </jdbcConnection>-<!--default false, the JDBC DECimal and NUMERIC types resolve to Integer, true when JDBC DECIMAL and NUMERIC types are parsed to Java.math.BigDecimal--and <javat        yperesolver> <property name= "Forcebigdecimals" value= "false"/> </javaTypeResolver> <!--targetproject: Generate PO Class location--<javamodelgenerator targetpackage= "Com.fd.mybatis.po" Targetp Roject= ". \src" > <!--enablesubpackages: Do you want the schema to be the suffix of the package--<property name= "Enablesubpack Ages "value=" false "/> <!--the value returned from the database is cleared before and after the space--<property name=" trimstrings "Value=" Tru  E "/> </javaModelGenerator> <!--targetproject:mapper Map file generated location--<sqlmapgenerator Targetpackage= "Com.fd.mybatis.mapper" targetproject= ". \src" > <!--enablesubpackages: let Schem        A as a suffix of the package--<property name= "Enablesubpackages" value= "false"/> </sqlMapGenerator> <!--targetpackage:mApper interface generated Location--<javaclientgenerator type= "Xmlmapper" targetpackage= "Com.fd.mybatis.mapper" Targetproject= ". \src" > <!--enablesubpackages: Do you want the schema to be the suffix of the package--<property name= "Enablesubpackages" value= "false"/> </javaClientGenerator> <!--Specify database table--<table Schema= "" Tablename= "Tb_content" ></table> <table schema= "" Tablename= "Tb_content_category" ></tab le> <table schema= "" Tablename= "Tb_item" ></table> <table schema= "" Tablename= "Tb_item_cat" ></table> <table schema= "" Tablename= "Tb_item_desc" ></table> <table schema= "" Tablen Ame= "Tb_item_param" ></table> <table schema= "" Tablename= "Tb_item_param_item" ></table> & Lt;table schema= "" Tablename= "Tb_order" ></table> <table schema= "" Tablename= "Tb_order_item" ></tab Le> <table SChema= "Tablename=" tb_order_shipping "></table> <table schema=" "Tablename=" Tb_user "></table> <!--some table fields need to specify the Java type <table schema= "" Tablename= "" > <columnoverride column= "java Type= ""/> </table> </context></generatorConfiguration>

The main contents of the configuration file are:

1. Connect to the database

2. Specify where you want to generate the code

3. Specify which tables in the database you want to generate

The main code is as follows:

Package Com.fd.mybatis.generatorsqlmap;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 ;p Ublic class Generatorsqlmap {public void Generator () throws Exception {list<string> warnings = new Arraylist<st Ring> (); Boolean overwrite = true;//Specifies the reverse engineering configuration file//inputstream input = ("Generatorconfig.xml"); File ConfigFile = new file ("Generatorconfig.xml"); Configurationparser cp = new Configurationparser (warnings); Configuration config = cp.parseconfiguration (configfile);D Efaultshellcallback callback = new Defaultshellcallback ( overwrite); Mybatisgenerator mybatisgenerator = new Mybatisgenerator (config, callback, warnings); Mybatisgenerator.generate (NULL) ;} public static void Main (string[] args) {try {generatorsqlmap generatorsqlmap = new Generatorsqlmap (); generAtorsqlmap.generator ();} catch (Exception e) {e.printstacktrace ();}}}

  

SOURCE Link Https://pan.baidu.com/s/1DzJJ5wqEx8AGmEAjYPeyjQ

MyBatis 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.