Mybatis 逆向工程的三種方法詳解
Mybatis 逆向工程
逆向工程通常包括由數(shù)據(jù)庫(kù)的表生成 Java 代碼 和 通過(guò) Java 代碼生成數(shù)據(jù)庫(kù)表。而Mybatis 逆向工程是指由數(shù)據(jù)庫(kù)表生成 Java 代碼。
Mybaits 需要程序員自己編寫(xiě) SQL 語(yǔ)句,但是 Mybatis 官方提供逆向工程可以針對(duì)單表自動(dòng)生成 Mybaits 執(zhí)行所需要的代碼,包括 POJO、Mapper.java、Mapper.xml …。
一、通過(guò) Eclipse 插件完成 Mybatis 逆向工程
1. 在線(xiàn)安裝 Eclipse 插件
操作步驟:打開(kāi)Eclipse => Help => Eclipse Marketplace => 搜索 Mybatis Generator => 選擇 Mybatis Generator 的版本 => Install => 重啟。
2. 新建一個(gè) Java Project 項(xiàng)目
新建一個(gè)叫 mybatisGenerator 的 Java 項(xiàng)目,導(dǎo)入 MySQL 的驅(qū)動(dòng)包,如果是 Oracle 數(shù)據(jù)庫(kù)就導(dǎo)入 Oracle 的驅(qū)動(dòng)包,我這里是 MySQL 數(shù)據(jù)庫(kù),所以導(dǎo)入的是 MySQL 的。
3. 編寫(xiě)配置文件
逆向工程需要用到 xml 配置文件,編寫(xiě)配置文件(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> <!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 --> <property name="suppressAllComments" value="false" /> </commentGenerator> <!--數(shù)據(jù)庫(kù)連接的信息:驅(qū)動(dòng)類(lèi)、連接地址、用戶(hù)名、密碼 --> <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> --> <!-- 默認(rèn)false,把JDBC DECIMAL 和 NUMERIC 類(lèi)型解析為 Integer,為 true時(shí)把JDBC DECIMAL 和 NUMERIC 類(lèi)型解析為java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成PO類(lèi)的位置 --> <javaModelGenerator targetPackage="com.ssm.po" targetProject="mybatisGenerator"> <!-- enableSubPackages:是否讓schema作為包的后綴 --> <property name="enableSubPackages" value="false" /> <!-- 從數(shù)據(jù)庫(kù)返回的值被清理前后的空格 --> <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> <!-- 指定數(shù)據(jù)庫(kù)表 --> <!-- tableName:要生成的表名 domainObjectName:生成后的實(shí)例名 enableCountByExample:Count語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 enableUpdateByExample:Update語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 enableDeleteByExample:Delete語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 enableSelectByExample:Select多條語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 selectByExampleQueryId:Select單個(gè)對(duì)象語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 --> <table tableName="items"> <!-- 常用: property:將所有字段逆向生成為類(lèi)屬性,默認(rèn)全部 ignoreColumn:生成時(shí)忽略列字段 --> </table> <table tableName="orders"></table> <table tableName="orderdetail"></table> <table tableName="user"></table> </context> </generatorConfiguration>
注意:targetProject="mybatisGenerator"
4. 使用插件運(yùn)行
操作步驟:右擊 generatorConfig.xml 文件 => Run as => Run Mybatis Generator => 刷新工程。
有報(bào)錯(cuò)是因?yàn)闆](méi)有導(dǎo)入 Mybatis 相關(guān)的包。最后將生成的文件拷入相關(guān)的工程當(dāng)中。
二、通過(guò) Java 代碼完成 Mybatis 逆向工程
1. 新建一個(gè) Java Project 項(xiàng)目
新建一個(gè) Java 項(xiàng)目,導(dǎo)入Mybatis逆向工程包mybatis-generator-core-1.3.2.jar
和數(shù)據(jù)庫(kù)驅(qū)動(dòng)包mysql-connector-java-5.1.39-bin.jar
。
2. 編寫(xiě)配置文件
編寫(xiě)配置文件,和前一種方法的配置文件差不多,區(qū)別在于這里的 targetProject 不一樣,這種方式的是targetProject="./src"
,生成的文件也會(huì)在這個(gè)下面。
<?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> <!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 --> <property name="suppressAllComments" value="false" /> </commentGenerator> <!--數(shù)據(jù)庫(kù)連接的信息:驅(qū)動(dòng)類(lèi)、連接地址、用戶(hù)名、密碼 --> <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> --> <!-- 默認(rèn)false,把JDBC DECIMAL 和 NUMERIC 類(lèi)型解析為 Integer,為 true時(shí)把JDBC DECIMAL 和 NUMERIC 類(lèi)型解析為java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成PO類(lèi)的位置 --> <javaModelGenerator targetPackage="com.ssm.po" targetProject="./src"> <!-- enableSubPackages:是否讓schema作為包的后綴 --> <property name="enableSubPackages" value="false" /> <!-- 從數(shù)據(jù)庫(kù)返回的值被清理前后的空格 --> <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> <!-- 指定數(shù)據(jù)庫(kù)表 --> <!-- tableName:要生成的表名 domainObjectName:生成后的實(shí)例名 enableCountByExample:Count語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 enableUpdateByExample:Update語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 enableDeleteByExample:Delete語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 enableSelectByExample:Select多條語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 selectByExampleQueryId:Select單個(gè)對(duì)象語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 --> <table tableName="items"> <!-- 常用: property:將所有字段逆向生成為類(lèi)屬性,默認(rèn)全部 ignoreColumn:生成時(shí)忽略列字段 --> </table> <table tableName="orders"></table> <table tableName="orderdetail"></table> <table tableName="user"></table> </context> </generatorConfiguration>
3. 編寫(xiě)生成代碼程序
最后編寫(xiě)一個(gè)簡(jiǎn)單的 Java 運(yùn)行程序,運(yùn)行后刷新工程就可以了。
<?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> <!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 --> <property name="suppressAllComments" value="false" /> </commentGenerator> <!--數(shù)據(jù)庫(kù)連接的信息:驅(qū)動(dòng)類(lèi)、連接地址、用戶(hù)名、密碼 --> <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> --> <!-- 默認(rèn)false,把JDBC DECIMAL 和 NUMERIC 類(lèi)型解析為 Integer,為 true時(shí)把JDBC DECIMAL 和 NUMERIC 類(lèi)型解析為java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成PO類(lèi)的位置 --> <javaModelGenerator targetPackage="com.ssm.po" targetProject="./src"> <!-- enableSubPackages:是否讓schema作為包的后綴 --> <property name="enableSubPackages" value="false" /> <!-- 從數(shù)據(jù)庫(kù)返回的值被清理前后的空格 --> <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> <!-- 指定數(shù)據(jù)庫(kù)表 --> <!-- tableName:要生成的表名 domainObjectName:生成后的實(shí)例名 enableCountByExample:Count語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 enableUpdateByExample:Update語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 enableDeleteByExample:Delete語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 enableSelectByExample:Select多條語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 selectByExampleQueryId:Select單個(gè)對(duì)象語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 --> <table tableName="items"> <!-- 常用: property:將所有字段逆向生成為類(lèi)屬性,默認(rèn)全部 ignoreColumn:生成時(shí)忽略列字段 --> </table> <table tableName="orders"></table> <table tableName="orderdetail"></table> <table tableName="user"></table> </context> </generatorConfiguration>
建議在這個(gè)項(xiàng)目中加入日志,這樣能直觀得看出其運(yùn)行過(guò)程。
加入日志配置文件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
運(yùn)行 GeneratorFromXML.java 時(shí)產(chǎn)生的日志記錄:
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"
三、通過(guò) Maven 完成 Mybatis 逆向工程
1. 新建一個(gè) Maven Project 項(xiàng)目
新建一個(gè) Maven 項(xiàng)目,然后新建文件夾 /mybatis-maven/src/main/resources,在文件夾下新建文件 generatorConfig.xml。
2. 配置 pom.xml 文件
配置 pom.xml 文件,在 pom.xml 文件的 project 標(biāo)簽里加入代碼:
<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 驅(qū)動(dòng)是 5.1.38。
3. 配置文件 generatorConfig.xml
generatorConfig.xml 是在目錄 src 下的 main 下的 resources 下。注意這里的targetProject="./src"
生成的文件也會(huì)在這個(gè)下面。
<?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> <!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 --> <property name="suppressAllComments" value="false" /> </commentGenerator> <!--數(shù)據(jù)庫(kù)連接的信息:驅(qū)動(dòng)類(lèi)、連接地址、用戶(hù)名、密碼 --> <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> --> <!-- 默認(rèn)false,把JDBC DECIMAL 和 NUMERIC 類(lèi)型解析為 Integer,為 true時(shí)把JDBC DECIMAL 和 NUMERIC 類(lèi)型解析為java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成PO類(lèi)的位置 --> <javaModelGenerator targetPackage="com.ssm.po" targetProject="./src"> <!-- enableSubPackages:是否讓schema作為包的后綴 --> <property name="enableSubPackages" value="false" /> <!-- 從數(shù)據(jù)庫(kù)返回的值被清理前后的空格 --> <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> <!-- 指定數(shù)據(jù)庫(kù)表 --> <!-- tableName:要生成的表名 domainObjectName:生成后的實(shí)例名 enableCountByExample:Count語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 enableUpdateByExample:Update語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 enableDeleteByExample:Delete語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 enableSelectByExample:Select多條語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 selectByExampleQueryId:Select單個(gè)對(duì)象語(yǔ)句中加入where條件查詢(xún),默認(rèn)true開(kāi)啟 --> <table tableName="items"> <!-- 常用: property:將所有字段逆向生成為類(lèi)屬性,默認(rèn)全部 ignoreColumn:生成時(shí)忽略列字段 --> </table> <table tableName="orders"></table> <table tableName="orderdetail"></table> <table tableName="user"></table> </context> </generatorConfiguration>
4. 運(yùn)行 Maven
運(yùn)行命令mybatis-generator:generate
。
操作步驟:選中項(xiàng)目右擊 => Run As => Maven build… =>在 Goals 中輸入mybatis-generator:generate
=> Run =>刷新工程。
到此這篇關(guān)于Mybatis 逆向工程的三種方法詳解的文章就介紹到這了,更多相關(guān)Mybatis 逆向工程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java String、Json對(duì)象與byte數(shù)組轉(zhuǎn)換方式
這篇文章主要介紹了java String、Json對(duì)象與byte數(shù)組轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Java數(shù)據(jù)結(jié)構(gòu)之并查集的實(shí)現(xiàn)
并查集是一種用來(lái)管理元素分組情況的數(shù)據(jù)結(jié)構(gòu)。并查集可以高效地進(jìn)行如下操作。本文將通過(guò)Java實(shí)現(xiàn)并查集,感興趣的小伙伴可以了解一下2022-01-01SpringBoot集成Devtools實(shí)現(xiàn)熱更新
DevTools是開(kāi)發(fā)者工具集,主要用于簡(jiǎn)化開(kāi)發(fā)過(guò)程中的熱部署問(wèn)題,熱部署是指在開(kāi)發(fā)過(guò)程中,當(dāng)代碼發(fā)生變化時(shí),無(wú)需手動(dòng)重啟應(yīng)用,系統(tǒng)能夠自動(dòng)檢測(cè)并重新加載修改后的代碼,本文給大家介紹了SpringBoot集成Devtools實(shí)現(xiàn)熱更新,需要的朋友可以參考下2024-08-08java使用鏈表實(shí)現(xiàn)約瑟夫環(huán)
這篇文章主要為大家詳細(xì)介紹了java使用鏈表實(shí)現(xiàn)約瑟夫環(huán),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05SpringBoot中發(fā)送QQ郵件功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了SpringBoot中發(fā)送QQ郵件功能的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-02-02java SelectableChannel的使實(shí)例用法講解
在本篇文章里小編給大家整理的是一篇關(guān)于java SelectableChannel的使實(shí)例用法講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03解決Maven的pom.xml中設(shè)置repository不起作用問(wèn)題
這篇文章主要介紹了解決Maven的pom.xml中設(shè)置repository不起作用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03FutureTask為何單個(gè)任務(wù)僅執(zhí)行一次原理解析
這篇文章主要為大家介紹了FutureTask為何單個(gè)任務(wù)僅執(zhí)行一次原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法
這篇文章主要介紹了Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08