下载地址:
http://blog.mybatis.org/2017/12/mybatis-generator-version-136-released.html
参考了
http://blog.csdn.net/qr719169236/article/details/51086997
在使用mybatis开发的过程中,通常我们会给数据库的每张表编写对应的domain、dao、mapping,很简单,但是工作很大,所以我们通常会使用代码生成器帮我们自动生成。具体方法如下:
mybatis-generator 下载
解压后目录结构如下:
generatorConfiguration文件配置
打开doc目录中的index.html,可以发现mybatis-generator提供了5中方法供使用(本文介绍第一种,命令行方法),分别如下:
点击From the Command Line,显示must create an XML configuration file to run MBG from the command line,就是说必须要创建一个configuration 配置文件,然后执行下面的命令:
java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml -overwrite java -cp mybatis-generator-core-x.x.x.jar org.mybatis.generator.api.ShellRunner -configfile generatorConfig.xml java -cp mybatis-generator-core-x.x.x.jar org.mybatis.generator.api.ShellRunner -configfile generatorConfig.xml -overwrite
在lib目录下新建一个generatorConfig.xml文件,点击XML Configuration Reference 将里面的代码拷贝到 generatorConfig.xml 中
修改generatorConfig.xml文件。在上图中的代码中,classPathEntry为数据库连接驱动,多以将对应的jar文件放入lib文件中,location指定其路径。实例如下:
- <?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>
- <!--MySQL连接驱动-->
- <classPathEntry location="mysql-connector-java-5.1.9.jar" />
- <!--数据库链接URL,用户名、密码 -->
- <context id="MySQL" targetRuntime="MyBatis3">
- <commentGenerator>
- <property name="suppressDate" value="true"/>
- <!-- 是否去除自动生成的注释 true:是 : false:否 -->
- <property name="suppressAllComments" value="true"/>
- </commentGenerator>
- <jdbcConnection driverClass="com.mysql.jdbc.Driver"
- connectionURL="jdbc:mysql://127.0.0.1/test"
- userId="root"
- password="123456">
- </jdbcConnection>
- <!--是否启用java.math.BigDecimal-->
- <javaTypeResolver >
- <property name="forceBigDecimals" value="false" />
- </javaTypeResolver>
- <javaModelGenerator targetPackage="test.model" targetProject="src">
- <property name="enableSubPackages" value="true" />
- <property name="trimStrings" value="true" />
- </javaModelGenerator>
- <sqlMapGenerator targetPackage="test.xml" targetProject="src">
- <property name="enableSubPackages" value="true" />
- </sqlMapGenerator>
- <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="src">
- <property name="enableSubPackages" value="true" />
- </javaClientGenerator>
- <table tableName="salary" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
- </table>
- <table tableName="persons" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
- </table>
- <table tableName="orders" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
- </table>
- </context>
- </generatorConfiguration>
<?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>
<!--MySQL连接驱动-->
<classPathEntry location="mysql-connector-java-5.1.9.jar" />
<!--数据库链接URL,用户名、密码 -->
<context id="MySQL" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1/test"
userId="root"
password="123456">
</jdbcConnection>
<!--是否启用java.math.BigDecimal-->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="test.model" targetProject="src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="test.xml" targetProject="src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="salary" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="persons" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="orders" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
Running MyBatis Generator
执行命令前,在lib目录下先创建 配置文件中targetProject指定的文件夹。
执行命令:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
结果如下: