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类了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值