天天看點

如何使用Mybatis-Generator自動生成代碼如何使用Mybatis-Generator自動生成代碼

如何使用Mybatis-Generator自動生成代碼

目前很流行SSM架構的使用,在用到mybatis時,dao、mapping、model代碼可以用mybatis的自動生成工具生成。下面就介紹2種生成的方式。

方法1 使用windows CMD指令執行

  • 通路 mybatis-generator的下載下傳位址 mybatis
  • 選擇一個版本【mybatis-generator-1.3.5.zip】進行下載下傳。
  • 下載下傳後解壓,得到一個【mybatis-generator-core-1.3.5.jar】檔案
  • 把這個檔案和 【mysql-connector-java-5.1.40.jar】放在同一檔案夾下
注意:檔案夾請不要使用中文,否則會亂碼導緻生成失敗。
  • xml配置檔案的内容可以參照 官網的sample
<?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:\mybatis-generator\mysql-connector-java-5.1.40.jar" />
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressDate" value="true" />
			<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!--資料庫連結URL,使用者名、密碼 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://ws-test-97/mybatis" userId="test"
			password="Password">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!-- 生成模型的包名和位置 -->
		<javaModelGenerator targetPackage="cn.redsoft.com.vo"
			targetProject="MyBatisGenerator" >
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!-- 生成映射檔案的包名和位置 -->
		<sqlMapGenerator targetPackage="cn.redsoft.com.mapping"
			targetProject="MyBatisGenerator" >
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!-- 生成DAO的包名和位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="cn.redsoft.com.dao" targetProject="MyBatisGenerator" >
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!-- 要生成的表 tableName是資料庫中的表名或視圖名 domainObjectName是實體類名 -->
		<table tableName="t_userlistdaliy" domainObjectName="UserListDaliy"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			selectByExampleQueryId="false"></table>
		<table tableName="t_userlistdiff" domainObjectName="UserListDiff"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			selectByExampleQueryId="false"></table>
	</context>
</generatorConfiguration>    
           
  • 在jar的所在目錄下運作CMD執行以下的指令
java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite
           
  • 就可以在指定的目錄下找到生成的代碼了,把這些生成的内容複制到你的工程中即可。
注意:如果提示targetproject:MyBatisGenerator不存在,需要手動建立一個這樣的目錄在C:\mybatis-generator\下。

方法2 使用eclipse的mybatis-generator插件執行

  • 下載下傳eclipse的插件,比如org.mybatis.generator.eclipse.site-1.3.7.201807042148.zip,解壓後重新開機eclipse。
  • 打開eclipse,New→Other→MyBatis Generator Configuration File建立上文中的XML配置檔案。
  • 然後在generatorConfig.xml上右鍵→Run As→Run Mybatis Generator,就可以在目前project下生成對應的package和檔案。