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刷新项目

相关文章
相关标签/搜索