1. First put the top 4 packages under the Lib folder
2. Configure XML, placed in the SRC directory
<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE generatorconfiguration Public "-//mybatis.org//dtd mybatis generator Configuration" "1.0//en
G/dtd/mybatis-generator-config_1_0.dtd "> <generatorConfiguration> <!--Configure the Oracle driver jar package path. Using a relative path--> <classpathentry location= "Webroot/web-inf/lib/ojdbc7.jar"/> <context id= "Yihaomen_mysql_tables" Targetruntime= "MyBatis3" > <!--to prevent a lot of comments in the generated code, it's ugly to add the following configuration control--> <commentGenerator> <p Roperty name= "Suppressallcomments" value= "true"/> <property "name=" Suppressdate "true" value=/> ommentgenerator> <!--Note Control--> <!--database connection,--> <jdbcconnection driverclass= "Oracle.jdbc . Driver. Oracledriver "connectionurl=" jdbc:oracle:thin:@152.19.5.181:1521/gldbpdb1 "userid=" Mybatis "Passwo" rd= "Mylove" > </jdbcConnection> <javatyperesolver > <property name= "Forcebigdecimals "value=" false "/> </javaTypeResolver> <!--data table corresponding to the model layer, Targetproject for the automatically generated file Put the path--> <javamodelgenerator targetpackage= "src. Model "targetproject=" src "> <property name=" enablesubpackages "value=" false "/> <property" tr Imstrings "value=" true "/> </javaModelGenerator> <!--SQL Mapper insinuate configuration file--> Rator targetpackage= "src.
Map "targetproject=" src "> <property name=" enablesubpackages "value=" false "/> </sqlMapGenerator> <!--is a DAO layer in Ibatis2, but in Mybatis3, it is mapper interface--> <javaclientgenerator type= "Xmlmapper" Targetpacka Ge= "src. Client "targetproject=" src "> <property name=" enablesubpackages "value=" false "/> </javaclientgenera Tor> <!--You must have one for those data tables to be generated. --> <table schema= "MyBatis tablename=" category "Domainobjectname=" category "Enablecountbyexample=" Fals E "EnableUpdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" Selectbyexamplequeryid = "false" > </table> </context> </generatorConfiguration>
3. Write Execution code
Package com.yihaomen.test;
Import Java.io.File;
Import java.io.IOException;
Import java.sql.SQLException;
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.InvalidConfigurationException;
Import org.mybatis.generator.exception.XMLParserException;
Import Org.mybatis.generator.internal.DefaultShellCallback; public class Genmain {public static void main (string[] args) {list<string> warnings = new ARRAYLIST<STRING&G
t; ();
Boolean overwrite = true;
String gencfg = "/mbgconfiguration.xml";
File ConfigFile = new file (GenMain.class.getResource (gencfg). GetFile ());
Configurationparser cp = new Configurationparser (warnings);
Configuration config = null;
try {config = cp.parseconfiguration (configfile);
catch (IOException e) {e.printstacktrace (); catch (XMLParserexception e) {e.printstacktrace ();
} Defaultshellcallback callback = new Defaultshellcallback (overwrite);
Mybatisgenerator mybatisgenerator = null;
try {mybatisgenerator = new Mybatisgenerator (config, callback, warnings);
catch (Invalidconfigurationexception e) {e.printstacktrace ();
try {mybatisgenerator.generate (null);
catch (SQLException e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
catch (Interruptedexception e) {e.printstacktrace ();
}
}
}
4. Execute the main method to automatically generate the required files