面试准备--持久层框架

      在说到持久层框架,耳熟能详的有Hibernate和iBatis,他们最简单的区别是一个全自动的,一个是半自动的。但是归根结底他们的实现原理都差不多,他们都是引用了C3P0数据库连接池,使用ibatis 提供的orm机制,对业务逻辑实现人员而言,面对的是纯粹的java对象。这一层与通过hibernate 实现orm 而言基本一致,而对于具体的数据操作,hibernate会自动生成sql 语句,而ibatis 则要求开发者编写具体的sql 语句。相对hibernate而言,ibatis 以sql开发的工作量和数据库移植性上的让步,为系统设计提供了更大的自由空间。 

     以下是本人模仿写的持久层框架

     当用户的业务逻辑处理中,需要对数据库进行操作,依照Hibernate的经验,我们需要自定义一些条件带入,所以这里创建了2个类:Criteria.java负责查询,Handler.java负责修改数据。

  

package com.qj.engine.sqlmap;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qj.engine.session.Session;
import com.qj.engine.session.SessionImpl;
import com.qj.engine.session.exception.SessionException;
import com.qj.engine.sqlmap.util.FilterUtil;


public class Criteria {
    private String tableName="";
    private int custom = 0;
    private Map<String, String> filterMap = new HashMap<String, String>();
    private Map<String, String> orderMap = new HashMap<String, String>();
    private Session session = new SessionImpl();
    public Criteria(String tableName) {
        this.tableName = tableName;
    }
    /**
     *    添加过滤条件 
     */
    public void filter(CriteriaTypes type, String colName, Object value){
        String filterStr = FilterUtil.getFilterStr(type, colName, value);
        this.filterMap.put(colName, filterStr);
    }
    /**
     *    添加过滤条件 between and 
     */
    public void filter(CriteriaTypes type, String colName, Object val1, Object val2){
        String filterStr = FilterUtil.getFilterStr(type, colName, val1, val2);
        this.filterMap.put(colName, filterStr);
    }
    /**
     *    添加过滤条件
     *    @param filterStr:自定义过滤条件
     */
    public void filter(String filterStr){
        this.custom=this.custom+1;
        this.filterMap.put("CUSTOM"+this.custom, filterStr);
    }
    /**
     * 添加排序条件
     * @param type
     * @param colName
     */
    public void order(OrderTypes type, String colName){
        this.orderMap.put(colName, colName + " " + type.toString());
    }
    /**
     * 查询一列数据,以Map形式返回
     * @return
     */
    public Map<String, Object> executeQueryForMap(){
        String querySql = FilterUtil.getQuerySQL(this.tableName, this.filterMap, this.orderMap);
        Map<String, Object> resultMap = null;
        try {
            resultMap = this.session.executeQueryForMap(querySql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SessionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }
    /**
     * 查询数据列表,list子集是以Map形式返回
     * @return
     */
    public List<Map<String, Object>> executeQueryForMapList(){
        String querySql = FilterUtil.getQuerySQL(this.tableName, this.filterMap, this.orderMap);
        List<Map<String, Object>> resultList = null;
        try {
            resultList = this.session.executeQueryForMapList(querySql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SessionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultList;
    }
    /**
     * 查询数据列表,以Object形式返回
     * @param cls
     * @return
     */
    public Object executeQueryForObject(Class<?> cls){
        String querySql = FilterUtil.getQuerySQL(this.tableName, this.filterMap, this.orderMap);
        Object obj = null;
        try {
            obj = this.session.executeQueryForObject(querySql, cls);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SessionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return obj;
    }
    /**
     * 查询数据列表,list子集是以Object形式返回
     * @param cls
     * @return
     */
    public List<Object> executeQueryForList(Class<?> cls){
        String querySql = FilterUtil.getQuerySQL(this.tableName, this.filterMap, this.orderMap);
        List<Object> objList = null;
        try {
            objList = this.session.executeQueryForList(querySql, cls);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SessionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return objList;
    }
    /**
     * 执行存储过程
     * @param procName 存储过程名称
     * @param map 传递的参数列表, 如key:in; value: param
     * @return 执行结果
     */
    public Object executeProcessQueryForObject(String procName, Map<String, Object> map){
        Object result = null;
        try {
            result = this.session.executeProcessQueryForObject(procName, map);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SessionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 执行存储过程
     * @param procName 存储过程名称
     * @param map 传递的参数列表, 如key:in; value: param
     * @return 执行结果
     */
    public Object[] executeProcessQueryForList(String procName, Map<String, Object> map){
        Object[] results = null;
        try {
            results = this.session.executeProcessQueryForList(procName, map);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SessionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return results;
    }
}
package com.qj.engine.sqlmap;

import java.sql.SQLException;
import java.util.Map;

import com.qj.engine.session.Session;
import com.qj.engine.session.SessionImpl;
import com.qj.engine.session.exception.SessionException;
import com.qj.engine.sqlmap.exception.SqlMappingException;
import com.qj.engine.sqlmap.util.HandlerUtil;
/**
 * 
 * @author QJ
 *
 */
public class Handler {
    private String tableName="";
    private Session session = new SessionImpl();
    public Handler(String tableName) {
        this.tableName = tableName;
    }
    /**
     * 插入
     * @param pkRow
     * @param pkVal
     * @param valsMap
     * @return
     */
    public boolean insert(String pkRow, Object pkVal, Map<String, Object> valsMap){
        if(this.tableName.equals("")){
            try {
                throw new SqlMappingException("表的名称没有设定");
            } catch (SqlMappingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        String insertSql = HandlerUtil.getDmlSql(HandlerTypes.INSERT,
                this.tableName, pkRow, pkVal, valsMap);
        boolean result = false;
        try {
            result = session.insert(insertSql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SessionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 修改
     * @param pkRow
     * @param pkVal
     * @param valsMap
     * @return
     */
    public boolean update(String pkRow, Object pkVal, Map<String, Object> valsMap){
        if(this.tableName.equals("")){
            try {
                throw new SqlMappingException("表的名称没有设定");
            } catch (SqlMappingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        String updateSql = HandlerUtil.getDmlSql(HandlerTypes.UPDATE,
                this.tableName, pkRow, pkVal, valsMap);
        boolean result = false;
        try {
            result = session.insert(updateSql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SessionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 删除
     * @param pkRow
     * @param pkVal
     * @return
     */
    public boolean delete(String pkRow, Object pkVal){
        if(this.tableName.equals("")){
            try {
                throw new SqlMappingException("表的名称没有设定");
            } catch (SqlMappingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        String deleteSql = HandlerUtil.getDmlSql(HandlerTypes.DELETE,
                this.tableName, pkRow, pkVal, null);
        boolean result = false;
        try {
            result = session.insert(deleteSql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SessionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 自定义DML sql 
     * @param userDefinedSql
     * @return
     */
    public boolean userDefinedSql(String userDefinedSql){
        boolean result = false;
        try {
            result = session.insert(userDefinedSql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SessionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 执行存储过程
     * @param procName
     * @param map
     * @return
     */
    public boolean executeProcess(String procName, Map<String, Object> map){
        boolean result = false;
        try {
            result = this.session.executeProcess(procName, map);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SessionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
    
}

 

 

posted on 2014-02-10 11:13  it_code  阅读(815)  评论(0编辑  收藏  举报