結合hibernate與mybatis各自優勢自定義web應用擴展(三)

接上java

上一篇中完成了根據bean自動生成mybatis所需的inter和xml,同時指出了其中的問題,本篇將解決多字段操做時的繁瑣問題sql

觀察數據庫操做行爲發現不管是何種操做,都在增刪改查四種行爲以內數據庫

命令模式能很好的解決這個問題mybatis

要點:爲適應多類型,本篇將大量使用反射機制,上轉型與下轉型ide

1,新建接口autoOrder.javaui

package plug.gg.commonsAuto;

import login.bean.user;

import java.util.List;

public interface autoOrder {
    Object execute(Object object);
}

2,新建命令請求類this

package plug.gg.commonsAuto;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

public class autoBeanMap {
    /***
     * 命令模式請求類
     * */
    private Map beanMap=new LinkedHashMap();
/*得到map名*/
    public String[] getBeanName(){
        Set<String> set=beanMap.keySet();
        String[] name=new String[beanMap.size()];
        int x=0;
        for(String key:set){
            name[x]=key;
            x++;
        }
        x=0;
        return name;
    }
/*得到map值*/
    public String[] getBeanValue(){
        Set<String> set=beanMap.keySet();
        String[] value=new String[beanMap.size()];
        int x=0;
        for(String key:set){

            value[x]=beanMap.get(key).toString();
            x++;
        }
        x=0;
        return value;
    }

