Mybatis逆向生成工具
所谓mybatis逆向工程,就是mybatis会根据我们设计好的数据表,自动生成pojo、mapper以及mapper.xml。
首先看下生成之后的工程目录,如下图:
接下来开始使用
工程实例
1.首先在项目的pom.xml文件中加入一下插件
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
2.在模块的根目录加入 GeneratorMapper.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>
<!-- 指定连接数据库的JDBC驱动包所在位置,指定到你本机的完整路径,需确保本地路径下存在该jar -->
<classPathEntry location="F:\InstallationPackage\apache-maven-3.5\LocalRepository\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar"/>
<!-- 配置table表信息内容体,targetRuntime指定采用MyBatis3的版本 -->
<context id="tables" targetRuntime="MyBatis3">
<!--序列化-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!--以下需要插件 -->
<!--
插入成功后返回ID
<plugin type="cn.doity.common.generator.plugin.InsertAndReturnKeyPlugin"/>
分页查询功能
<plugin type="cn.doity.common.generator.plugin.SelectByPagePlugin"/>
生成带有for update后缀的select语句插件
<plugin type="cn.doity.common.generator.plugin.SelectForUpdatePlugin"/> -->
<!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/eblog"
userId="root"
password="root">
</jdbcConnection>
<!-- 生成pojo类,targetPackage指定pojo类的包名, targetProject指定生成的model放在eclipse的哪个工程下面-->
<javaModelGenerator targetPackage="com.jia.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="false" />
</javaModelGenerator>
<!-- 生成MyBatis的Mapper.xml文件,targetPackage指定mapper.xml文件的包名, targetProject指定生成的mapper.xml放在eclipse的哪个工程下面 -->
<sqlMapGenerator targetPackage="/mappers" targetProject="src/main/resources">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成MyBatis的Mapper接口类文件,targetPackage指定Mapper接口类的包名, targetProject指定生成的Mapper接口放在eclipse的哪个工程下面 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.jia.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 数据库表名及对应的Java模型类名 -->
<table tableName="m_user"
domainObjectName="User"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="m_user_action"
domainObjectName="UserAction"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="m_user_collection"
domainObjectName="UserCollection"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="m_user_message"
domainObjectName="UserMessage"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="m_post"
domainObjectName="Post"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="m_comment"
domainObjectName="Comment"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<table tableName="m_category"
domainObjectName="Category"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
最后一步使用Maven插件
显示出 BUILD SUCCESS
则成功
评论区