1.在父pom中加入mybatis generator代码生成器插件
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
</dependencies>
</plugin>
2.在指定的文件路径下添加配置文件
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
在公共模块server的resources文件下新建目录generator添加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>
<context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
<!-- 避免关键字与字段名冲突-->
<property name="autoDelimitKeywords" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!--覆盖生成XML文件-->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
<!-- 生成的实体类添加toString()方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<!-- 不生成注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接和驱动包-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/courseimooc"
userId="root"
password="root">
</jdbcConnection>
<!-- domain类的位置 -->
<javaModelGenerator targetProject="src\main\java"
targetPackage="com.course.server.domain"/>
<!-- mapper xml的位置 -->
<sqlMapGenerator targetProject="src\main\resources"
targetPackage="mapper"/>
<!-- mapper类的位置 -->
<javaClientGenerator targetProject="src\main\java"
targetPackage="com.course.server.mapper"
type="XMLMAPPER" />
<!-- <table tableName="test" domainObjectName="Test"/>-->
<!-- <table tableName="chapter" domainObjectName="Chapter"/>-->
<!-- <table tableName="section" domainObjectName="Section"/>-->
<!-- <table tableName="course" domainObjectName="Course"/>-->
<!-- <table tableName="course_content" domainObjectName="CourseContent"/>-->
<!-- <table tableName="course_content_file" domainObjectName="CourseContentFile"/>-->
<!-- <table tableName="teacher" domainObjectName="Teacher"/>-->
<!-- <table tableName="file" domainObjectName="File"/>-->
<!-- <table tableName="user" domainObjectName="User"/>-->
<!-- <table tableName="resource" domainObjectName="Resource"/>-->
<!-- <table tableName="role" domainObjectName="Role"/>-->
<!-- <table tableName="role_resource" domainObjectName="RoleResource"/>-->
<!-- <table tableName="role_user" domainObjectName="RoleUser"/>-->
<!-- <table tableName="member" domainObjectName="Member"/>-->
<!-- <table tableName="sms" domainObjectName="Sms"/>-->
<table tableName="test" domainObjectName="Test"/>
</context>
</generatorConfiguration>
3.添加Maven命令
在Edit Configuration中
之后可以运行一下。
如果报错
有如下可能:
1.命令写错
正确写法:mybatis-generator:generate -e
2.父pom文件没有添加mybatis generator插件
4.解决代码生成重复的问题
解决方法一: 手动删除mapper.xml
解决方案二:在generatorConfig.xml配置文件中添加
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
注:该UnmergeableXmlMappersPlugin类在1.3.7版本才出现
5.example的使用
给类是代码生成器生成的
Example可以帮我们写入where ,order by等,熟练掌握,可以极大地提高单表的开发效率