傳送門
使用這個插件可以快速生成一些代碼,包含 實體類/Mapper接口/*Mapper.xml文件
首先,我們需要搭建一個Maven
的項目。
在pom.xml
中添加代碼
<plugins>
<plugin>
<!--Mybatis-generator插件,用於自動生成Mapper和POJO-->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>yourLocation/mybatis-generator-config.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
注意,plugins
標簽是build
標簽的子標簽
添加好之后,我們就需要配置mybatis-generator-config.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>
<!-- 本地數據庫驅動程序jar包的全路徑 -->
<classPathEntry location=""/>
<context id="context" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- 數據庫的相關配置 -->
<jdbcConnection driverClass="" connectionURL="" userId="" password=""/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 實體類生成的位置 -->
<javaModelGenerator targetPackage="目標包" targetProject="目標項目classpath">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- *Mapper.xml 文件的位置 -->
<sqlMapGenerator targetPackage="目標包" targetProject="目標項目classpath">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- Mapper 接口文件的位置 -->
<javaClientGenerator targetPackage="目標包" targetProject="目標項目classpath" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 相關表的配置 -->
<table tableName="表名" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
隨后,在idea
的右側欄點擊Maven,選中添加的Mybatis-generator插件並運行,就可以得到相應的代碼啦~
是不是很簡單啊?
那就快動手試試吧!
至於mybatis-generator
的詳細說明,可以參考參考文檔