MyBatis Reverse engineering can automatically generate the code required for MyBatis execution against a single table in the database. Includes entity class Po, Mapper.java interface, mapper.xml mapping file.
Steps:
Add the Generatorsqlmap.java class under SRC
ImportOrg.mybatis.generator.api.MyBatisGenerator;ImportOrg.mybatis.generator.config.Configuration;ImportOrg.mybatis.generator.config.xml.ConfigurationParser;ImportOrg.mybatis.generator.internal.DefaultShellCallback;ImportJava.io.File;ImportJava.util.ArrayList;ImportJava.util.List;/** * Created with IntelliJ idea. * User:yen * DATE:2016/8/13 * time:21:02 * * Public class generatorsqlmap { Public Static void Main(string[] args) {Try{Generatorsqlmap Generatorsqlmap =NewGeneratorsqlmap (); Generatorsqlmap.generator (); }Catch(Exception e) {E.printstacktrace (); } } Public void Generator()throwsexception{list<string> warnings =NewArraylist<string> ();BooleanOverwrite =true;//Specify 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); }}
Add under Project root: 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.org/dtd/mybatis-generator-config_1_0.dtd "><generatorconfiguration> <context id="Testtables" targetruntime="MyBatis3"> <commentgenerator> <!--whether to remove automatically 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/db_school" userId="root"password="admin"> </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.BigDe when True Cimal -- <javatyperesolver> < property name="Forcebigdecimals" value="false" /> </javatyperesolver> <!--targetproject: Generate POS Class Location-- <javamodelgenerator targetpackage= "pojo"targetproject=" . \src "> <!--enablesubpackages: Do you want the schema to be a suffix of the package-- < property name="Enablesubpackages" value="false" /> <!--The value returned from the database is cleared before and after the space- < property name="trimstrings" value="true" /> </javamodelgenerator> <!--targetproject:mapper Map file generated location-- <sqlmapgenerator targetpackage="Mapper"targetproject=". \src" > <!--enablesubpackages: Do you want the schema to be a suffix of the package-- < property name="Enablesubpackages" value="false" /> </sqlmapgenerator> <!--Targetpackage:mapper interface generated location-- <javaclientgenerator type= "xmlmapper"targetpackage="Mapper" targetproject=". \src"> <!--enablesubpackages: Do you want the schema to be a suffix of the package-- < property name="Enablesubpackages" value="false" / > </javaclientgenerator> <!--specifying database tables -- <table Schema= "" tableName= "tb_user"></table> <table Schema= "" tableName= "tb_teacher"></table> </Context></generatorconfiguration>
The main function that runs Generatorsqlmap.java generates the PO, mapper package under SRC, and contains the entity class Po, Mapper.java interface, mapper.xml mapping file.
If you want to regenerate, you should delete and rerun because the file is not overwritten but appended.
MyBatis Reverse Engineering