首先需要一个配置文件configuration.xml文件
/p>
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
connectionURL="jdbc:MysqL://localhost:3306/test?characterEncoding=utf8"
userId="root" password="password" />
targetProject="E:\xml\main">
targetProject="E:\xml\main">
targetPackage="net.okdi.api.dao" targetProject="E:\xml\main">
domainObjectName="SmsLog" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" />
domainObjectName="SmsLxhlKey" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" />
然后就是一个工具类GenerateRun.java
package com.amssy.util;
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 GenerateRun {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
generateMbgConfiguration();
}
private static void generateMbgConfiguration() {
/*
* Mybatis自带Generator工具生成相应东西
*/
List warnings = new ArrayList();
boolean overwrite = true;
// 配置文件的位�?可以为项目路径也可以为磁盘的绝对路径
File configFile = new File("./src/com/amssy/config/configuration.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
try {
config = cp.parseConfiguration(configFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XMLParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
try {
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,callback,warnings);
myBatisGenerator.generate(null);
} catch (InvalidConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (sqlException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("生成Mybatis配置成功?");
}
}
用到的jar包:
mybatis-3.2.2.jar mybatis-generator-core-1.3.2.jar mybatis-spring-1.2.2.jar MysqL-connector-java-5.1.21.jar
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250