如何自动生成JAVA代码?---MyBatis逆向工程详细解析---Eclipse篇

MyBatis的逆向工程

  MyBatis的逆向工程能自动帮开发者生成数据库表对应的 pojo实体文件,自动生成映射文件
  自定生成表的各种(CRUD)的sql语句, 但是只能做单表操作,联合查询还得开发者自己动
  使用逆向工程得先在Eclipse安装逆向工程的插件

一、插件安装步骤

在Eclipse中点击导航行中的Help—>Eclipse Marketplace wizard

在这里插入图片描述
搜索 mybatis 点击install进行安装
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
可创建MyBatis文件即安装成功
在这里插入图片描述

二、逆向工程步骤

   新建一个普通java项目,导入mybatis.jar包和数据库驱动包
在这里插入图片描述

   在src目录创建MyBatis文件
在这里插入图片描述
   配置生成文件

<?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="DB2Tables" targetRuntime="MyBatis3">
   <!-- 注释构建 -->
    <commentGenerator>
       <!-- 去掉所有的注释 -->
    	<property name="suppressAllComments" value="true"/>
    	<property name="suppressDate" value="true"/>
    </commentGenerator>
    
    <!-- 数据库四要素 -->
    <jdbcConnection connectionURL="jdbc:mysql://localhost:3306/mybatis" 
    driverClass="com.mysql.jdbc.Driver" 
    password="root" 
    userId="root" />
    <!-- 实体类 : pojo
    	targetPackage : 实体类生成后存放的包
    	targetProject : 存放的目录一般都放在 src下面  项目名/src
      -->
    <javaModelGenerator targetPackage="com.ywq.mybatis.pojo" targetProject="mybatis-generator/src" />
    <!-- 映射文件 -->
    <sqlMapGenerator targetPackage="com.ywq.mybatis.mapper" targetProject="mybatis-generator/src" />
    
    <!-- 操作接口 
    	type 生成映射的形式
    		ANNOTATEDMAPPER : 纯注解的,没有xml映射
    		XMLMAPPER : 生成的有xml映射文件
    -->
    <javaClientGenerator type="ANNOTATEDMAPPER"   targetPackage="com.ywq.mybatis.mapper" targetProject="mybatis-generator/src" />
    
    <!-- 要生成对应表的配置
    	tableName : 数据库表名
    	//如果下面全部是true,mybatis直接可以使用纯面向对象开发
    	enableCountByExample : 是否生成查询总数的 Example 
    	enableDeleteByExample : 是否生成删除的 Example 
    	enableSelectByExample : 是否生成查询集合的 Example 
    	enableUpdateByExample : 是否生成修改的 Example 
     -->
    <table  tableName="user"   enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
    <table  tableName="employee" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
    <table  tableName="department" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
  </context>
</generatorConfiguration>

三、开始逆向工程

  选中逆向工程配置文件,点击鼠标右键
在这里插入图片描述

运行结果

在这里插入图片描述

四、逆向功能的缺点

   逆向功能不能逆向多表,只能逆向单表操作,多表之间有外键对应java关联关系没办法映射,需要开发者手动编写对应代码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值