今天处理个老项目,要用以下 MyBatis Generator,好久没搞过 MyBatis 了,把这个自动生成器 Generator 的安装使用记录下;
在 IDEA 中安装 MyBatis Generator 插件,自动根据数据表生成代码;
注:此为 MyBatis 使用,MyBatisPlus 对应的是 MyBatis Plus Generator;
IDEA 安装插件
IDEA 中:
File -- Settings
Plugins -- Marketplace 搜索 Mybatis Generator 安装插件
重启 IDEA
MybatisGenerator 配置
在项目中的 resources 目录下创建配置文件:
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="D:\soft\m2\repository\mysql\mysql-connector-java\8.0.11\mysql-connector-java-8.0.11.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://10.2.3.4:3306/databasename"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.zsoft.pojo" targetProject="D:\ideaProject\ProjectName\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="D:\ideaProject\ProjectName\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zsoft.mapper" targetProject="D:\ideaProject\ProjectName\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="t_user" domainObjectName="User" schema=""></table>
</context>
</generatorConfiguration>
注:路径要使用绝对路径,并设置好相关包名
右键点击 generatorConfig.xml 文件,选择 MybatisGenerator,完成后会有弹框 “success”
右键项目 main 目录 -- Reload from Disk
查看相应的 mapper、pojo 包和 resources/mapper 文件夹下已经自动建好了相关代码文件;