MyBatis Generator 配置文件自動生成

官網:http://mybatis.github.io/generator/index.htmlhtml

項目目錄結構

項目目錄結構

添加Maven插件(pom.xml)

<project ...>
     ...
     <build>
       ...
       <plugins>
        ...
        <plugin>
      	  <groupId>org.mybatis.generator</groupId>
      	  <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.2</version>
        </plugin>
        ...
      </plugins>
      ...
    </build>
    ...
  </project>

配置Maven執行命令

mybatis-generator:generate

** 右鍵 --> Run As --> Run Configurations... **java

配置命令

配置命令

配置MyBatis GeneratorXML

generatorConfig.xmlmysql

<?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="init.properties" />
	<!-- JDBC驅動 -->
	<classPathEntry location="${class_path}" />

	<context id="Tables" targetRuntime="MyBatis3">

		<!-- 註釋 -->
		<commentGenerator>
			<property name="suppressAllComments" value="true" /><!-- 是否取消註釋 -->
			<property name="suppressDate" value="true" /> <!-- 是否生成註釋代時間戳 -->
		</commentGenerator>

		<!-- JDBC鏈接 -->
		<jdbcConnection driverClass="${jdbc_driver}"
			connectionURL="${jdbc_url}/${schema}" userId="${jdbc_user}" password="${jdbc_password}">
		</jdbcConnection>

		<!-- 類型轉換 -->
		<javaTypeResolver>
			<!-- 是否使用bigDecimal, false可自動轉化如下類型(Long, Integer, Short, etc.) -->
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- 生成實體類地址 -->
		<javaModelGenerator targetPackage="model"
			targetProject="${target_project}">
			<property name="enableSubPackages" value="false" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>

		<!-- 生成mapxml文件 -->
		<sqlMapGenerator targetPackage="xml" targetProject="${target_project}">
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>

		<!-- 生成mapxml對應client-->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="dao" targetProject="${target_project}">
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>

		<!-- 配置表信息 -->
		<!-- schema即爲數據庫名 tableName爲對應的數據庫表 domainObjectName是要生成的實體類 enable*ByExample 
			是否生成 example類 -->
		<table schema="${schema}" tableName="${table_name}"
			domainObjectName="${domain_object_name}" enableCountByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			enableUpdateByExample="false" />

	</context>
</generatorConfiguration>

init.propertiesgit

#Mybatis Generator configuration  
#JDBC驅動
class_path=C:\\Users\\Administrator\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.9\\mysql-connector-java-5.1.9.jar 
#驅動
jdbc_driver=com.mysql.jdbc.Driver
#數據庫鏈接
jdbc_url=jdbc:mysql://localhost:3306
#數據庫用戶名
jdbc_user=ROOT
#數據庫密碼
jdbc_password=******
#項目位置
target_project=src\\main\\java
#數據庫
schema=vcdb
#數據庫表
table_name=table
#model類名
domain_object_name=Model

執行運行命令

** 右鍵 --> Run As --> Maven build **github

運行

日誌

F5刷新項目

相關文章
相關標籤/搜索