1. Java Project directory structure:
|src/main/java
|com.leslie.mybatis.generator
| Mybatisgeneratorutil.java
|src/main/resources
Generatorconfig.xml
|lib
|mybatis-3.3.0.jar
|mybatis-generator-core-1.3.2.jar
|mysql-connector-java-5.1.14-bin.jar
2, Mybatisgeneratorutil.java
PackageCom.leslie.mybatis.generator;ImportJava.io.File;Importjava.io.IOException;Importjava.sql.SQLException;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.exception.InvalidConfigurationException;Importorg.mybatis.generator.exception.XMLParserException;ImportOrg.mybatis.generator.internal.DefaultShellCallback; Public classMybatisgeneratorutil { Public Static voidMain (string[] args) {Try{System.out.println ("Start generator ..."); List<String> warnings =NewArraylist<string>(); BooleanOverwrite =true; File ConfigFile=NewFile (Mybatisgeneratorutil.class. GetResource ("/generatorconfig.xml"). GetFile ()); Configurationparser CP=Newconfigurationparser (warnings); Configuration Config=cp.parseconfiguration (configfile); Defaultshellcallback Callback=Newdefaultshellcallback (overwrite); Mybatisgenerator Mybatisgenerator=Newmybatisgenerator (config, callback, warnings); Mybatisgenerator.generate (NULL); System.out.println ("End generator!"); } Catch(IOException e) {e.printstacktrace (); } Catch(xmlparserexception e) {e.printstacktrace (); } Catch(invalidconfigurationexception e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); } Catch(interruptedexception e) {e.printstacktrace (); } }}
3, 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> <Classpathentry Location= "D:\svn\MyBatisGenerator\lib\mysql-connector-java-5.1.14-bin.jar" /> <ContextID= "Db2tables"Targetruntime= "MyBatis3"> <Commentgenerator> < Propertyname= "Suppressdate"value= "true"/> < Propertyname= "Suppressallcomments"value= "true"/> </Commentgenerator> <jdbcconnectionDriverclass= "Com.mysql.jdbc.Driver"Connectionurl= "Jdbc:mysql://192.168.0.39:3306/leslie?useunicode=true&characterencoding=utf-8"userId= "Leslie"Password= "123456" > </jdbcconnection> <Javatyperesolver> < Propertyname= "Forcebigdecimals"value= "false"/> </Javatyperesolver> <JavamodelgeneratorTargetpackage= "Com.wsj.ygbh.model"Targetproject= "src"> < Propertyname= "Enablesubpackages"value= "true"/> < Propertyname= "Trimstrings"value= "true"/> </Javamodelgenerator> <SqlmapgeneratorTargetpackage= "Com.wsj.ygbh.mapping"Targetproject= "src"> < Propertyname= "Enablesubpackages"value= "true"/> </Sqlmapgenerator> <Javaclientgeneratortype= "Xmlmapper"Targetpackage= "Com.wsj.ygbh.dao"Targetproject= "src"> < Propertyname= "Enablesubpackages"value= "true"/> </Javaclientgenerator> <TableTableName= "Leslie_ad"Domainobjectname= "Ad"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false"></Table> <TableTableName= "Leslie_ad_position"Domainobjectname= "Adposition"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false"></Table> </Context></generatorconfiguration>
Start MyBatis generator with Java