1. 程式人生 > >mybatis自動生成mapping檔案

mybatis自動生成mapping檔案

老的經典框架像ssh ssm ssmm,資料傳輸層用hibernate和mabatis的屬於比較流行的,hibernate的對映檔案有幾種生成方式,這裡主要結束mabatis對映檔案的生成方式:

1、環境

     資料庫:MariaDB(就是mysql)

     開發工具:springsts-3.9.0(版本啟動需要jdk1.8,如果環境變數不是1.8,自己設定一下init檔案的虛擬機器環境,設定成1.8)

 2、只結束兩種簡單的方式

(1)、下載mybatis-generator-core-1.3.2.jar和mysql-connector-java-5.1.35.jar放到一個檔案中,新建一個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="E:\software\SSMM\CmdGenerator\mysql-connector-java-5.1.35.jar" />
	<!--<classPathEntry location="xxxxx\ojdbc14.jar" /> -->
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		
		<!-- 資料庫連結URL、使用者名稱、密碼 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://*。*。*。*:*/*" userId="*" password="*"> 
		<!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="xe" password="xe">-->
		</jdbcConnection>
		
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		
		<!-- 生成模型的包名和位置 -->
		<javaModelGenerator targetPackage="*.model" targetProject="E:\**\src">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		
		<!-- 生成的對映檔案包名和位置 -->
		<sqlMapGenerator targetPackage="*.mapping" targetProject="E:\**\src">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		
		<!-- 生成DAO的包名和位置 -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="*.dao" targetProject="E:\**\src">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		
		<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
		<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
		<table tableName="user_role" domainObjectName="UserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
		<table tableName="role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
		<table tableName="role_permission" domainObjectName="RolePermission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
		<table tableName="permission" domainObjectName="Permission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
	</context>
</generatorConfiguration>
很簡單,從上往下就是根據資料庫驅動包連線你要連線的資料庫,生成名字是model、mapping、dao的資料夾,存在在targetProject指向的絕對路徑下,並且根據資料庫表名實體類的對映;tableName->domainObjectName

第二部進入dos命令,執行java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

目錄結構如下:


然後再從src下分別複製出類檔案放到專案中

(2)第二種更簡單,直接在spring-sts ->help ->eclipse marketplace 搜尋mybatis

點選install,建立一個maven專案,右鍵新建->others->



建立一個generatorConfig.xml檔案,裡面內容和上面那個檔案一樣,複製過來把絕對路徑改成專案的絕對路徑就行,然後右鍵general mybatis/ibatis artifacts,完事了。

每次新建一個數據庫表,增刪查改可以通過這兩種方式生成,剩下的多表關聯還是要自己寫語句,mybatis簡單,就是寫sql語句。