詳解mybatis generator代碼生成器的使用
MyBatis Generator簡介
MyBatis Generator(MBG)是MyBatis MyBatis 和iBATIS的代碼生成器。它將為所有版本的MyBatis以及版本2.2.0之后的iBATIS版本生成代碼。它將內(nèi)省數(shù)據(jù)庫表(或許多表),并將生成可用于訪問表的工件。這減少了設置對象和配置文件以與數(shù)據(jù)庫表交互的初始麻煩。MBG尋求對簡單CRUD(創(chuàng)建,檢索,更新,刪除)的大部分數(shù)據(jù)庫操作產(chǎn)生重大影響。您仍然需要為連接查詢或存儲過程手動編寫SQL和對象代碼。
MyBatis Generator下載
1.源碼地址: https://github.com/mybatis/generator/releases
2.官方文檔: http://www.mybatis.org/generator/index.html
下面看下mybatis generator代碼生成器的使用,開始結構圖如下:

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ù)庫驅動寫自己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>
<!--控制是否強制DECIMAL和NUMERIC類型的字段轉換為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>
<!--生成對應表及類名-->
<table tableName="student" domainObjectName="student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
生成

生成后的結構圖

世界不會因為你的疲憊,而停下它的腳步
到此這篇關于mybatis generator代碼生成器的使用的文章就介紹到這了,更多相關mybatis generator代碼生成器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MyEclipse2018中安裝Mybatis generator插件的實現(xiàn)步驟
這篇文章主要介紹了MyEclipse2018中安裝Mybatis generator插件的實現(xiàn)步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
java的MybatisPlus調用儲存過程的返回數(shù)據(jù)問題
這篇文章主要介紹了java的MybatisPlus調用儲存過程的返回數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

