MyBatis Generator簡介
MyBatis Generator(MBG)是MyBatis MyBatis 和iBATIS的代碼生成器。它將為所有版本的MyBatis以及版本2.2.0之后的iBATIS版本生成代碼。它將內(nèi)省數(shù)據(jù)庫表(或許多表),并將生成可用于訪問表的工件。這減少了設(shè)置對象和配置文件以與數(shù)據(jù)庫表交互的初始麻煩。MBG尋求對簡單CRUD(創(chuàng)建,檢索,更新,刪除)的大部分?jǐn)?shù)據(jù)庫操作產(chǎn)生重大影響。您仍然需要為連接查詢或存儲過程手動編寫SQL和對象代碼。
MyBatis Generator下載
1.源碼地址: https://github.com/mybatis/generator/releases
2.官方文檔: http://www.mybatis.org/generator/index.html
下面看下mybatis generator代碼生成器的使用,開始結(jié)構(gòu)圖如下:
maven文件引入
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ding</groupId> <artifactId>mybatis_generator</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!--配置文件的位置--> <configurationFile> src/main/resources/generatorConfig.xml </configurationFile> </configuration> </plugin> </plugins> </build> </project>
數(shù)據(jù)庫
| Create Table | | -------------------------------------------------- | | CREATE TABLE `student` ( | | `id` int(11) NOT NULL AUTO_INCREMENT, | | `NAME` varchar(20) DEFAULT NULL, | | `age` int(11) DEFAULT NULL, | | PRIMARY KEY (`id`) | | ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=u |
編寫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> <!--數(shù)據(jù)庫驅(qū)動寫自己jar包的位置--> <classPathEntry location="D:\develop\apache-maven-3.6.1\mvn_repository\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar"/> <!-- 一個數(shù)據(jù)庫一個context --> <context id="MYTables" targetRuntime="MyBatis3"> <commentGenerator> <!--suppressDate:**阻止**生成的注釋包含時間戳--> <property name="suppressDate" value="true"/> <!--suppressAllComments:**阻止**生成注釋--> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--數(shù)據(jù)庫鏈接地址賬號密碼--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/db1" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <!--控制是否強(qiáng)制DECIMAL和NUMERIC類型的字段轉(zhuǎn)換為Java類型的 java.math.BigDecimal--> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成Model類存放位置--> <javaModelGenerator targetPackage="com.ding.pojo" targetProject="src\main\java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="mapper" targetProject="src\main\resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成Dao類存放位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.ding.dao" targetProject="src\main\java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--生成對應(yīng)表及類名--> <table tableName="student" domainObjectName="student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context> </generatorConfiguration>
生成
生成后的結(jié)構(gòu)圖
世界不會因為你的疲憊,而停下它的腳步
到此這篇關(guān)于mybatis generator代碼生成器的使用的文章就介紹到這了,更多相關(guān)mybatis generator代碼生成器內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://www.cnblogs.com/dss-99/p/15312779.html