天天看點

Eclipse內建Mybatis Generator及應用

  說明:xxxPo—xxxPoMapper—xxPoMapper.xml 都是典型的體力活,好在有很多插件可以幫我們去完成這些複制粘貼的工作,這裡就從兩方面1、Eclipse內建mybatis generator的方式 2、mybatis generator逆向操作及應用 來介紹一下mybatis的插件。

一、Eclipse內建Mybatis Generator插件

  Eclipse內建插件一般都有兩種方式,一種是線上安裝,一種是離線,根據自己習慣安裝就可以了。

1.1、線上安裝

  打開eclipse中help->Eclipse Marketplace輸入mybatis generator打開,然後install跟着提示安裝就行,記得重新開機。

Eclipse內建Mybatis Generator及應用

1.2、離線安裝

  連結:https://pan.baidu.com/s/1PMiMaDzBKquISRtuCRe1IQ

  提取碼:nyk9

  将上連結中的包下載下傳,解壓第一層壓縮裡面共有7個jar,COPY到eclipse安裝目錄下的plugins下,重新開機eclipse就可以了。

  若在file->new->other中看到下圖說明安裝成功。

Eclipse內建Mybatis Generator及應用

二、maven內建插件

  在pom.xml引入插件配置,注意裡面generatorConfig.xml配置檔案引入路徑(下面會有完成的pom.xml配置)。

<!-- Mybatis-Generator插件,自動生成代碼 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>${project.basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
           

  基本上就是這麼簡單,簡單配置一下就OK了。

三、應用內建

Eclipse內建Mybatis Generator及應用

  說明:generatorConfig.xml為核心配置檔案,com.dyc下建了三個空的包路徑,用來存放生成的相應的逆向生成的檔案。

3.1、pom.xml檔案

<?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.dyc</groupId>
	<artifactId>mybatis-generator-test</artifactId>
	<version>1.0</version>

	<packaging>jar</packaging>

	<name>mybatis-generator-test</name>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
		<relativePath />
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.7</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- mybatis -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.2.0</version>
		</dependency>

		<!-- mysql -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

		<!--thymeleaf -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
			
			<!-- Mybatis-Generator插件,自動生成代碼 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>${project.basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
		</plugins>
	</build>


	<description>mybatis-generator-test</description>
</project>

           

3.2、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>
	<!-- 資料庫驅動 -->
	<classPathEntry
		location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar" />
	<context id="test-monitor" targetRuntime="MyBatis3">
		<!-- 生成的pojo,生成equals和hashCode -->
		<!-- <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" /> -->
		<!-- 生成的pojo,将implements Serializable -->
		<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
		<!-- 該插件給實體類添加toString() -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
		<commentGenerator>
			<property name="suppressDate" value="true" />
			<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!-- 資料庫配置 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://10.100.1.236:3306/antispider-monitor?useUnicode=true&amp;characterEncoding=UTF-8" userId="root" password="root">
		</jdbcConnection>
		<!--生成POJO類存放位置 -->
		<javaModelGenerator targetPackage="com.dyc.pojo" targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!--生成映射關系 -->
		<sqlMapGenerator targetPackage="com.dyc.sqlmap" targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!--生成Mapper -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="com.dyc.mapper" targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!-- 生成對應的表及類名 -->
		<!-- 下面不生成xxxExample對象 -->
		<table tableName="t_user" domainObjectName="UserPo"
			enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
			<property name="ignoreQualifiersAtRuntime" value="true" />
			<generatedKey column="ID" sqlStatement="MySql" identity="true" />
			<columnOverride column="ID" javaType="java.lang.Long" />
		</table>
	</context>
</generatorConfiguration> 
           

  提示:如果基于eclipse插件配置該檔案時,targetProject可以指定項目名稱,但基于maven插件時略有差別,指向工程名會找不到路徑。

  說明:關于table标簽中的,幾個enableXXXByExample的屬性,如果打開多生成一個Example結尾的對象,這個對象可以支援更多更複雜的操作(比如分頁,多條件組合查詢等,一般我都會關掉,生成的代碼幹淨些),詳細可以到mybatis官網了解。

3.3、運作

  1、基于eclipse插件的,直接在generatorConfig.xml右鍵,點Generate MyBatis/iBATIS Artifacts 便可。

  2、基于maven插件的,在工程上右鍵Run Configurations打開視窗,在Goals中輸入mybatis-generator:generate –e 然後點run便可。

  結果輸出如下,在UserPoMapper中生成了常用的操作方法,非常友善。

Eclipse內建Mybatis Generator及應用

4、打包下載下傳

https://pan.baidu.com/s/1HkGGx263rWV0vMPJA8ZbYw 
           

  若連結莫名失效,可以留言聯系,也可以按照上述的方式自行配置。

Eclipse內建Mybatis Generator及應用