4、自己手写ORM框架之代理实现SQL语句执行

package com.framework.betterorm.proxy;


import com.framework.betterorm.Datesource.AbstractDataBaseOperationFactory;
import com.framework.betterorm.annotation.Delete;
import com.framework.betterorm.annotation.Insert;
import com.framework.betterorm.annotation.Select;
import com.framework.betterorm.annotation.Update;
import com.framework.betterorm.common.CommonSingleton;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author libi
 * 用于动态代理,获取方法的参数并且给返回值
 */
public class InvocationHandlerOrm implements InvocationHandler {

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("method:" + method);
        System.out.println("arg.length:" + args.length);
        Object object = null;
        //获取此时实例化的类
        String clz = CommonSingleton.getInstance().dataBaseOperateStrategy(AbstractDataBaseOperationFactory.databaseType);
        Class clazz = Class.forName(clz);
        System.out.println("clazz:" + clazz);
        Select select = method.getAnnotation(Select.class);
        if (select != null) {
            System.out.println("select"+select);
            System.out.println("select方法被代理");
            Method selectMethod = clazz.getMethod("select", String.class, Object.class);
            System.out.println("selectMethod:" + selectMethod);
            object = selectMethod.invoke(clazz.newInstance(), select.value()[0], args[0]);
        } else {
            Annotation[] annotations=method.getDeclaredAnnotations();
            System.out.println("annotations"+annotations[0]);
            Insert insert = method.getAnnotation(Insert.class);
            Update update = method.getAnnotation(Update.class);
            Delete delete = method.getAnnotation(Delete.class);
            String value = "";
            if (insert != null) {
                value = insert.value()[0];
            }
            if (update != null) {
                value = update.value()[0];
            }
            if (delete != null) {
                value = delete.value()[0];
            }
            System.out.println("update方法被代理");
            Method selectMethod = clazz.getMethod("update", String.class, Object.class);
            System.out.println("value:"+value);
            object = selectMethod.invoke(clazz.newInstance(), value, args[0]);
        }
        return object;
    }
}

 

测试类如下

import com.framework.betterorm.session.SqlSession;
import com.framework.betterorm.test.User;
import com.framework.betterorm.test.UserDao;

public class Test {
    @org.junit.Test
    public void select() {
        UserDao userDao= SqlSession.getMapper(UserDao.class);
        User user=new User();
        user.setUserName("test1");
        User user1 = userDao.selectUser(user);
        System.out.println("test:"+user1.toString());
    }
    @org.junit.Test
    public void update() {
        UserDao userDao= SqlSession.getMapper(UserDao.class);
        User user=new User();
        user.setUserName("test1");
        user.setPassword("test");
        int flag = userDao.updateInfo(user);
        if(flag>0){
            System.out.println("修改成功");
        }
    }
}

 

你可能感兴趣的:(java)