Mybatis逆向工程運(yùn)行代碼實(shí)例
簡(jiǎn)單的理解,MyBatis逆向工程,就是通過相應(yīng)插件,自動(dòng)生成MyBatis數(shù)據(jù)庫(kù)連接的一些文件。
mybatis需要編寫sql語(yǔ)句,mybatis官方提供逆向工程,可以針對(duì)單表自動(dòng)生成mybatis執(zhí)行所需要的代碼(mapper.java、mapper.xml、pojo…),提高工作效率。
命令:
mvn mybatis-generator:generate
項(xiàng)目結(jié)構(gòu):
generatorConfig.xml內(nèi)容示例
<?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 targetRuntime='MyBatis3'> <property name='autoDelimitKeywords' value='true'/> <!--可以使用``包括字段名,避免字段名與sql保留字沖突報(bào)錯(cuò)--> <property name='beginningDelimiter' value='`'/> <property name='endingDelimiter' value='`'/> <!-- 自動(dòng)生成toString方法 --> <plugin type='org.mybatis.generator.plugins.ToStringPlugin'/> <!-- 自動(dòng)生成equals方法和hashcode方法 --> <plugin type='org.mybatis.generator.plugins.EqualsHashCodePlugin'/> <!-- 非官方插件 https://github.com/itfsw/mybatis-generator-plugin --> <!-- 查詢單條數(shù)據(jù)插件 --> <plugin type='com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin'/> <!-- 查詢結(jié)果選擇性返回插件 --> <plugin type='com.itfsw.mybatis.generator.plugins.SelectSelectivePlugin'/> <!-- Example Criteria 增強(qiáng)插件 --> <plugin type='com.itfsw.mybatis.generator.plugins.ExampleEnhancedPlugin'/> <!-- 數(shù)據(jù)Model屬性對(duì)應(yīng)Column獲取插件 --> <plugin type='com.itfsw.mybatis.generator.plugins.ModelColumnPlugin'/> <!-- 邏輯刪除插件 --> <plugin type='com.itfsw.mybatis.generator.plugins.LogicalDeletePlugin'> <!-- 這里配置的是全局邏輯刪除列和邏輯刪除值,當(dāng)然在table中配置的值會(huì)覆蓋該全局配置 --> <!-- 邏輯刪除列類型只能為數(shù)字、字符串或者布爾類型,數(shù)據(jù)庫(kù)中用tinyint(1) --> <property name='logicalDeleteColumn' value='deleted'/> <!-- 邏輯刪除-已刪除值 --> <property name='logicalDeleteValue' value='1'/> <!-- 邏輯刪除-未刪除值 --> <property name='logicalUnDeleteValue' value='0'/> </plugin> <commentGenerator> <property name='suppressDate' value='true'/> <!--<property name='suppressAllComments' value='true'/>--> </commentGenerator> <!--數(shù)據(jù)庫(kù)連接信息--> <jdbcConnection driverClass='com.mysql.jdbc.Driver' connectionURL='jdbc:mysql://192.168.1.100:3306/theorydance?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&verifyServerCertificate=false&useSSL=false' userId='root' password='123456'/> <javaTypeResolver> <property name='useJSR310Types' value='true'/> </javaTypeResolver> <javaModelGenerator targetPackage='demo.theorydance.db.domain' targetProject='src/main/java'/> <sqlMapGenerator targetPackage='demo.theorydance.db.dao' targetProject='src/main/resources'/> <javaClientGenerator type='XMLMAPPER' targetPackage='demo.theorydance.db.dao' targetProject='src/main/java'/> <!--表名--> <table tableName='student'></table> </context></generatorConfiguration>
pom.xml中添加插件
<build> <plugins> <plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.7</version><configuration> <configurationFile> mybatis-generator/generatorConfig.xml </configurationFile> <overwrite>true</overwrite> <verbose>true</verbose></configuration><dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <dependency> <groupId>com.itfsw</groupId> <artifactId>mybatis-generator-plugin</artifactId> <version>1.2.12</version> </dependency></dependencies> </plugin> </plugins> </build>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 深入探討Oracle數(shù)據(jù)庫(kù)10g的Shrink機(jī)制2. Oracle數(shù)據(jù)庫(kù)在線表格重定義功能簡(jiǎn)介3. 用腳本和查詢主動(dòng)監(jiān)視Oracle 9i性能4. 特定配置下Oracle日志批量提交的最優(yōu)數(shù)量5. Oracle中如何從BasicFile遷移到SecureFile6. MySQL Community Server 5.1.497. SQL Server使用PIVOT與unPIVOT實(shí)現(xiàn)行列轉(zhuǎn)換8. Oracle9i在線表格重定義來(lái)組織表格9. SQL Server中的數(shù)據(jù)類型詳解10. oracle定時(shí)分析用戶下的所有表
