MapperPlugin 添加Mapper到Mapper接口上

package info.qiuranke.mybatis.generator.plugins;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.TopLevelClass;

public class MapperPlugin extends PluginAdapter {

	private final Collection<Annotations> annotations;

	public MapperPlugin() {
		annotations = new HashSet<Annotations>(Annotations.values().length);
	}

	public boolean validate(List<String> warnings) {
		return true;
	}

	@Override
	public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass,
			IntrospectedTable introspectedTable) {
		super.clientGenerated(interfaze, topLevelClass, introspectedTable);
		addDataAnnotation(interfaze);
		return true;
	}

	private void addDataAnnotation(Interface interfaze) {
		for (Annotations annotation : annotations) {
			interfaze.addImportedType(annotation.javaType);
			interfaze.addAnnotation(annotation.name);
		}
	}

	@Override
	public void setProperties(Properties properties) {
		super.setProperties(properties);
		for (Entry<Object, Object> entry : properties.entrySet()) {
			boolean isEnable = Boolean.parseBoolean(entry.getValue().toString());
			if (isEnable) {
				String paramName = entry.getKey().toString().trim();
				Annotations annotation = Annotations.getValueOf(paramName);
				if (annotation != null) {
					annotations.add(annotation);
				}
			}
		}
	}

	private enum Annotations {
		DATA("mapper", "@Mapper", "org.apache.ibatis.annotations.Mapper");

		private final String paramName;
		private final String name;
		private final FullyQualifiedJavaType javaType;

		Annotations(String paramName, String name, String className) {
			this.paramName = paramName;
			this.name = name;
			this.javaType = new FullyQualifiedJavaType(className);
		}

		private static Annotations getValueOf(String paramName) {
			for (Annotations annotation : Annotations.values())
				if (String.CASE_INSENSITIVE_ORDER.compare(paramName, annotation.paramName) == 0)
					return annotation;

			return null;
		}
	}
}

转载于:https://my.oschina.net/IBM/blog/843931

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值