1、pom 依赖的添加
target开头配置的参数需要注意, 否则在生成的是会提示, 找不到目录
1)、版本号定义
<properties>
<!-- MyBatis Generator -->
<!-- Java接口和实体类 -->
<targetJavaProject>${basedir}/src/main/java</targetJavaProject> <targetMapperPackage>com.jdjr.zk.mapper</targetMapperPackage>
<targetModelPackage>com.jdjr.zk.entity</targetModelPackage>
<!-- XML生成路径 -->
<targetResourcesProject>${basedir}/src/main/resources</targetResourcesProject>
<targetXMLPackage>mappers</targetXMLPackage>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<mysql-version>5.1.45</mysql-version>
<druid-version>1.0.28</druid-version>
<mapper.version>3.4.0</mapper.version>
</properties>
2)、添加实际依赖
<!-- mybatis -->
<!-- 通用mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-version}</version>
</dependency>
3)、编译插件配置:
注意: 貌似1.3.5 版本覆盖命令才有用
<build>
<finalName>zk-control-admin</finalName>
<plugins>
<!-- 此插件用来生成通用mapper的代码 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/builder/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>${mapper.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
2、resources 下面创建builder 文件夹
在当前文件夹下面创建: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="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<property name="mergeable" value="false"></property>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
</plugin>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/dubbo-admin?useUnicode=true&characterEncoding=UTF8"
userId="root"
password="123456">
</jdbcConnection>
<javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"/>
<sqlMapGenerator targetPackage="${targetXMLPackage}" targetProject="${targetResourcesProject}"/>
<javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}"
type="XMLMAPPER"/>
<table tableName="zk_info" domainObjectName="zkInfo">
</table>
</context>
</generatorConfiguration>
图片详解:
3、执行执行maven 生成命令(下面是来自mybatis 官网)
Maven Goal and Execution
The MBG Maven plugin includes one goal:
- mybatis-generator:generate
The goal is not automatically executed by Maven. It can be executed in two ways.
The goal can be executed from the command line with the command:
- mvn mybatis-generator:generate
You can pass parameters to the goal with standard Maven command line properties. For example:
- mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate
This will run MBG and instruct it to overwrite any existing Java files it may find.
In a continuous build environment, you may want to automatically execute MBG as a part of a Maven build. This can be accomplished by configuring the goal to execute automatically. An example of this is shown below:
参考地址: http://www.mybatis.org/generator/running/runningWithMaven.html#