daomethodinterceptor.java

来自「使用annotation封装的jdbc」· Java 代码 · 共 46 行

JAVA
46
字号
package cn.cja.dao;

import java.lang.reflect.Method;
import java.sql.Connection;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import cn.cja.sql.Query;
import cn.cja.sql.Update;

public class DAOMethodInterceptor implements MethodInterceptor{
	private IConnectionProvider connectionManager;
	
	public DAOMethodInterceptor(IConnectionProvider connectionManager) {
		this.connectionManager=connectionManager;
	}

	public Object intercept(Object object, Method method, Object[] params,
			MethodProxy proxy) throws Throwable {
		Query query=method.getAnnotation(Query.class);
		if(query!=null)
		{
			QueryController selectController=new QueryController();
			Class type=method.getReturnType();
			return selectController.query(query,connectionManager.get(),params,type,object);
		}
		Update update=method.getAnnotation(Update.class);
		if(update!=null)
		{
			UpdateController updateController=new UpdateController();
			Class type=method.getReturnType();
			if(type.equals(Integer.class)||type.equals(int.class))
			{
				return updateController.update(update,connectionManager.get(),params);
			}
			else
			{
				updateController.update(update,connectionManager.get(),params);
				return null;
			}
		}
		return null;
	}
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?