mybatis逆向工程是什么意思_长话短说的方法

mybatis逆向工程是什么意思_长话短说的方法目录Mybatis逆向工程一、通过Eclipse插件完成Mybatis逆向工程1.在线安装Eclipse插件2.新建一个JavaProject项目3.编写配置文件4.使用插件运行二、通过Java代码完成Mybatis逆向工程1.新建一个JavaProject项目2.编写配置文件3.编写生成代码程序三、通过Maven完成Mybatis逆向工程1…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

Mybatis 逆向工程

  逆向工程通常包括由数据库的表生成 Java 代码 和 通过 Java 代码生成数据库表。而Mybatis 逆向工程是指由数据库表生成 Java 代码。
  Mybaits 需要程序员自己编写 SQL 语句,但是 Mybatis 官方提供逆向工程可以针对单表自动生成 Mybaits 执行所需要的代码,包括 POJO、Mapper.java、Mapper.xml …。

一、通过 Eclipse 插件完成 Mybatis 逆向工程

1. 在线安装 Eclipse 插件

  操作步骤:打开Eclipse => Help => Eclipse Marketplace => 搜索 Mybatis Generator => 选择 Mybatis Generator 的版本 => Install => 重启。
下载安装 Mybatis Generator

2. 新建一个 Java Project 项目

  新建一个叫 mybatisGenerator 的 Java 项目,导入 MySQL 的驱动包,如果是 Oracle 数据库就导入 Oracle 的驱动包,我这里是 MySQL 数据库,所以导入的是 MySQL 的。
Java 项目结构

3. 编写配置文件

  逆向工程需要用到 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>
	<context id="testTables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!-- 是否去除自动生成的注释 true:是 : false:否 -->
			<property name="suppressAllComments" value="false" />
		</commentGenerator>
		<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
			password="123456">
		</jdbcConnection>
		<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
			connectionURL="jdbc:oracle:thin:@localhost:1521:mybatis" 
			userId=""
			password="">
		</jdbcConnection> -->

		<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
			NUMERIC 类型解析为java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- targetProject:生成PO类的位置 -->
		<javaModelGenerator targetPackage="com.ssm.po"
			targetProject="mybatisGenerator">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
			<!-- 从数据库返回的值被清理前后的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="com.ssm.mapper" 
			targetProject="mybatisGenerator">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper接口生成的位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.ssm.mapper" 
			targetProject="mybatisGenerator">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		<!-- 指定数据库表 -->
		<!-- 
			tableName:要生成的表名
       		domainObjectName:生成后的实例名
	        enableCountByExample:Count语句中加入where条件查询,默认true开启
	        enableUpdateByExample:Update语句中加入where条件查询,默认true开启
	        enableDeleteByExample:Delete语句中加入where条件查询,默认true开启
	        enableSelectByExample:Select多条语句中加入where条件查询,默认true开启
	        selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认true开启
		 -->
		<table tableName="items">
			<!-- 
				常用:
				property:将所有字段逆向生成为类属性,默认全部
				ignoreColumn:生成时忽略列字段 
			 -->
		</table>
		<table tableName="orders"></table>
		<table tableName="orderdetail"></table>
		<table tableName="user"></table>

		
	</context>
</generatorConfiguration>

注意:targetProject="mybatisGenerator"

4. 使用插件运行

  操作步骤:右击 generatorConfig.xml 文件 => Run as => Run Mybatis Generator => 刷新工程。
生成的代码
  有报错是因为没有导入 Mybatis 相关的包。最后将生成的文件拷入相关的工程当中。

二、通过 Java 代码完成 Mybatis 逆向工程

1. 新建一个 Java Project 项目

  新建一个 Java 项目,导入Mybatis逆向工程包mybatis-generator-core-1.3.2.jar和数据库驱动包mysql-connector-java-5.1.39-bin.jar
Java Project 项目结构

2. 编写配置文件

  编写配置文件,和前一种方法的配置文件差不多,区别在于这里的 targetProject 不一样,这种方式的是targetProject="./src",生成的文件也会在这个下面。

<?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>
	<context id="testTables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!-- 是否去除自动生成的注释 true:是 : false:否 -->
			<property name="suppressAllComments" value="false" />
		</commentGenerator>
		<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
			password="123456">
		</jdbcConnection>
		<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
			connectionURL="jdbc:oracle:thin:@localhost:1521:mybatis" 
			userId=""
			password="">
		</jdbcConnection> -->

		<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
			NUMERIC 类型解析为java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- targetProject:生成PO类的位置 -->
		<javaModelGenerator targetPackage="com.ssm.po"
			targetProject="./src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
			<!-- 从数据库返回的值被清理前后的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="com.ssm.mapper" 
			targetProject="./src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper接口生成的位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.ssm.mapper" 
			targetProject="./src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		<!-- 指定数据库表 -->
		<!-- 
			tableName:要生成的表名
       		domainObjectName:生成后的实例名
	        enableCountByExample:Count语句中加入where条件查询,默认true开启
	        enableUpdateByExample:Update语句中加入where条件查询,默认true开启
	        enableDeleteByExample:Delete语句中加入where条件查询,默认true开启
	        enableSelectByExample:Select多条语句中加入where条件查询,默认true开启
	        selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认true开启
		 -->
		<table tableName="items">
			<!-- 
				常用:
				property:将所有字段逆向生成为类属性,默认全部
				ignoreColumn:生成时忽略列字段 
			 -->
		</table>
		<table tableName="orders"></table>
		<table tableName="orderdetail"></table>
		<table tableName="user"></table>

		
	</context>
