1. 程式人生 > >使用maven和mybatis自動生成程式碼配置

使用maven和mybatis自動生成程式碼配置

使用自動生成真的很方便,之前試過手寫,但是無奈太慢而且容易出錯,一急之下馬上去 研究了下,沒想到2個小時不到就研究出來了,廢話不多說,我們正式開始,

我自己的個人網站就用到了這種技術

如果需要生成資料庫欄位的註釋,請下載:點我開啟 連結內的檔案,裡面有詳細教程

    準備工作:

          1、下載 mysql-connector-java-5.0.8-bin.jar 驅動包,點我下載,如果沒有分數的話建議去官網或者百度找,很多的,因為我這裡用的是mysql資料庫,所以我下載的mysql 的驅動包, 其他資料庫自行下載對於的驅動包

          2、maven環境的專案

 一、 一般情況下,我們的專案都是模組化程式設計的,我的mybatis的資料層就放在common層裡面如下圖,在 src/main/resources 目錄源下建立一個 generatorConfig.xml 檔案,

 二、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>
	<!--資料庫驅動 ,這裡你們需要自行修改自己的jar包路徑-->
	<classPathEntry
		location="H:/repository/mysql-connector-java-5.0.8-bin.jar" />
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressDate" value="true" />
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!--資料庫連結地址賬號密碼 ,這裡你們需要自己修改-->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://xxx.xxx.xxx.xxx/xd_love" userId="root"
			password="123456">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!--生成Model類存放位置 -->
		<javaModelGenerator targetPackage="com.maven.common.model"
			targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!--生成對映檔案存放位置 -->
		<sqlMapGenerator targetPackage="sqlMapper"
			targetProject="src/main/resources">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!--生成Dao類存放位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.maven.common.mapper" targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!--生成對應表及類名 tableName對應你的表名,domainObjectName對應你生成的檔案字首名-->
		<table tableName="symphony_address" domainObjectName="symphonyAddress"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			selectByExampleQueryId="false"></table>
	</context>
</generatorConfiguration>

三、在pom.xml檔案中新增外掛

<build>
	<finalName><!--你的專案名稱--></finalName>
	<pluginManagement>
		<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><!--配置檔案路徑 -->
					<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>
	</pluginManagement>
</build>

四、右擊專案-->Run As-->Maven Build

在Goals一欄輸入:mybatis-generator:generate -e,然後點選Run之後就會自動生成程式碼了

五、生成程式碼後的目錄

-------------------------------------我是分割線-------------------------------------------------------

後續:如果出現了錯誤Project build error: Non-resolvable parent POM for com.maven:xxx-common:0.0.1-SNAPSHOT: Could not find artifact com.maven:xxx-parent:pom:0.0.1-  SNAPSHOT and 'parent.relativePath' points at wrong local POM

出現這個錯誤是因為父工程沒有註冊,右鍵parent 專案 -run as - maven install  即可解決