什么是逆向工程?
Mybatis需要程序员自己编写SQL语句,mybatis官方提供逆向工程,可以针对单表自动生成Mybatis执行所需要的代码(Mapper接口、Mapper.xml,po)
在企业开发中,常用的逆向工程方式:
由数据库的表生成Java代码,
开发过程,先设计的(表和字段已经有了)
逆向工程的使用
导包
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
建好数据表对应的model层:bean
映射文件,mapper
逆向工程的配置
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>
<!--1.配置mysql驱动jar包路径,用了绝对路径-->
<classPathEntry location="E:\MavenRepository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar"/>
<context id="scm_mysql_tables" targetRuntime="MyBatis3">
<!-- 防止生成的代码中有很多注释,加入下面的配置控制 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<!--2.数据库连接 characterEncoding=UTF-8-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?useSSL=false"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--3.数据表对应的model层-->
<javaModelGenerator targetPackage="com.hd.bean" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
<property name="constructorBased" value="true"/>
</javaModelGenerator>
<!--4.sql mapper 映射配置文件-->
<sqlMapGenerator targetPackage="com.hd.mapper"
targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--5.mybatis中的mapper接口-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.hd.mapper"
targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 6.数据表进行生成操作 schema:相当于库名; tableName:表名;
domainObjectName:对应的DO
-->
<table schema="test" tableName="user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
配置好文件,就可以双击箭头指向生成代码了
运行成功