在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.16</version>
</dependency>
</dependencies>
</plugin>
**建立 File db.properties **
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo1
username=root
password=123456
mode.package=com.openlab.pojo //
dao.package=com.openlab.mapper
xml.mapper.package=com.openlab.mapper
target.project=MavenDemo
创建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>
<!--导入数据连接及属性配置等-->
<properties resource="jdbc.properties"></properties>
<!--基础配置在context标签
id 可以随意起
targetRuntime 运行环境
-->
<context id="context" targetRuntime="MyBatis3">
<!--对注释的控制-->
<commentGenerator>
<property name="suppressDate" value="true" />
</commentGenerator>
<!--我们要自动生成对应的代码,自然要获取数据库的连接-->
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}"></jdbcConnection>
<!--Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径
-->
<javaModelGenerator targetPackage="com.openlab.pojo" targetProject="src/main/java"></javaModelGenerator>
<!--Mapper.xml映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="com.openlab.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件的代码,即生成我们的dao接口
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<javaClientGenerator targetPackage="com.openlab.mapper" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!--配置需要映射的表-->
<table tableName="user" enableSelectByExample="false" enableDeleteByExample="false" enableUpdateByExample="false"
enableCountByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="emp" enableSelectByExample="false" enableDeleteByExample="false" enableUpdateByExample="false"
enableCountByExample="false" selectByExampleQueryId="false">
</table>
</context>
以上三个文件都存放在main\java\resourses
否则会报错 找不到相关文件
**相关文件配置完成后 右键点击项目工程-----> run as ----->Run Configurations 得到下图
**
Run之后refresh项目 相应的 包以及代码会在generatorConfig的指定目录出现