Recent projects to use the MyBatis middleware, the middle involves the table structure to generate Bean,dao, and sqlconfig.xml so record the learning process
The first is the preparation of the required jar package: Our database MySQL, so the driver Mysql-connector-5.1.8.jar also needs MyBatis jar package: Mybatis-3.0.5-snapshot.jar
and auto-generated jar package: Mybatis-generator-core-1.3.2.jar .... These can be downloaded to the internet ...
I first tested this auto-build feature, so I did not configure it directly in the project, but first tested it on the D drive: put three jar packages into D:\test\lib
The second is to prepare the generatorconfig.xml, and put this file directly under D:\test. Generatorconfig.xml files such as:
<?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= "Lib/mysql-connector-5.1.8.jar"/>
<context id= "Mysqltables" targetruntime= "MyBatis3" >
<!--configuration database link--
<jdbcconnection driverclass= "Com.mysql.jdbc.Driver"
Connectionurl= "Jdbc:mysql://localhost:3306/abcproject?characterencoding=utf8" userId= "root"
password= "11112222" >
</jdbcConnection>
<javaTypeResolver>
<property name= "Forcebigdecimals" value= "false"/>
</javaTypeResolver>
<!--configuring entity bean-->
<javamodelgenerator targetpackage= "Xmlparer"
targetproject= "D:\test\xmlParer" >
<property name= "Enablesubpackages" value= "true"/>
<property name= "Trimstrings" value= "true"/>
</javaModelGenerator>
<!--Configure Entity Map Interface (DAO)--
<sqlmapgenerator targetpackage= "Xmlparer"
targetproject= "D:\test\xmlParer" >
<property name= "Enablesubpackages" value= "true"/>
</sqlMapGenerator>
<!--configuring sql.xml--> for Entity Beans
<javaclientgenerator type= "Xmlmapper"
Targetpackage= "Xmlparer"
targetproject= "D:\test\xmlParer" >
<property name= "Enablesubpackages" value= "true"/>
</javaClientGenerator>
<!--configuring Entities--
<table tablename= "abc_agency" domainobjectname= "agency" >
</table>
</context>
</generatorConfiguration>
Because it's a simple test, my bean and DAO and XML are all in a unified directory ... D:\test\xmlParer
Finally executed on the cmd command line
Switch directory to D:\test\lib
Java-jar mybatis-generator-core-1.3. 0.jar-configfile D:\test\generatorconfig.xml-overwrite
This is basically OK ... Later, I also studied the main method of Java to execute ... Also OK
2012-09-19
Today the last Main method read configuration automatically generated beans, and map code copied for your reference
Package net.xh.xuanzhicms.util;
Import Java.io.File;
Import java.io.IOException;
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.XMLParserException;
Import Org.mybatis.generator.internal.DefaultShellCallback;
public class Automybatis {
/**
* @param args
*/
public static void Main (string[] args) {
list<string> warnings = new arraylist<string> ();
Boolean overwrite = true;
File ConfigFile = new file ("Src/generatorconfig.xml");
Configurationparser cp = new Configurationparser (warnings);
Configuration config;
try {
Config = cp.parseconfiguration (configfile);
Defaultshellcallback callback = new Defaultshellcallback (overwrite);
Mybatisgenerator Mybatisgenerator;
try {
Mybatisgenerator = new Mybatisgenerator (config, callback,
warnings);
Mybatisgenerator.generate (NULL);
} catch (Exception e) {
E.printstacktrace ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Xmlparserexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
This makes it easier to generate code automatically.
[Go] Mybatis how to automatically generate a bean DAO XML configuration File Generatorconfig.xml (main () method automatically generated faster)