Mybatis逆向工程運行代碼實例
簡單的理解,MyBatis逆向工程,就是通過相應插件,自動生成MyBatis數(shù)據(jù)庫連接的一些文件。
mybatis需要編寫sql語句,mybatis官方提供逆向工程,可以針對單表自動生成mybatis執(zhí)行所需要的代碼(mapper.java、mapper.xml、pojo…),提高工作效率。
命令:
mvn mybatis-generator:generate
項目結構:
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 id="mysqlgenerator" targetRuntime="MyBatis3"> <property name="autoDelimitKeywords" value="true"/> <!--可以使用``包括字段名,避免字段名與sql保留字沖突報錯--> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!-- 自動生成toString方法 --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/> <!-- 自動生成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"/> <!-- 查詢結果選擇性返回插件 --> <plugin type="com.itfsw.mybatis.generator.plugins.SelectSelectivePlugin"/> <!-- Example Criteria 增強插件 --> <plugin type="com.itfsw.mybatis.generator.plugins.ExampleEnhancedPlugin"/> <!-- 數(shù)據(jù)Model屬性對應Column獲取插件 --> <plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/> <!-- 邏輯刪除插件 --> <plugin type="com.itfsw.mybatis.generator.plugins.LogicalDeletePlugin"> <!-- 這里配置的是全局邏輯刪除列和邏輯刪除值,當然在table中配置的值會覆蓋該全局配置 --> <!-- 邏輯刪除列類型只能為數(shù)字、字符串或者布爾類型,數(shù)據(jù)庫中用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ù)庫連接信息--> <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>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解使用IntelliJ IDEA新建Java Web后端resfulAPI模板
這篇文章主要介紹了詳解使用IntelliJ IDEA新建Java Web后端resfulAPI模板,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Java Fluent Mybatis實戰(zhàn)之構建項目與代碼生成篇上
Java中常用的ORM框架主要是mybatis, hibernate, JPA等框架。國內(nèi)又以Mybatis用的多,基于mybatis上的增強框架,又有mybatis plus和TK mybatis等。今天我們介紹一個新的mybatis增強框架 fluent mybatis2021-10-10springboot實現(xiàn)學生管理系統(tǒng)
這篇文章主要為大家詳細介紹了springboot實現(xiàn)學生管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07