MyBatisGenerator 自動生成java代碼(反向工具)

1、引入jar:mybatis-generator-core-1.3.1-bundle.zip


2、編寫主類,程序入口:

package mybatis;

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

import junit.framework.TestCase;

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;

/**
 * mybatis反向工具,可以使用此生成所需代碼。
 * 
 * @author dongjian
 * */
public class MyBatisGeneratorTest extends TestCase {
	
	private String confFilePath = "generatorConfig.xml";

	public final void testGenerator() throws Exception {
		
		List<String> warnings = new ArrayList<String>();
		
		confFilePath = this.getClass().getResource(confFilePath).getFile();
		
		File configFile = new File(confFilePath);
		
		Configuration config = new ConfigurationParser(warnings).parseConfiguration(configFile);
		
		DefaultShellCallback callback = new DefaultShellCallback(true);
		
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		
		myBatisGenerator.generate(null);
	}
	
}

3、編寫插件,用於生成hashCode和equals方法:

package mybatis.plugin;

import java.util.List;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.plugins.EqualsHashCodePlugin;

/**
 * 生成器插件,用於生成Equals和HashCode方法
 * 
 * @author dongjian
 *
 */
public class MyEqualsHashCodePlugin extends EqualsHashCodePlugin {
		@Override
	    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
	            IntrospectedTable introspectedTable) {
		 	return modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
	    }

	    @Override
	    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
	            IntrospectedTable introspectedTable) {
	    	List<IntrospectedColumn> columns = introspectedTable.getPrimaryKeyColumns();
	    	if(columns == null || columns.size() == 0){
	    		columns = introspectedTable.getAllColumns();
	    	}
	    	
	        generateEquals(topLevelClass, columns, introspectedTable);
	        generateHashCode(topLevelClass, columns, introspectedTable);

	        return true;
	    }

	    @Override
	    public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
	        return modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
	    }
}

4、編寫配置文件(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>

	<context id="tables" targetRuntime="MyBatis3">
        <plugin type="mybatis.plugin.MyEqualsHashCodePlugin"></plugin>
		<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
		
		<!-- import mapper -->
		<plugin type="org.mybatis.generator.plugins.MapperConfigPlugin">
			<property name="fileName" value="GeneratedMapperConfig.xml" />
			<property name="targetPackage" value="mapper" />
			<property name="targetProject" value="C:\code\resources\" />
		</plugin>
		
		<!-- 是否去除自動生成的註釋 -->
		<commentGenerator>
            <property name="suppressAllComments" value="false" />
            <property name="suppressDate" value="false" />
		</commentGenerator>
		
		<!-- jdbc連接配置 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver" 
			connectionURL="jdbc:mysql://localhost:3306/dbname"
			userId="root" password="root">
		</jdbcConnection>
		
        <!-- java類型配置 -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- generate Model -->
		<javaModelGenerator targetPackage="com.jd.spinsidesales.domain.po" 
			targetProject="C:\code\java">
			<property name="enableSubPackages" value="true" />
		</javaModelGenerator>

		<!-- generate xml -->
		<sqlMapGenerator targetPackage="sqlmap.mybatis.mysql" 
			targetProject="C:\code\resources\">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>

		<!-- generate dao -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="com.jd.spinsidesales.dao" 
			targetProject="C:\code\java">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		
		<table tableName="approve_log" domainObjectName="ApproveLog" 
			enableCountByExample="false" enableSelectByExample="false" 
			enableDeleteByExample="false" enableUpdateByExample="false"></table>
		
	</context>
</generatorConfiguration>  

5、建立文件夾

C:\code\resources\

C:\code\java


6、使用Junit運行MyBatisGeneratorTest類,您當然可以使用main方法來運行,這無關緊要,運行之後就可以看見生成的java類了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章