代码自动生成,这里的代码自动生成包括,与数据库一一对应的实体对象entity,Mapper基本结构文件,与Mapper对应的dao基本接口文件,三种执行方法包括 :
1.cmd命令执行配置文件,2通过Main方法执行配置文件,3通过Maven插件运行。
在自己放配置文件的目录下新建generatorConfig.xml,与我的目录不冲突,放哪里都可以。
generatorConfig.xml:
<generatorConfiguration>
<classPathEntry location="E:/maven/repository/mysql/mysql-connector-java/5.1.30/mysql-connector-java-5.1.30.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/huahua" userId="root" password="root">
jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
javaTypeResolver>
<javaModelGenerator targetPackage="com.entity" targetProject="E:\workspace\maven_test\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
javaModelGenerator>
<sqlMapGenerator targetPackage="com.mapping" targetProject="E:\workspace\maven_test\src\main\java">
<property name="enableSubPackages" value="true" />
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.dao" targetProject="E:\workspace\maven_test\src\main\java">
<property name="enableSubPackages" value="true" />
javaClientGenerator>
<table tableName="test" domainObjectName="Test" enableCountByExample="true" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">table>
context>
generatorConfiguration>
2 . 根据上图配置好后,下载下载mybatis-generator-core,用于cmd启动xml文件。
下载地址,我用的1.3.2版本:
http://download.csdn.net/download/qq_22860341/9849578
将下载好的mybatis-generator-core-1.3.2放在与generatorConfig.xml同一目录下,如上图所示我放在项目上的位置和下图所示根目录路径:
3.配置和启动就需要一个如上所示的xml文件和jar即可。
打开cmd,进入上图路径下的lib目录,复制脚本:Java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/huahua" userId="root" password="root">
jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
javaTypeResolver>
<javaModelGenerator targetPackage="com.entity" targetProject="E:\workspace\maven_test\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
javaModelGenerator>
<sqlMapGenerator targetPackage="com.mapping" targetProject="E:\workspace\maven_test\src\main\java">
<property name="enableSubPackages" value="true" />
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.dao" targetProject="E:\workspace\maven_test\src\main\java">
<property name="enableSubPackages" value="true" />
javaClientGenerator>
<table tableName="test" domainObjectName="Test" enableCountByExample="true" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">table>
context>
generatorConfiguration>
2.java.main方法执行代码:
package com.text;
import java.io.*;
import java.util.*;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
//以上import需要 mybatis-generator-core-1.3.2.jar
public class Text {
public static void main(String[] args) {
try {
List warnings = new ArrayList();
boolean overwrite = true;
String genCfg = "/generatorConfig.xml";
File configFile = new File(Text.class.getResource(genCfg).getFile());
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
修改pom.xml文件,在project标签里面加入如下:
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>1.3.2version>
<configuration>
<verbose>trueverbose>
<overwrite>trueoverwrite>
configuration>
plugin>
plugins>
build>
2.generatorConfig.xml中如下,有这行:
<generatorConfiguration>
<classPathEntry location="E:/maven/repository/mysql/mysql-connector-java/5.1.30/mysql-connector-java-5.1.30.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/huahua" userId="root" password="root">
jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
javaTypeResolver>
<javaModelGenerator targetPackage="com.entity" targetProject="E:\workspace\maven_test\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
javaModelGenerator>
<sqlMapGenerator targetPackage="com.mapping" targetProject="E:\workspace\maven_test\src\main\java">
<property name="enableSubPackages" value="true" />
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.dao" targetProject="E:\workspace\maven_test\src\main\java">
<property name="enableSubPackages" value="true" />
javaClientGenerator>
<table tableName="test" domainObjectName="Test" enableCountByExample="true" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">table>
context>
generatorConfiguration>
3,运行方法:在eclipse 中,选择pom.xml文件,击右键先择Run AS——>Maven Build… ——>在Goals框中输入:mybatis-generator:generate