5, mybatis-mybatis生成器的使用

MyBatis Generator是MyBatis 官方出的代码生成器。MBG能够自动生成实体类、Mapper接口以及对应的mapping文件,简化开发。

mybatis提供了很多种生成方案, 例如eclipse/idea插件, maven, ant等等方式生成。(推荐直接安装插件)

插件运行(未能正确运行)

添加依赖

MyBatis Generator Core

<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.4.0</version>
</dependency>

添加generatorConfig.xml配置文件

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>
	
	<classPathEntry
		location="D:\dev\m2\mysql\mysql-connector-java\8.0.21\mysql-connector-java-8.0.21.jar"/>

	<context id="msqlTables" targetRuntime="MyBatis3">
		<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
		<jdbcConnection connectionURL="jdbc:mysql://172.28.149.239:3306/test?useSSL=false"
			driverClass="com.mysql.cj.jdbc.Driver" password="root" userId="root" >

			<property name="nullCatalogMeansCurrent" value="true"/>
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		
		
		<javaModelGenerator targetPackage="top.itkaoti.example04.demo" targetProject="src\main\java">
			<property name="enableSubPackages" value="true"/>
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<sqlMapGenerator targetPackage="mapping" targetProject="src\main\resources">
			<property name="enableSubPackages" value="true"/>
		</sqlMapGenerator>
		<javaClientGenerator type="XMLMAPPER" targetPackage="top.itkaoti.example04.mapper" targetProject="src\main\java">
			<property name="enableSubPackages" value="true"/>
		</javaClientGenerator>

		<!--数据库表, 多个表在这里以此添加-->
		<table tableName="User" domainObjectName="User"
			enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false" >
			<property name="useActualColumnNames" value="false"/>
		</table>
	</context>
</generatorConfiguration>

安装MBG插件

help --> Eclipse Marketplace 搜索mybatis, 找到mybatis Generator插件安装

运行

在generatorConfig.xml文件上run as --> Run Mybatis Generator

命令运行(运行ok)

使用此方法的时候, 配置文件targetProject使用绝对路径

 java -jar /d/dev/m2/org/mybatis/generator/mybatis-generator-core/1.4.0/mybatis-generator-core-1.4.0.jar -configfile  /d/liumq/xiaodoubi/mybatis-test/src/main/resources/generatorConfig.xml

项目地址

MyBatis Generator