`
amosleaf
  • 浏览: 59521 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Mybatis generator 使用

阅读更多

声明:本文感谢satellite同学。

====================================

先看Generator,另外generator启动方式还有命令行 和 ant 方式等。

package mybatis.generator;

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 Generator {

	public static void main(String[] args) {
		List<String> warnings = new ArrayList<String>();
		ConfigurationParser cp = new ConfigurationParser(warnings);

		boolean overwrite = true;
		//staticTableConfig.xml,dynamicTableConfig.xml
		File configFile = new File(
				"E:/javaspace/svnspace/WCM5.0/PublishServer/tools/generator/dynamicTableConfig.xml");		
		try {
			Configuration config = cp.parseConfiguration(configFile);
			DefaultShellCallback callback = new DefaultShellCallback(overwrite);
			MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
					callback, warnings);
			myBatisGenerator.generate(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 接下来看Plugin,Plugin用于生成的Java代码改造 和 生成的sql改造,具体请参见Mybatis 文档。

参考链接:

http://www.mybatis.org/
http://www.mybatis.org/java.html
http://code.google.com/p/mybatis

 

package mybatis.generator;

import java.util.List;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Element;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

public class MyPlugin extends PluginAdapter {

	@Override
	public boolean validate(List<String> arg0) {
		return true;
	}

	@Override
	public boolean sqlMapSelectByPrimaryKeyElementGenerated(XmlElement element,
			IntrospectedTable introspectedTable) {
		element.getAttributes().remove(2);
		element.addAttribute(new Attribute("parameterType", introspectedTable
				.getBaseRecordType()));
		return super.sqlMapSelectByPrimaryKeyElementGenerated(element,
				introspectedTable);
	}

	@Override
	public boolean sqlMapDeleteByPrimaryKeyElementGenerated(XmlElement element,
			IntrospectedTable introspectedTable) {
		element.getAttributes().remove(1);
		element.addAttribute(new Attribute("parameterType", introspectedTable
				.getBaseRecordType()));
		return super.sqlMapDeleteByPrimaryKeyElementGenerated(element,
				introspectedTable);
	}

	@Override
	public boolean clientDeleteByPrimaryKeyMethodGenerated(Method method,
			Interface interfaze, IntrospectedTable introspectedTable) {
		method.getParameters().remove(0);
		String type = introspectedTable.getBaseRecordType();
		method.addParameter(new Parameter(new FullyQualifiedJavaType(type),
				type.substring(type.lastIndexOf(".") + 1).toLowerCase()));
		return super.clientDeleteByPrimaryKeyMethodGenerated(method, interfaze,
				introspectedTable);
	}

	@Override
	public boolean clientSelectByPrimaryKeyMethodGenerated(Method method,
			Interface interfaze, IntrospectedTable introspectedTable) {
		method.getParameters().remove(0);
		String type = introspectedTable.getBaseRecordType();
		method.addParameter(new Parameter(new FullyQualifiedJavaType(type),
				type.substring(type.lastIndexOf(".") + 1).toLowerCase()));
		return super.clientSelectByPrimaryKeyMethodGenerated(method, interfaze,
				introspectedTable);
	}

	@Override
	public boolean sqlMapUpdateByExampleSelectiveElementGenerated(
			XmlElement element, IntrospectedTable introspectedTable) {
		element.getElements().remove(0);
		StringBuilder sb = new StringBuilder();
		sb.append("update ");
		sb.append(introspectedTable
				.getAliasedFullyQualifiedTableNameAtRuntime().replace(
						"${tablesite}", "${record.tablesite}"));
		element.addElement(0, new TextElement(sb.toString()));
		return super.sqlMapUpdateByExampleSelectiveElementGenerated(element,
				introspectedTable);
	}

	@Override
	public boolean sqlMapUpdateByExampleWithoutBLOBsElementGenerated(
			XmlElement element, IntrospectedTable introspectedTable) {
		element.getElements().remove(0);
		StringBuilder sb = new StringBuilder();
		sb.append("update ");
		sb.append(introspectedTable
				.getAliasedFullyQualifiedTableNameAtRuntime().replace(
						"${tablesite}", "${record.tablesite}"));
		element.addElement(0, new TextElement(sb.toString()));
		return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element,
				introspectedTable);
	}

	@Override
	public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
			IntrospectedTable introspectedTable) {
		addTableSite(topLevelClass, introspectedTable, "tablesite");
		return super.modelBaseRecordClassGenerated(topLevelClass,
				introspectedTable);
	}

	@Override
	public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
			IntrospectedTable introspectedTable) {
		addTableSite(topLevelClass, introspectedTable, "tablesite");
		return super.modelExampleClassGenerated(topLevelClass,
				introspectedTable);
	}

	private void addTableSite(TopLevelClass topLevelClass,
			IntrospectedTable introspectedTable, String name) {
		CommentGenerator commentGenerator = context.getCommentGenerator();
		Field field = new Field();
		field.setVisibility(JavaVisibility.PROTECTED);
		field.setType(FullyQualifiedJavaType.getIntInstance());
		field.setName(name);
		field.setInitializationString("-1");
		commentGenerator.addFieldComment(field, introspectedTable);
		topLevelClass.addField(field);
		char c = name.charAt(0);
		String camel = Character.toUpperCase(c) + name.substring(1);
		Method method = new Method();
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setName("set" + camel);
		method.addParameter(new Parameter(FullyQualifiedJavaType
				.getIntInstance(), name));
		method.addBodyLine("this." + name + "=" + name + ";");
		commentGenerator.addGeneralMethodComment(method, introspectedTable);
		topLevelClass.addMethod(method);
		method = new Method();
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setReturnType(FullyQualifiedJavaType.getIntInstance());
		method.setName("get" + camel);
		method.addBodyLine("return " + name + ";");
		commentGenerator.addGeneralMethodComment(method, introspectedTable);
		topLevelClass.addMethod(method);
	}

	@Override
	public boolean sqlMapBaseColumnListElementGenerated(XmlElement element,
			IntrospectedTable introspectedTable) {
		List<Element> elist = element.getElements();
		int size = elist.size();
		Element e = elist.get(size - 1);
		String str = e.getFormattedContent(0);
		e = new TextElement(str + ", ${tablesite} AS tablesite");
		elist.remove(size - 1);
		elist.add(size - 1, e);
		return super.sqlMapBaseColumnListElementGenerated(element,
				introspectedTable);
	}

	@Override
	public boolean sqlMapResultMapWithoutBLOBsElementGenerated(
			XmlElement element, IntrospectedTable introspectedTable) {
		List<Element> elist = element.getElements();
		XmlElement xe = new XmlElement("result");
		xe.addAttribute(new Attribute("column", "tablesite"));
		xe.addAttribute(new Attribute("property", "tablesite"));
		xe.addAttribute(new Attribute("jdbcType", "INTEGER"));
		elist.add(xe);
		return super.sqlMapResultMapWithoutBLOBsElementGenerated(element,
				introspectedTable);
	}

	@Override
	public boolean sqlMapResultMapWithBLOBsElementGenerated(XmlElement element,
			IntrospectedTable introspectedTable) {
		List<Element> elist = element.getElements();
		XmlElement xe = new XmlElement("result");
		xe.addAttribute(new Attribute("column", "tablesite"));
		xe.addAttribute(new Attribute("property", "tablesite"));
		xe.addAttribute(new Attribute("jdbcType", "INTEGER"));
		elist.add(xe);
		return super.sqlMapResultMapWithoutBLOBsElementGenerated(element,
				introspectedTable);
	}
	
	
}

 

另外配置文件,请参见附件,包括静态表 和 动态表两种。

分享到:
评论
1 楼 caiyunlong520 2012-04-23  

求大侠帮助解决一个小问题:
我在用mybatis generator 自动生成代码的时候怎样指定table中的PrimaryKey主键
并且让生成的SqlMapper文件中有
selectByPrimaryKeyQueryId
enableSelectByPrimaryKey
enableDeleteByPrimaryKey
这几个方法,我的表标签如下:
  <table tableName="t_ep_ent_circlewarn_his" domainObjectName="CircleWarnBean"
         enableCountByExample="false" enableUpdateByExample="false" 
          enableDeleteByExample="false" enableSelectByExample="false" 
         selectByExampleQueryId="false" selectByPrimaryKeyQueryId="true"
         enableSelectByPrimaryKey="true" enableDeleteByPrimaryKey="true"
         delimitIdentifiers="warnid"
         >
</table>

相关推荐

Global site tag (gtag.js) - Google Analytics