1、创建Maven项目
2、在pom.xml中配置MyBatis逆向工程插件
<build>
<finalName>mybatis-generator-maven</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
3、配置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 >
<!-- mysql jar 文件位置 -->
<classPathEntry location="/Point9/MyMaven/maven-repository/mysql/mysql-connector-java/5.1.43/mysql-connector-java-5.1.43.jar" />
<context id="store" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
<!-- 是否去除所有自动生成的文件的时间戳,默认为false -->
<property name="suppressDate" value="true"/>
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/myshop"
userId="root"
password="root">
</jdbcConnection>
<!-- targetPackage:包名称(自定义) targetProject:项目路径(自定义) -->
<!--定义model的包名称-->
<javaModelGenerator targetPackage="top.point9.shop.v1.entity" targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 配置生成相应的实体Mapper.xml,对于Mapper3.X我们需要把type="XMLMAPPER" -->
<!-- targetPackage:包名称(自定义) targetProject:项目路径(自定义) -->
<sqlMapGenerator targetPackage="top.point9.shop.v1.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 配置生成相应的接口类,对应与Mapper.xml中的一系列CRUD方法SQL语句 -->
<!-- targetPackage:包名称(自定义) targetProject:项目路径(自定义) -->
<javaClientGenerator targetPackage="top.point9.shop.v1.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 商品表 -->
<table schema="" tableName="t_product" domainObjectName="Product"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<!-- 商品详情表 -->
<table schema="" tableName="t_product_desc" domainObjectName="ProductDesc"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<!-- 商品类别表 -->
<table schema="" tableName="t_product_type" domainObjectName="ProductType"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
4、在Intellij IDEA添加一个“Run运行”选项,使用maven运行mybatis-generator-maven-plugin插件
mybatis-generator:generate -e
运行结果如下:
原文(含源码下载)地址:http://www.point9.top/index.php/2019/03/09
点九博客 www.point9.top
点九论坛 bbs.point9.top
点九邮箱 [email protected]
Web全栈技术交流
点击链接加入群聊【Web全栈交流群】:https://jq.qq.com/?_wv=1027&k=5rnUzsF