mybatis-generator自动生成代码(二)
程序员文章站
2022-05-29 17:04:28
...
借助maven可以让我们更加便捷来生成代码,下面讲解一下简单操作
第一步在Eclipse中创建一个maven项目;
第二步要配置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="E:/test/mysql-connector-java-5.1.40-bin.jar" /> <context id="MysqlTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/lian?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false" userId="root" password="root"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="org.lian.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="org.lian.xml" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="org.lian.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="t_test" domainObjectName="Tester" > <property name="useActualColumnNames" value="true"/> <generatedKey column="ID" sqlStatement="MySql" identity="true" /> <!-- <columnOverride column="DATE_FIELD" property="startDate" /> <ignoreColumn column="FRED" /> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> --> </table> </context> </generatorConfiguration>
注意:配置文件要放到maven项目的resource目录下
第四步maven项目的pom.xml配置添加mybatis-generator-plugin
<build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> </plugin> </plugins> </build>
最后执行maven的构建命令
mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate
如果使用eclipse可以采用如下 maven build来创建