</generatorConfiguration>

3. 编写生成代码程序

  最后编写一个简单的 Java 运行程序,运行后刷新工程就可以了。

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GeneratorFromXML {

	public static void main(String[] args) throws Exception {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		File configFile = new File("generatorConfig.xml");
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(configFile);
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		myBatisGenerator.generate(null);
	}

}

生成的代码结构
  建议在这个项目中加入日志,这样能直观得看出其运行过程。
加入日志配置文件log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

运行 GeneratorFromXML.java 时产生的日志记录:

DEBUG [main] - Retrieving column information for table "items"
DEBUG [main] - Found column "id", data type 4, in table "mybatis..items"
DEBUG [main] - Found column "name", data type 12, in table "mybatis..items"
DEBUG [main] - Found column "price", data type 7, in table "mybatis..items"
DEBUG [main] - Found column "detail", data type -1, in table "mybatis..items"
DEBUG [main] - Found column "pic", data type 12, in table "mybatis..items"
DEBUG [main] - Found column "createtime", data type 93, in table "mybatis..items"
DEBUG [main] - Retrieving column information for table "orders"
DEBUG [main] - Found column "id", data type 4, in table "mybatis..orders"
DEBUG [main] - Found column "user_id", data type 4, in table "mybatis..orders"
DEBUG [main] - Found column "number", data type 12, in table "mybatis..orders"
DEBUG [main] - Found column "createtime", data type 93, in table "mybatis..orders"
DEBUG [main] - Found column "note", data type 12, in table "mybatis..orders"
DEBUG [main] - Retrieving column information for table "orderdetail"
DEBUG [main] - Found column "id", data type 4, in table "mybatis..orderdetail"
DEBUG [main] - Found column "orders_id", data type 4, in table "mybatis..orderdetail"
DEBUG [main] - Found column "items_id", data type 4, in table "mybatis..orderdetail"
DEBUG [main] - Found column "items_num", data type 4, in table "mybatis..orderdetail"
DEBUG [main] - Retrieving column information for table "user"
DEBUG [main] - Found column "ID", data type 4, in table "mybatis..user"
DEBUG [main] - Found column "USERNAME", data type 12, in table "mybatis..user"
DEBUG [main] - Found column "SEX", data type 12, in table "mybatis..user"
DEBUG [main] - Found column "birthday", data type 91, in table "mybatis..user"
DEBUG [main] - Found column "address", data type 12, in table "mybatis..user"

三、通过 Maven 完成 Mybatis 逆向工程

1. 新建一个 Maven Project 项目

  新建一个 Maven 项目,然后新建文件夹 /mybatis-maven/src/main/resources,在文件夹下新建文件 generatorConfig.xml。
Maven 项目结构

2. 配置 pom.xml 文件

  配置 pom.xml 文件,在 pom.xml 文件的 project 标签里加入代码:

<build>
	<plugins>
		<plugin>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-maven-plugin</artifactId>
			<version>1.3.2</version>
			<dependencies>
				<dependency>
		            <groupId>mysql</groupId>
		            <artifactId>mysql-connector-java</artifactId>
		            <version>5.1.38</version>
		        </dependency>	
			</dependencies>
			<configuration>
				<overwrite>true</overwrite>
			</configuration>
		</plugin>
	</plugins>
</build>

  配置插件 generator 版本是 1.3.2 并配置 Mysql 驱动是 5.1.38。

3. 配置文件 generatorConfig.xml

  generatorConfig.xml 是在目录 src 下的 main 下的 resources 下。注意这里的targetProject="./src" 生成的文件也会在这个下面。

