之前生成mybatis对应的entity、dao、dao.xml啊都是自己写了一套gui工具生成的。感觉还挺好用的。就是通用性不是很强 = =!
今儿偶尔发现原来官方有生成工具,还挺强大的(可惜没有gui啊)。果断试用下(只试验在maven下的使用)。
ps:参考的官网
操作步骤
在pom.xml中添加MBG的插件配置
<!--mybatis-generator-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<!--配置文件路径-->
<configurationFile>${basedir}/src/main/resources/mybatis/generatorConfig.xml</configurationFile>
<!--打印日志-->
<verbose>true</verbose>
<!--覆盖存在的文件-->
<overwrite>true</overwrite>
</configuration>
</plugin>
添加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>
<!-- Class Driver Path -->
<classPathEntry
location="/xxx/mysql-connector-java-5.1.35.jar"/>
<context id="context" targetRuntime="MyBatis3">
<commentGenerator>
<!-- This property is used to specify whether MBG will include any coments in the generated code -->
<property name="suppressAllComments" value="false"/>
<!-- This property is used to specify whether MBG will include the generation timestamp in the generated comments -->
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8"
userId="test" password="123456"/>
<javaTypeResolver>
<!-- This property is used to specify whether MyBatis Generator should force the use of java.math.BigDecimal
for DECIMAL and NUMERIC fields, rather than substituting integral types when possible -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.xkeshi.entities" targetProject="src/main/java">
<!-- This property is used to select whether MyBatis Generator will generate different Java packages for
the objects based on the catalog and schema of the introspected table -->
<property name="enableSubPackages" value="false"/>
<!-- This property is used to select whether MyBatis Generator adds code to trim the white space from character fields returned from the database -->
<property name="trimStrings" value="false"/>
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.xkeshi.dao" targetProject="src/main/resources">
<!-- This property is used to select whether MyBatis Generator will generate different Java packages for
the objects based on the catalog and schema of the introspected table -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.xkeshi.dao" targetProject="src/main/java" type="XMLMAPPER">
<!-- This property is used to select whether MyBatis Generator will generate different Java packages for
the objects based on the catalog and schema of the introspected table -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<table tableName="account" enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false">
<!--insert时id设置-->
<generatedKey column="id" sqlStatement="MySql" identity="true" type="pre"/>
</table>
</context>
</generatorConfiguration>
输入命令运行
|
|
这里-e参数是为了输出错误信息,方便排查问题。
看到“BUILD SUCCESS”表示成功生成了~