1、创建mybatis工程和数据库表,以下完成的项目图和数据库表图
2、配置pom.xml,放在build部分,要和pluginManagement同级
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</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.7</version>
</dependency>
</dependencies>
</plugin>
</plugins>
出现下图代表成功
3、将下载好的generatorConfig.xml文件复制到resources中,并做相关配置
<?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="E:\jar\mysql-connector-java-5.1.46.jar"/>
<!--<context id="MySqlContext"
targetRuntime="MyBatis3Simple"
defaultModelType="flat">-->
<context id="MySqlContext" defaultModelType="flat">
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 注释 -->
<commentGenerator>
<property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->
<property name="suppressDate" value="true"/> <!-- 是否生成注释代时间戳-->
</commentGenerator>
<!-- jdbc连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis"
userId="root"
password="1234">
</jdbcConnection>
<!-- 生成model模型,对应的包,存放位置可以指定具体的路径 -->
<javaModelGenerator targetPackage="com.etc.model"
targetProject="src/main/java">
<!-- 是否针对string类型的字段在set的时候进行trim调用 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成mapper.xml文件 -->
<sqlMapGenerator targetPackage="resources"
targetProject="src/main/resources">
<!-- 是否在当前路径下新加一层schema,eg:
fase路径com.oop.eksp.user.model,
true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成mapper.xml对应client,也就是接口dao -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.etc.dao"
targetProject="src/main/java">
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 配置表信息 -->
<table tableName="class_15" domainObjectName="Student">
<!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
是否生成 example类 -->
<!-- 忽略列,不生成bean 字段 -->
<!-- <ignoreColumn column="FRED" /> -->
<!-- 指定列的java数据类型 -->
<!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
</table>
</context>
</generatorConfiguration>
(1)generatorConfig.xml 中(不需要生成example相关方法)
<context id="MySqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
(2)generatorConfig.xml 中(能生成Example对象和相关方法), 不设置targetRuntime属性,默认值为MyBatis3。
<context id="MySqlContext" defaultModelType="flat">
Mybatis Generator最完整配置详解:https://www.jianshu.com/p/e09d2370b796
4、配置dtd,配置后,在文件中输入才会有提示。下载地址:http://www.mybatis.org/dtd/mybatis-generator-config_1_0.dtd
打开file——Settings..(快捷键ctrl+alt+s)
找到Languages&Frameworks——Schemas and DTDs
右侧的External Schemas and DTDs窗口中点击添加按钮
将拷贝的url粘贴到对应位置,选择本地的dtd文件(mybatis-generator-config_1_0.dtd)
5、双击执行,即可自动生成映射和对应类文件