·java代码:
package com.oa.common.table.dao;
import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils; import org.springframework.orm.ibatis.SqlMapClientTemplate;
/** * ZSGCAction.java * <p>[类说明:增删改差的公共类]</p> * @author: wangsheng * @create: Mar 17, 2012 8:38:07 AM */ public class CommonProcessTableDaoImpl extends SqlMapClientTemplate implements ICommonProcessTableDao {
/** * <p>[动态查看传入的表名是否存在]</p> * @param tablename * @return * @return: Boolean true:表存在;false:表不存在。 * @author: wangsheng * @update: [Mar 18, 2012 8:29:19 AM] [更改人姓名][变更描述] */ public Boolean confirmTableExists(String tablename) { if(null==tablename) return false;
Map map = new HashMap(); map.put("tablename", tablename); Integer tablecount = (Integer)queryForObject("dynamic.select.table.exists",map);
return (null!=tablecount&&1==tablecount)?true:false; } /** * <p>[动态获取表中的最大行标识]</p> * @param tablename * @return * @return: Integer * @author: wangsheng * @update: [Mar 18, 2012 9:15:13 AM] [更改人姓名][变更描述] */ public synchronized Integer getMaxHangBiaoShiDynamic(String tablename) { if(!confirmTableExists(tablename)) return null;
HashMap<String,String> map = new HashMap<String,String>(); map.put("hangbiaoshi", "hangbiaoshi"); map.put("tablename", tablename); Integer value = (Integer)queryForObject("dynamic.select.table.maxhangbiaoshi",map); return value; }
/** * <p>[动态批量根据行标识删除数据]</p> * @param tablename * @param hangbiaoshis * @return * @return: Integer * @author: wangsheng * @update: [Mar 18, 2012 9:15:32 AM] [更改人姓名][变更描述] */ public Integer deleteTableDynamic(String tablename, List<Integer> hangbiaoshis) { if(!confirmTableExists(tablename) || null==hangbiaoshis) return null;
Map map = new HashMap(); map.put("tablename", tablename); map.put("hangbiaoshis",hangbiaoshis);
Integer row = this.delete("dynamic.delete.table.batch.byhangbiaoshi",map); return row; }
/** * <p>[动态普通方法功能中文描述]</p> * @param tableDTO * @param tablename * @return * @return: Integer * @author: wangsheng * @update: [Mar 18, 2012 9:15:51 AM] [更改人姓名][变更描述] */ public Integer updatTableDynamic(Object tableDTO,String tablename) { try { if(!confirmTableExists(tablename)) return null;
Map<String,Object> map = PropertyUtils.describe(tableDTO); Iterator<String> key = map.keySet().iterator(); List fieldlist = new ArrayList(); Integer hangbiaoshi = null; while(key.hasNext()){ String keyProperty = key.next(); Object valueProperty = PropertyUtils.getProperty(tableDTO,keyProperty); if(keyProperty.equals("hangbiaoshi")||keyProperty.equals("hangBiaoShi")) hangbiaoshi = (Integer)valueProperty; UpdateTableParameterDTO dto = new UpdateTableParameterDTO(); dto.setTableKey(keyProperty); dto.setTableValue(valueProperty); if(dto.getTableValue()!=null && !dto.getTableKey().equals("class")) fieldlist.add(dto);
} Map tableMap = new HashMap(); tableMap.put("tablename", tablename); tableMap.put("hangbiaoshi",hangbiaoshi); //更新条件 tableMap.put("fieldlist", fieldlist); Integer updatecount = this.update("dynamic.update.table.hangbiaoshi",tableMap);
return updatecount; } catch (Exception e) { e.printStackTrace(); return null; } }
/** * <p>[动态插入表信息]</p> * @param tableDTO * @param tablename * @return * @return: Integer * @author: wangsheng * @update: [Mar 18, 2012 9:16:03 AM] [更改人姓名][变更描述] */ public Integer insertTableDynamic(Object tableDTO,String tablename) { try { if(!confirmTableExists(tablename)) return null;
Map<String,Object> map = PropertyUtils.describe(tableDTO); Iterator<String> key = map.keySet().iterator(); List fieldlist = new ArrayList(); while(key.hasNext()){ String keyProperty = key.next(); Object valueProperty = PropertyUtils.getProperty(tableDTO,keyProperty); if(keyProperty.equals("hangbiaoshi")||keyProperty.equals("hangBiaoShi")) { Integer maxhangbiaoshi = getMaxHangBiaoShiDynamic(tablename); if(null==maxhangbiaoshi) return null; valueProperty = maxhangbiaoshi; } UpdateTableParameterDTO dto = new UpdateTableParameterDTO(); dto.setTableKey(keyProperty); dto.setTableValue(valueProperty); if(dto.getTableValue()!=null && !dto.getTableKey().equals("class")) fieldlist.add(dto);
}
Map tableMap = new HashMap(); tableMap.put("tablename", tablename); tableMap.put("fieldlist", fieldlist); Integer insertcount = this.update("dynamic.insert.table",tableMap);
return insertcount; } catch (Exception e) { e.printStackTrace(); return null; } }
}
/** * CommonProcessTableDaoImpl.java * <p>[类说明:]</p> * @author: wangsheng * @create: Mar 18, 2012 8:07:59 AM */ class UpdateTableParameterDTO {
/** * <p>[要更新的字段]</p> *@author wangsheng *@update: Mar 18, 2012 8:00:40 AM */ public String tableKey;
/** * <p>[要更新的值]</p> *@author wangsheng *@update: Mar 18, 2012 8:05:07 AM */ public Object tableValue;
public String getTableKey() { return tableKey; } public void setTableKey(String tableKey) { this.tableKey = tableKey; } public Object getTableValue() { return tableValue; } public void setTableValue(Object tableValue) { this.tableValue = tableValue; }
} |
·ibatis代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" > <sqlMap namespace="GZW">
<select id="dynamic.select.table.exists" parameterClass="java.util.Map" resultClass="int"> select count(*) from gzwdb.dbo.sysobjects WHERE NAME=#tablename# AND TYPE='U' </select>
<select id="dynamic.select.table.maxhangbiaoshi" parameterClass="java.util.Map" resultClass="int"> select max(hangbiaoshi)+1 from $tablename$ </select>
<delete id="dynamic.delete.table.batch.byhangbiaoshi" parameterClass="java.util.Map"> DELETE FROM $tablename$ WHERE HANGBIAOSHI IN <iterate property="hangbiaoshis" open="(" close=")" conjunction=","> #hangbiaoshis[]# </iterate> </delete>
<insert id="dynamic.insert.table" parameterClass="java.util.Map">
INSERT INTO $tablename$ <iterate property="fieldlist" open="(" close=")" conjunction=","> $fieldlist[].tableKey$ </iterate> VALUES <iterate property="fieldlist" open="(" close=")" conjunction=","> '$fieldlist[].tableValue$' </iterate> </insert>
<update id="dynamic.update.table.hangbiaoshi" parameterClass="java.util.Map"> update $tablename$ set <iterate property="fieldlist" conjunction=","> $fieldlist[].tableKey$ = '$fieldlist[].tableValue$' </iterate> where hangbiaoshi = #hangbiaoshi# </update>
</sqlMap> |