日期:2014-05-16  浏览次数:20536 次

实现通用的PreparedStatement更新记录的方法
        在JadePool中,除了常规的更新方法之外,还有以下更新记录的方法,

           1、public int update(String tableName, Map<String, Object> mapRecord, String where) throws SQLException//根据条件更新单条记录
           2、public int update(String tableName, Map<String, Object> mapRecord) throws SQLException//更新单条记录
           3、public int update(String tableName, List<Map> listRecord) throws SQLException  //更新多条记录,主键值不变
           4、public int update(String tableName, List<Map> listRecord, String where) throws SQLException   //根据where条件,更新多条记录,主键值不变

        它们都调用底层最核心的方法
            private int _preparedStatementUpdate(String tableName, List<Map> listRecord, boolean isUpdateKey, String whereStr) throws SQLException

        将使用参数Map变量或者是List<Map>变量更新数据库表中的记录,待更新的记录的条件是主键值等于mapRecord.get(主键名),在这一组操作中,主键值不变。

        底层核心方法_preparedStatementUpdate同__preparedStatementInsert的共同点有以下几点:
           1、插入语句或更新语句只需要合成一次;
           2、都支持相同的SQL数据类型及其对应的Java类;
           3、都需要实现相同的通配符对应的赋值方法(参见“SQL数据类型与Java类的对应关系”一文);
           4、都整合相同的异常,统一抛出SQLException异常。

        底层核心方法_preparedStatementUpdate同__preparedStatementInsert的区别在于:
           1、合成PreparedStatement的更新语句;
           2、更新数据时,保持主键值不变。

        合成PreparedStatement的更新语句的算法是

        Map<String, Object> _m = new LinkedHashMap(listRecord.get(0));//获取一条记录,作为过滤、分组依据
        String[] tableFields = db.getFields(tableName);//表中的字段
        String[] tableKeys = db.getKeys(tableName);//表中的主键
        Object[] recordFields = (_m.keySet()).toArray(); //获取记录里的字段名的集合
        for (int i = 0; i < recordFields.length; i++) {
            if (!tool.isInFields(tableFields, recordFields[i].toString())) {
                _m.remove(recordFields[i].toString());//移除无效字段, 查看记录中的字段在表中是否存在,如果不存在,则移除到
            }
        }
        Object[] k0 = (_m.keySet()).toArray(); //过滤后的有效字段
        Map<String, Object> key_m = new LinkedHashMap();//记录里的主键
        if (!isUpdateKey) {
            for (int i = 0; i < k0.length; i++) {
                if (tool.isInFields(tableKeys, k0[i].toString())) {//记录中是否有主键
                    key_m.put(k0[i].toString(), _m.remove(k0[i].toString()));//将记录中的主键移到key_m中;保证不对主键更新
                }
            }
        }
        Object[] fields = (_m.keySet()).toArray(); //记录中不包含主键的有效字段;再次过滤掉主键字段的结果
        Object[] keys = (key_m.keySet()).toArray(); //记录中包含的主键


        if (isUpdateKey) {
            if (keys.length == 0 || keys.length != tableKeys.length) {
                return num;
            }
        }


        String[] kss = new String[fields.length]; //保存"键名=?"
        for (int i = 0; i < fields.length; i++) {
            kss[i] = fields[i].toString() + "=?";
        }

        String n_v = tool.arryToString(kss, ",");

        String preparedStatementUpdate = "update " + tableName + " set " + n_v + " ";
        //System.out.println(preparedStatementUpdate);


        完整的_preparedStatementUpdate代码

    /**
     * 使用PreparedStatement更新多条记录 本方法是根据JDBC API类名的判断