作⽤:反向⽣成实体类,接⼝,mapper.xml
步骤:
1.添加依赖包:
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
2.加载插件:(就在pom.xml 上面的依赖包下面)
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--配置文件的路径 (!!!!!!此处要修改成你放置配置生成文件的路径,一般就是下面这个路径) -->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
</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>
<!--数据库驱动jar --> <!-- (!!!!!!此处要修 jar 包路径,改成了本地仓库的路径) -->
<classPathEntry
location="E:\Maven\maven_repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar" />
<context id="MyBatis" targetRuntime="MyBatis3">
<!--去除注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接 --> <!-- (!!!!!!此处要修数据库路径,用户名,密码等) -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mytest"
userId="root"
password="123465">
</jdbcConnection>
<!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建
使用Maven生成在target目录下,会自动创建) -->
<!-- (!!!!!!此处要改!!!!!!) -->
<javaModelGenerator targetPackage="com.yuyu.bean"
targetProject="D:\mybatis3\src\main\java">
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成SQLmapper文件 --> <!-- (!!!!!!此处要改!!!!!!) -->
<sqlMapGenerator targetPackage="mapper"
targetProject="D:\mybatis3\src\main\resources">
</sqlMapGenerator>
<!--生成Dao文件,生成接口 --> <!-- (!!!!!!此处要改!!!!!!) -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.yuyu.dao"
targetProject="D:\mybatis3\src\main\java">
</javaClientGenerator>
<!-- (!!!!!!此处要改!!!!!!) -->
<table tableName="student" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="grade" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="role" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
4.运行
maven Project
选项卡
->plugins->
找到
mybatis-generator-core
,双击运⾏就会⾃动⽣成
注意
:
运⾏⼀次即可,如果运⾏过程中,未完全成功。则将原来⽣成的代码删除后,再次运⾏。