mybatis-generator逆向工程生成Model与Mapper的笔记

想要尝试使用Maven插件-mybatis-generator(mybatis自动生成实体与mapper带增删查改接口代码的插件)
话不多说上代码
1.新建一个maven项目
修改pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mybatis</groupId>
	<artifactId>autogenerator</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>autogenerator Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<!-- 添加依赖开始 -->
		<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>1.3.5</version>
		</dependency>
		<!-- 添加依赖结束 -->
	</dependencies>
	<build>
		<finalName>autogenerator</finalName>
		<!-- 添加插件开始 -->
		<plugins>
		<plugin>
           <groupId>org.mybatis.generator</groupId>
           <artifactId>mybatis-generator-maven-plugin</artifactId>
           <version>1.3.2</version>
           <executions>
              <execution>
                 <id>Generate MyBatis Files</id>
                 <goals>
                    <goal>generate</goal>
                 </goals>
                 <phase>generate</phase>
                 <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                 </configuration>
              </execution>
           </executions>
           <dependencies>
              <dependency>
                 <groupId>mysql</groupId>
                 <artifactId>mysql-connector-java</artifactId>
                 <version>8.0.13</version>
              </dependency>
              <dependency>
                 <groupId>org.mybatis.generator</groupId>
           <artifactId>mybatis-generator-core</artifactId>
                 <version>1.3.5</version>
              </dependency>
              <dependency>
                 <groupId>org.mybatis</groupId>
                 <artifactId>mybatis</artifactId>
                 <version>3.4.2</version>
              </dependency>
           </dependencies>
        </plugin>
        </plugins>
        <!-- 添加插件结束 -->
	</build>
</project>

2.resource文件夹添加配置文件

<?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="properties/jdbc.properties" />

	<context id="mysqlTables" targetRuntime="MyBatis3">
		<jdbcConnection driverClass="${jdbc.driver}"
			connectionURL="${jdbc.url}" userId="${jdbc.username}"
			password="${jdbc.password}" />
		<!--指定生成的类型为java类型,避免数据库中number等类型字段 -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!--自动生成的实体的存放包路径 -->
		<javaModelGenerator
			targetPackage="com.mybatis.generator.entity"
			targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!--自动生成的*Mapper.xml文件存放路径 -->
		<sqlMapGenerator
			targetPackage="com.mybatis.generator.mapper"
			targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!--自动生成的*Mapper.java存放路径 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.mybatis.generator.mapper"
			targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<table tableName="tb_user" domainObjectName="User"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			selectByExampleQueryId="false">
			<generatedKey column="id"
				sqlStatement="selectuuid_short()" identity="false" />
		</table>
		<table tableName="tb_customer" domainObjectName="Customer"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			selectByExampleQueryId="false">
			<generatedKey column="id"
				sqlStatement="select uuid_short()" identity="false" />
		</table>
	</context>
</generatorConfiguration>

添加maven运行命令
右键项目run as ==》run configrations
这里需要填写两个红框内的内容(第二个框的内容固定)
在这里插入图片描述

点击apply后,点击run
即可生成
项目结构(红框为生成的包)
在这里插入图片描述

在这里插入图片描述

项目地址(无添加log4j)无spring

参考地址
https://blog.csdn.net/liyonghong3333/article/details/79590588
官网
http://www.mybatis.org/generator/quickstart.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值