• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Mybatis 逆向工程的三种方法详解

mybatis 搞代码 4年前 (2022-01-09) 31次浏览 已收录 0个评论

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 => 重启。

2. 新建一个 Java Project 项目

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

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 和 NUMERI<mark style="color:transparent">来4源gaodaimacom搞#代%码*网</mark>C 类型解析为 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>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Mybatis 逆向工程的三种方法详解
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址