一、依賴jar包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
二、核心配置檔案: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="GCCSMysql" targetRuntime="MyBatis3" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<property name="mergeable" value="false"></property>
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
<commentGenerator>
<!-- 抑制警告 -->
<property name="suppressTypeWarnings" value="true"/>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="false"/>
<property name="javaFileEncoding" value="UTF-8"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/db_test_user?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true"
userId="root"
password="root">
</jdbcConnection>
<javaModelGenerator targetPackage="com.mybatis.test.model.user"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.mybatis.test.mapper.user"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.mybatis.test.mapper.user"
targetProject="src/main/java"
type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="t_custom" domainObjectName="Custom"
enableInsert="true"
enableSelectByPrimaryKey="true"
enableSelectByExample="true"
enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true"
enableDeleteByExample="true"
enableCountByExample="true"
enableUpdateByExample="true"
selectByPrimaryKeyQueryId="true"
selectByExampleQueryId="true">
</table>
<table tableName="t_seller" domainObjectName="Seller"
enableInsert="true"
enableSelectByPrimaryKey="true"
enableSelectByExample="true"
enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true"
enableDeleteByExample="true"
enableCountByExample="true"
enableUpdateByExample="true"
selectByPrimaryKeyQueryId="true"
selectByExampleQueryId="true">
</table>
</context>
</generatorConfiguration>
三、maven pom添加編譯配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<verbose>false</verbose>
<overwrite>false</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
四、運作
之後彈出運作配置框,為目前配置配置一個名稱,這裡其名為"generator",然後在 “Command line” 選項中輸入“mybatis-generator:generate -e”
這裡加了“-e ”選項是為了讓該插件輸出詳細資訊,這樣可以幫助我們定位問題。
如果添加成功,則會在run 選項中有“generator” 選項,如下: 點選運作
相關推薦:
快速搭建springboot項目
springboot內建mybatis
springboot內建hibernate