逆向工程简介
mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java、mapper.xml、pojo…),可以让程序员将更多的精力放在繁杂的业务逻辑上。
企业实际开发中,常用的逆向工程方式:由数据库的表生成java代码。
之所以强调单表两个字,是因为Mybatis逆向工程生成的Mapper所进行的操作都是针对单表的,也许你可能会觉得那这就有点鸡肋了,但是在大型项目中,很少有复杂的多表关联查询,所以作用还是很大的。
逆向工程的使用
1.首先我们创建一个springboot项目,如何搭建springboot项目可参考:https://blog.csdn.net/blueAndSmile/article/details/109593345
2.pom.xml文件添加依赖
<build>
<finalName>zsxt</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.项目结构:src---->main—>resources目录下创建generatorConfig.xml文件,如下图所示:
4.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="D:\mybatis逆向生成\mysql-connector-java-5.1.46\mysql-connector-java-5.1.46/mysql-connector-java-5.1.46.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="数据库连接地址"
userId="数据库用户名"
password="数据库密码">
<!--防止生成实体出现多余字符-->
<property name="nullCatalogMeansCurrent" value="false" />
</jdbcConnection>
<!-- targetPackage:包名称(自定义) targetProject:项目路径(自定义) -->
<!--定义model的包名称-->
<javaModelGenerator targetPackage="com.ssmblog.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="com.ssmblog.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 配置生成相应的接口类,对应与Mapper.xml中的一系列CRUD方法SQL语句 -->
<!-- targetPackage:包名称(自定义) targetProject:项目路径(自定义) -->
<javaClientGenerator targetPackage="com.ssmblog.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 需要生成的表 -->
<table schema="" tableName="sys_user" domainObjectName="sysUser"
delimitIdentifiers="true"
delimitAllColumns="true"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table schema="" tableName="sys_user_post" domainObjectName="sysUserPost"
delimitIdentifiers="true"
delimitAllColumns="true"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table schema="" tableName="sys_user_role" domainObjectName="sysUserRole"
delimitIdentifiers="true"
delimitAllColumns="true"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
5.增加配置参数
Edit configurations---------->左上角加号-------->maven------->在command line添加配置参数:mybatis-generator:generate -e ----------------->点击ok
6.运行程序
可看到左边生成对应的实体,mapper,xml文件。右边添加需要生成对应的表