【Mybatis】使用Mybatis-Generator自动生成entity、dao、mapping

时间:2023-03-08 19:32:20

使用过mybatis的应该都有用过Mybatis-Generator,本文主要介绍使用Mybatis-Generator来自动生成entity、dao、mapping文件。

Mybatis-Generator的github下载地址:https://github.com/mybatis/generator/releases

下面是本人下载后文件整理了下:

【Mybatis】使用Mybatis-Generator自动生成entity、dao、mapping

这里根据自已的数据库连接信息及表等修改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-java-5.1.25-bin.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://127.0.0.1:3306/test001" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver> <!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.hin.entity" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator> <!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="com.hin.mapping" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--
<sqlMapGenerator targetPackage="com.hin.mapping" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
--> <!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.hin.dao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator> <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="part" domainObjectName="Part" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>

要修改处根据注释修改即可,这里的相关路径(如数据库驱动包,生成对应的相关文件位置可以自定义)不能带有中文。上面配置文件中的table部分:

<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

tableName和domainObjectName为必选项,分别代表数据库表名和生成的实力类名,其余的可以自定义去选择,一般情况下均为false。

接着在该 目录下,打开命令行窗口,执行如下命令即可:

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

如下执行结果:

【Mybatis】使用Mybatis-Generator自动生成entity、dao、mapping

生成文件:

【Mybatis】使用Mybatis-Generator自动生成entity、dao、mapping

上述整理后的Mybatis-Generator下载地址:Mybatis-Generator