MyBatis Generator有很多方式:命令行、ant、java、maven、eclipse。
我采用的是命令行的方式。
文件列表:
generatorConfig.properties
# 包路径配置
model.package=com.koldoh.etmode.modules.test.entity
xml.mapper.package=mappings.modules.test
dao.package=com.koldoh.etmode.modules.test.dao
table.name=t_test
domainObjectName=MyTest
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>
<properties url="file:///D:/yodean/workspace/etmode/mybatis-generator/generatorConfig.properties" />
<classPathEntry
location="D:/yodean/workspace/etmode/mybatis-generator/mysql-connector-java-5.1.30.jar"/>
<context id="my" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/etmode?useUnicode=true" userId="root"
password="xxxxxx"/>
<javaModelGenerator targetPackage="${model.package}"
targetProject="D:/yodean/workspace/etmode/src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="${xml.mapper.package}"
targetProject="D:/yodean/workspace/etmode/src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator targetPackage="${dao.package}"
targetProject="D:/yodean/workspace/etmode/src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="${table.name}" domainObjectName="${domainObjectName}"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
generator.bat
java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite
pause
最后别忘了建表t_test.sql
DROP TABLE IF EXISTS `t_test`;
CREATE TABLE `t_test` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`user_name` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
参考资料:
http://www.mybatis.org/generator/
http://www.cnblogs.com/pixy/p/5038275.html
http://www.voidcn.com/blog/tianwei7518/article/p-6329286.html
http://bbs.520it.com/forum.php?mod=viewthread&tid=266