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關聯關係沒辦法映射,需要開發者手動編寫對應代碼