mybatis-generator自动生成dao,mapping,model
mybatis-generator下载地址:https://github.com/mybatis/generator/releases/tag/mybatis-generator-1.3.2
下载好后,解压:
D:\tools\mybatis-generator-core-1.3.2\lib>dir 驱动器 D 中的卷没有标签。 卷的序列号是 000B-8134 D:\tools\mybatis-generator-core-1.3.2\lib 的目录 2015/10/28 14:37 <DIR> . 2015/10/28 14:37 <DIR> .. 2015/10/28 14:38 2,039 generatorConfig.xml 2012/07/16 14:53 1,696,539 mybatis-generator-core-1.3.2-javadoc.jar 2012/07/16 14:53 411,564 mybatis-generator-core-1.3.2-sources.jar 2012/07/16 14:54 516,357 mybatis-generator-core-1.3.2.jar 2014/04/09 09:17 709,922 mysql-connector-5.1.7.jar 2015/10/28 14:39 <DIR> src 5 个文件 3,336,421 字节 3 个目录 249,392,951,296 可用字节
编辑好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> <!-- 数据库驱动--> <classPathEntry location="mysql-connector-5.1.7.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/sdp_di" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.cetc.di.secretkeydb.model" targetProject="src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置--> <sqlMapGenerator targetPackage="com.cetc.di.secretkeydb.mapping" targetProject="src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.cetc.di.secretkeydb.dao" targetProject="src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成哪些表--> <table tableName="sdp_di_secretkey" domainObjectName="secretKeyDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="user" domainObjectName="UserDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
执行如下命令:
D:\tools\mybatis-generator-core-1.3.2\lib>java -jar mybatis-generator-core-1.3.2 .jar -configfile generatorConfig.xml -overwrite Table configuration with catalog null, schema null, and table user did not resol ve to any tables MyBatis Generator finished successfully, there were warnings. D:\tools\mybatis-generator-core-1.3.2\lib>dir
执行完毕后,得到如下的代码生成目录: