Mybatis Generator 自動生成數據庫XML(Mybatis Generator 逆向工程)

Mybatis Generator 逆向工程自動生成XML


1.MyBatis Generator簡單介紹:

(1)MyBatis Generator 會生成: Dao,Model,Mapping基礎文件。
(2)基礎文件解決了對數據庫操作有最大影響的一些簡單的CRUD(插入,查詢,更新,刪除)操作。
3MyBatis/iBATIS 兼容 SQL 映射 XML 文件。
(4)存儲過程調用直接調用方法名。
(5)將文件導入工程就可以使用,新增的按照基礎格式改裝下即可。



2.配置前需要的jar包(本人使用版本是):

 mybatis-generator-core-1.3.2.jar
 mysql-connector-java-5.1.6-bin.jar


3.導入包後,創建一個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="lib/mysql-connector-java-5.1.6-bin.jar"/>
	<context id="Mysql"	defaultModelType="flat"  targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressDate" value="true"/>
			<!-- 是否去除自動生成註釋 -->
			<property name="suppressAllComments" value="true"/>
			
		</commentGenerator>
		
	    <!-- 連接MySql驅動 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false"/>
		</javaTypeResolver>
		
		
		<javaModelGenerator targetPackage="com.lmx.model" targetProject="src">
			<property name="enableSubPackages" value="true"/>
			<property name="trimStrings" value="true"/>
		</javaModelGenerator>
		
		<sqlMapGenerator targetPackage="com.lmx.mapping" targetProject="src">
			<property name="enableSubPackages" value="true"/>
		</sqlMapGenerator>
		
		<javaClientGenerator type="XMLMAPPER" targetPackage="com.lmx.dao" targetProject="src">
			<property name="enableSubPackages" value="true"/>
		</javaClientGenerator>
		
		 <!-- 生成表  SQL通配符匹配多個表     enableXXX=false  ByExample不生成  -->
       <table tableName="%" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <generatedKey column="id" sqlStatement="Mysql" />
        </table>
	  
	
	</context>
</generatorConfiguration>


4.通過main方法啓動逆向工程生成工程(網上調用的教程也有很多):

package com.lmx.main;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;


public class GeneratorDb {

	
	void dbRun() throws Exception{			
		    List<String> warnings = new ArrayList<String>();  
	        boolean overwrite = true;  
	        File configFile = new File("config/generatorConfig.xml");   
	        ConfigurationParser cp = new ConfigurationParser(warnings);  
	        Configuration config = cp.parseConfiguration(configFile);  
	        DefaultShellCallback callback = new DefaultShellCallback(overwrite);  
	        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);  
	        myBatisGenerator.generate(null);
	}
	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		GeneratorDb gd = new GeneratorDb();
		gd.dbRun();
		
	}

}

結果:




下載示例:http://download.csdn.net/detail/liangmaoxuan/9738211


總結不好多多擔待,文章只單純個人總結,如不好勿噴,技術有限,有錯漏麻煩指正提出。本人QQ:373965070

發佈了49 篇原創文章 · 獲贊 79 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章