<?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>
	<context id="testTables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!-- 是否去除自动生成的注释 true:是 : false:否 -->
			<property name="suppressAllComments" value="false" />
		</commentGenerator>
		<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
			password="123456">
		</jdbcConnection>
		<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
			connectionURL="jdbc:oracle:thin:@localhost:1521:mybatis" 
			userId=""
			password="">
		</jdbcConnection> -->

		<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
			NUMERIC 类型解析为java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- targetProject:生成PO类的位置 -->
		<javaModelGenerator targetPackage="com.ssm.po"
			targetProject="./src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
			<!-- 从数据库返回的值被清理前后的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="com.ssm.mapper" 
			targetProject="./src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper接口生成的位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.ssm.mapper" 
			targetProject="./src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		<!-- 指定数据库表 -->
		<!-- 
			tableName:要生成的表名
       		domainObjectName:生成后的实例名
	        enableCountByExample:Count语句中加入where条件查询,默认true开启
	        enableUpdateByExample:Update语句中加入where条件查询,默认true开启
	        enableDeleteByExample:Delete语句中加入where条件查询,默认true开启
	        enableSelectByExample:Select多条语句中加入where条件查询,默认true开启
	        selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认true开启
		 -->
		<table tableName="items">
			<!-- 
				常用:
				property:将所有字段逆向生成为类属性,默认全部
				ignoreColumn:生成时忽略列字段 
			 -->
		</table>
		<table tableName="orders"></table>
		<table tableName="orderdetail"></table>
		<table tableName="user"></table>

		
	</context>
</generatorConfiguration>

4. 运行 Maven

  运行命令mybatis-generator:generate
  操作步骤:选中项目右击 => Run As => Maven build… =>在 Goals 中输入mybatis-generator:generate => Run =>刷新工程。
生成的代码结构

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/171823.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 各位学弟学妹,别再看教材了,时间复杂度看这篇就好了[通俗易懂]

    各位学弟学妹,别再看教材了,时间复杂度看这篇就好了[通俗易懂]时间复杂度是学习算法的基石,今天我们来聊聊为什么要引入时间复杂度,什么是时间复杂度以及如何去算一个算法的时间复杂度一、刻画算法的运行时间某日,慧能叫来了一尘打算给他补习补习一下基础知识,只见克写了一段非常简单的代码一尘看老师有点生气,开始虚心请教了为了方便讨论,这里我们把每一条语句的执行时间都看做是一样的,记为一个时间单元①蓝色框的两条语句,花费两个时间单元②黑色框的一条语句,花费n+1个时间单元③红色框的两条语句,花费2*n个时间单元这不是.

    2022年5月15日
    33
  • PR曲线和ROC曲线概念及其区别

    PR曲线和ROC曲线概念及其区别将测试样本的预测结果按照置信度排序,由高到低,卡个阈值作为正负样本的判定依据,阈值较高时,Precision比较大,阈值较低时,Recall较大。(推荐的话,想Precision较大,用户希望排名靠前的推荐是自己想要的,刑侦的话希望Recall较大,不错过一个犯人)知识点Precision=TP/(TP+FP)Recall=TP/(总的正样本)=TP/(TP+FN)#这个时候的TP…

    2022年6月15日
    32
  • being搜索引擎用户体验[通俗易懂]

    being搜索引擎用户体验[通俗易懂]being的搜索引擎带有的自动区别全英(汉)来呈现不同内容的结果,使用户能更快找到需要的结果。它的取词翻译可以很快的使用户得到想要的讯息。但是它的取词翻译有很多地方有一些多余,如myusou

    2022年7月1日
    48
  • oracle 字符串转数组「建议收藏」

    SELECTdistinctREGEXP_SUBSTR(t.llbm1,‘[^;]+’,1,LEVEL,‘i’)ASSTRFROMuser_nbgzlldt,cpcshtinsswheret.shtinsid=s.shtinsidands.wfid=35connectbylevel<=(regexp_count(t.llbm1,’;’)+1)

    2022年4月14日
    79
  • python导入鸢尾花数据集_python数据挖掘学习笔记】十九.鸢尾花数据集可视化、线性回归、决策树花样分析…

    #2018-04-0516:57:26AprilThursdaythe14week,the095daySZSSMRpython数据挖掘学习笔记】十九.鸢尾花数据集可视化、线性回归、决策树花样分析1.鸢尾花数据集可视化分析2.线性回归分析鸢尾花花瓣长度和宽度的关系3.决策树分析鸢尾花数据集4.Kmeans聚类分析鸢尾花数据集一.鸢尾花数据集介绍本章采用Python的Sklea…

    2022年4月16日
    124
  • [黑苹果系列] M910x完美黑苹果系统安装教程 – 4 安装&设置macOS系统 – System Install

    [黑苹果系列] M910x完美黑苹果系统安装教程 – 4 安装&设置macOS系统 – System Install在BIOS中设置U盘启动,然后经过一段跑码,可以进到MacOS的界面,之后进入安装界面,先选择磁盘工具然后选择磁盘工具->显示所有设备,即可看见左侧的物理磁盘名称选择所需要安装的盘,选择抹掉然后,填好磁盘名和格式为APFS,方案为GUID分区图,然后抹掉完成后退出磁盘工具,选择安装macOSBigSur点击继续和同意后,选择相应的磁盘,点击继续,等待安装然后会安装完后会开始跑码重启后继续安装,到设置界面大约要重启…

    2022年6月9日
    61

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号