一、配置项目的pom.xml
<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>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
</plugin>
<plugins>
二、配置generatorConfig.xml
在src/main/resources下新建generatorConfig.xml,放在哪一个目录要根据第一步配置的 <configurationFile>
<?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>
<!-- 配置驱动:找到驱动所在的位置,路径不正确连接不了数据库 -->
<classPathEntry location="D:\mysql-connector-java-5.0.5.jar" />
<context id="context1">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressDate" value="true" />
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- connectionURL:要写你自己新建的数据库的名字,确保数据库中已经建好了表,用户名,密码要正确 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/crm userId="root"
password="123456" />
<!-- 生成实体类 实体bean文件,需要在src/main/java建立目录com.abc.po-->
<javaModelGenerator targetPackage="com.abc.po"
targetProject="src/main/java" />
<!-- mapper xml文件,需要在src/main/java建立目录com.abc.dao-->
<sqlMapGenerator targetPackage="com.abc.dao"
targetProject="src/main/java" />
<!-- mapper 接口文件,需要在src/main/java建立目录com.abc.mapper -->
<javaClientGenerator targetPackage="com.abc.mapper"
targetProject="src/main/java" type="XMLMAPPER" />
<!-- 要与数据库表的名称一致,enableCountByExample表示是否生成对应的example,false:不生成 -->
<table schema="" tableName="user_info" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table schema="" tableName="view_info" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
三、使用命令:maven mybatis-generator:generate
点击项目,右键maven--->maven build--->输入命令mybatis-generator:generate 等待后台结果。
注意:可能会出现错误,一般是数据库驱动的路径不对,找不到generatorConfig.xml配置文件,没有预先建立好相应的目录,如src/main/java下建立目录com.abc.mapper。