4. 자신 이 직접 쓴 ORM 프레임 워 크 의 대리 가 SQL 문 구 를 실행 합 니 다.

3450 단어 자바
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("    ");
        }
    }
}

좋은 웹페이지 즐겨찾기