    /*
    * 參數設置
    * 參數轉換 obj->map (字段名:值)
    *
    * */
    public autoBeanMap(Object obj){
        try {
            for (int i=0;i<obj.getClass().getMethods().length;i++) {
                if(obj.getClass().getMethods()[i].getName().indexOf("get")>=0){
                    if(obj.getClass().getMethods()[i].invoke(obj)!=null){
                         beanMap.put(obj.getClass().getMethods()[i].getName(),obj.getClass().getMethods()[i].invoke(obj));
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /*
    * add方法  參數object爲mybatis裏注入的inter對象
    * */
    public void  add(Object object){
        String uuid = null;
        try {
/*得到惟一標識*/
            for(int i=0;i<getBeanName().length;i++){
                if(getBeanName()[i].toLowerCase().indexOf("uuid")>=0){
                    uuid=getBeanValue()[i];
                    continue;
                }
            }
/*根據惟一標識查詢*/
            for(int i=0;i<getBeanName().length;i++){
                if(getBeanName()[i].toLowerCase().indexOf("uuid")>=0){
/*根據當前時間戳生成uuid*/
                    uuid=new ceaterTime().ceater();
/*執行mybatis裏inter裏定義的add方法*/
                    object.getClass().getMethod("add", String.class, String.class).invoke(object,"uuid", uuid);
                    continue;
                }else {
/*由於xml裏的基本sql只能插入一個值,所以第一個值是插入,以後的值是更新*/
                    if (getBeanName()[i].equals("getClass")) {
                        continue;
                    } else {
                        object.getClass().getMethod("update", String.class, String.class, String.class, String.class).invoke(object, getBeanName()[i].replace("get", "").toLowerCase(), getBeanValue()[i], "uuid", uuid);
                        System.out.println(getBeanName()[i].replace("get", "").toLowerCase()+":"+getBeanValue()[i]);
                   }
                }
            }

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
    /*
    * delete
    * */
/*根據惟一標示刪除*/
    public Object delete(Object object){
        try {
            return  object.getClass().getMethod("delete",String.class,String.class).invoke(object,getBeanName()[0].replace("get","").toLowerCase(),getBeanValue()[0]);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }
    /*
    *update
    */
/*調用屢次更新方法*/
    public void update(Object object){
        String uuid = null;
        try {
            for(int i=0;i<getBeanName().length;i++){
                if(getBeanName()[i].toLowerCase().indexOf("uuid")>=0){
                    uuid=getBeanValue()[i];
                    continue;
                }
            }
            for(int i=0;i<getBeanName().length;i++){
                if(getBeanName()[i].toLowerCase().indexOf("uuid")>=0){
                    uuid=getBeanValue()[i];
                    continue;
                }else {
                    if (getBeanName()[i].equals("getClass")) {
                        continue;
                    } else {
                        object.getClass().getMethod("update", String.class, String.class, String.class, String.class).invoke(object, getBeanName()[i].replace("get", "").toLowerCase(), getBeanValue()[i], "uuid", uuid);
                        System.out.println(getBeanName()[i].replace("get", "").toLowerCase()+":"+getBeanValue()[i]);

                    }
                }
            }

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

    }
    /*
    *
    * select
    * */
/*根據惟一標識查詢*/
    public Object select(Object object){
        try {
            return  object.getClass().getMethod("isSelect",String.class,String.class).invoke(object,getBeanName()[0].replace("get","").toLowerCase(),getBeanValue()[0]);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }
    /*
    * selectAll
    *
    * */
/*查詢全部*/
    public List selectAll(Object object){
        try {
            return (List) object.getClass().getMethod("allSelect").invoke(object);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

}

3,新建add方法所須要的時間戳調試

package plug.gg.commonsAuto;

import java.util.Date;

public class ceaterTime {
	public String ceater(){
		
        String date2=String.valueOf(new Date().getTime());
        return date2;
	}
	public String ceaterString(){
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        Date currentTime = new Date();
        String date= formatter.format(currentTime);
        return date;
	}
}

4,實現接口的實體類code

addorm

package plug.gg.commonsAuto;

import login.bean.user;

import java.util.List;

public class autoAdd implements autoOrder {
    private autoBeanMap beanMap;
    public void setBeanMap(autoBeanMap beanMap){
        this.beanMap=beanMap;
    }
    @Override
    public Object execute(Object object) {
         beanMap.add(object);
         return null;
    }
}

delete

package plug.gg.commonsAuto;

import login.bean.user;

import java.util.List;

public class autoDelete implements autoOrder {
    private autoBeanMap beanMap;
    public void setBeanMap(autoBeanMap beanMap){
        this.beanMap=beanMap;
    }
    @Override
    public Object execute(Object object) {

        return beanMap.delete(object);
    }
}

update
 

package plug.gg.commonsAuto;

import login.bean.user;

import java.util.List;

public class autoUpdate implements autoOrder{
    private autoBeanMap beanMap;
    public void setBeanMap(autoBeanMap beanMap){
        this.beanMap=beanMap;
    }
    @Override
    public Object execute(Object object) {
        beanMap.update(object);
        return null;
    }
}

seelet

package plug.gg.commonsAuto;

import login.bean.user;

import java.util.List;

public class autoSelect implements autoOrder {
    private autoBeanMap beanMap;
    public void setBeanMap(autoBeanMap beanMap){
        this.beanMap=beanMap;
    }
    @Override
    public Object execute(Object object) {
        return beanMap.select(object);
    }
}

selectAll

package plug.gg.commonsAuto;

import login.bean.user;

import java.util.List;

public class autoSelectAll implements autoOrder{
    private autoBeanMap beanMap;
    public void setBeanMap(autoBeanMap beanMap){
        this.beanMap=beanMap;
    }

    @Override
    public List execute(Object object) {
        return beanMap.selectAll(object);
    }
}

5,新建命令調用類

package plug.gg.commonsAuto;

public class autoBroker {
    private autoBeanMap beanMap;
    public autoBroker(autoBeanMap beanMap){
        this.beanMap=beanMap;
    }
    public autoOrder order(String order){
        if(order.equals("add")){
            autoAdd add=new autoAdd();
            add.setBeanMap(beanMap);
            return add;
        }
        if(order.equals("delete")){
            autoDelete delete=new autoDelete();
            delete.setBeanMap(beanMap);
            return delete;
        }
        if(order.equals("update")){
            autoUpdate update=new autoUpdate();
            update.setBeanMap(beanMap);
            return update;
        }
        if(order.equals("select")){
            autoSelect select=new autoSelect();
            select.setBeanMap(beanMap);
            return select;
        }
        if(order.equals("selectAll")){
            autoSelectAll selectAll=new autoSelectAll();
            selectAll.setBeanMap(beanMap);
            return selectAll;
        }
        return null;
    }
}

6,步驟控制類

package plug.gg.commonsAuto;

public class autoController {
    public Object getAuto(Object bean,String orders,Object dao){
        autoBeanMap beanMap=new autoBeanMap(bean);
        autoBroker broker=new autoBroker(beanMap);


        autoOrder order=broker.order(orders);
        return  order.execute(dao);

    }
}

7,目錄

8,調試

9,結果

10,成功!

至此最初構想已經基本完成

回顧一下整個過程

1,新建一個bean,會自動根據這個bean生成新建的sql語句

2,沒必要理會mybatis所須要的inter和xml配置,會自動根據bean生成相應的xml好intere

3,沒必要理會具體的sql語句,只需傳入一個bean對象,dao對象和,增刪改查命令,一行代碼解決全部問題

如今使用mybatis是否是就比之前更加快速的完成一個數據庫操做了呢

下一篇將以uml類圖的形式概述整個的結構與分析

相關文章
相關標籤/搜索