MyBatis Generator
簡稱MBG,是一個專門為MyBatis架構使用者定制的代碼生成器,可以快速的根據表生成對應的映射檔案,接口,以及bean類。支援基本的增删改查,以及QBC風格的條件查詢。但是表連接配接、存儲過程等這些複雜sql的定義需要我們手工編寫
官方文檔位址:http://www.mybatis.org/generator/
官方工程位址:https://github.com/mybatis/generator/releases
【1】POM檔案
引入依賴如下:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<!--<configurationFile>-->
<!--${basedir}/src/main/resources/generator/generatorConfig.xml-->
<!--</configurationFile>-->
</configuration>
</plugin>
</plugins>
【2】generator配置檔案
generatorConfig.properties
validationQuery=SELECT 1
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://127.0.0.1:3306/hhprovince?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=true
jdbc_username=root
jdbc_password=123456
tableName=user_employee
domainObjectName=SysEmployee
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>
<!-- 引用系統全局配置檔案 -->
<properties resource="generator/generatorConfig.properties" />
<classPathEntry location="E:/softinstall/mavenRep/mysql/mysql-connector-java/5.1.43/mysql-connector-java-5.1.43.jar"/>
<context id="hh" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
<commentGenerator>
<!-- 這個元素用來去除指定生成的注釋中是否包含生成的日期 false:表示保護 -->
<!-- 如果生成日期,會造成即使修改一個字段,整個實體類所有屬性都會發生變化,不利于版本控制,是以設定為true -->
<property name="suppressDate" value="true" />
<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--資料庫連結URL,使用者名、密碼 -->
<jdbcConnection driverClass="${jdbc_driverClassName}"
connectionURL="${jdbc_url}"
userId="${jdbc_username}"
password="${jdbc_password}">
</jdbcConnection>
<javaTypeResolver>
<!-- This property is used to specify whether MyBatis Generator should
force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.hh.sys.pojo" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射檔案的包名和位置 -->
<sqlMapGenerator targetPackage="com.hh.sys.mappers"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.hh.sys.dao" implementationPackage="com.hh.sys.dao.impl" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成哪些表 -->
<table tableName="${tableName}" domainObjectName="${domainObjectName}"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
關于最後一段的說明
如下所示會隻對表tb_sys_active進行逆向,生成的目标pojo為domainObjectName屬性指定的值。另外
<domainObjectRenamingRule searchString="^Tb" replaceString="" />
将會過濾掉
tb_
字首,注意
searchString="^Tb"
中T為大寫。
<table schema="general" tableName="tb_sys_active" domainObjectName="SysActive" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<domainObjectRenamingRule searchString="^Tb" replaceString="" />
</table>
如下所示,将會對資料庫所有表進行逆向:
<table schema="general" tableName="%" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<domainObjectRenamingRule searchString="^Tb" replaceString="" />
</table>
關于generator配置檔案位置說明
預設情況下,插件會先去
src/main/resources下找generatorConfig.xml
。如果在pom中插件位置進行了配置檔案配置,則會再去指定地方找:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>
${basedir}/src/main/resources/generator/generatorConfig.xml
</configurationFile>
</configuration>
</plugin>
【3】application.yml配置
主要是資料源的配置:
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/hhprovince?serverTimezone=GMT%2B8
username: root
password: 123456
# driver-class-name: com.mysql.jdbc.Driver
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: true
testOnReturn: false
poolPreparedStatements: true
# 配置監控統計攔截的filters,去掉後監控界面sql無法統計,'wall'用于防火牆
filters: stat,wall
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath*:mybatis/mapper/*.xml
# 下面可以不要
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
【4】maven指令運作
maven指令:
mybatis-generator:generate -e
在IntelliJ IDEA中的操作步驟如下:
- 首先選擇Run->Edit Configurations…
- 然後點選左上角的“+”号,選擇Maven
- 最後在Working directory中填入你項目的根目錄,然後在下面的Command line中填入
。點選OK即可。mybatis-generator:generate -e
如下圖所示:
idea直接在右側插件地方執行