什么是逆向工程
代码都放出来了
Mybatis需要程序员自己编写SQL语句,mybatis官网提供逆向工程,可以针对单表自动生成mybatis执行所有需要代码(mapper.java,mapper.xml,po)
企业实际开发中,常用的逆向工程方式:
数据库表来生产Java代码,
企业开发中,是先对数据库进行设计,也就是编码之前数据库已经有了。
逆向工程演示
1新建一个maven工程
2配置pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sgl</groupId>
<artifactId>Generator</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
</dependencies>
<build>
<plugins>
<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>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
3配置resource文件加下的generatorConfig.xml
1)配置mysql 驱动jar包路径.用了绝对路径
路径在你的本地仓库 的jar包
mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar
知道本地仓库就能找到 版本可以不用选5.1.38
2)数据库连接
3)数据表对应的model层
4)sql mapper 映射配置文件
5)mybatis3中的mapper接口
6)数据表进行生成操作 schema:相当于库名; tableName:表名;
domainObjectName:对应的DO
<?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=
"G:\0-maven\repository-BD\repository\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)数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis01?characterEncoding=UTF-8"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- (3)数据表对应的model层 -->
<javaModelGenerator targetPackage="com.hd.po"
targetProject="src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- (4)sql mapper 映射配置文件 -->
<sqlMapGenerator targetPackage="com.hd.mapper"
targetProject="src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- (5)mybatis3中的mapper接口 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.hd.mapper"
targetProject="src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- (6)数据表进行生成操作 schema:相当于库名; tableName:表名;
domainObjectName:对应的DO
-->
<table schema="mybatis" tableName="user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table schema="mybatis" tableName="orders" domainObjectName="Orders"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
4使用maven插件