好久没写了,随便写点东西,不然脑袋都得大掉,还希望大家有空多讨论啊!!!
对看守者模式,进行了简单的使用,结合工厂,管理工厂在内存中的使用
ObjectKeeper.java
package com.legion.project.crmproject.common; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; /** * 看守类的一个实现 * @author dsy * @date 2008-12-03 */ public class ObjectKeeper extends Thread { private static final Logger log = Logger.getLogger(ObjectKeeper.class.getName()); private static ObjectKeeper m_keeper = new ObjectKeeper(); private static List m_keptObjects = new ArrayList(); private ObjectKeeper() { new Thread(this).start(); } public void run() { try { join(); } catch (InterruptedException e) { } } /** * 任何对象传递到看守方法,直到调用discardObject()方法 */ public static void keepObject(Object myObject) { log.info("[ObjectKeeper]-[keepObject()] begin Total number of kept objects: "+ m_keptObjects.size()); m_keptObjects.add(myObject); log.info("[ObjectKeeper]-[keepObject()] success Total number of kept objects: "+ m_keptObjects.size()); } /** * 清除当前被看守的对象 */ public static void discardObject(Object myObject) { log.info("[ObjectKeeper]-[discardObject()] begin Total number of kept objects: " + m_keptObjects.size()); m_keptObjects.remove(myObject); log.info("[ObjectKeeper]-[discardObject()] begin Total number of kept objects: " + m_keptObjects.size()); } }
UpdateBackServices.java
package com.legion.project.crmproject.common; import java.util.List; /** * <p>Title: 数据备份操作服务工具类</p> * * <p>Description: 用于备份数据查询,插入,删除操作</p> * * <p>Copyright: Copyright (c) 2008</p> * * <p>Company: Legion Technology</p> * * @author dsy * @version 1.0 * @date 2008-12-04 */ public class UpdateBackServices { /** * 删除公用方法 * @param dto * isJoinType = "DELETE"时,根据dto中的值,自动组装删除条件 * @param isJoinType * isJoinType = "DELETE"时,根据dto中参数是否有值来自动组装where * isJoinType = ""时,删除全部数据 * @return boolean */ public static boolean deleteAllByType(TCrmUpdatebakDTO dto,String isJoinType) { ObjectKeeper.keepObject(UpdateBackDAO.getInstance()); IUpdateBackDAO dao = UpdateBackDAO.getInstance(); boolean deleteBool = dao.deleteAllByType(dto, isJoinType); ObjectKeeper.discardObject(UpdateBackDAO.getInstance()); return deleteBool; } /** * 查询公用方法 * @param dto * isJoinType = "SELECT"时,根据dto中的值,自动组装查询条件 * @param isJoinType * isJoinType = "SELECT"时,根据dto中参数是否有值来自动组装where * isJoinType = ""时,查询全部数据 * @return List */ public static List searchAllByType(TCrmUpdatebakDTO dto,String isJoinType) { ObjectKeeper.keepObject(UpdateBackDAO.getInstance()); IUpdateBackDAO dao = UpdateBackDAO.getInstance(); List allValue = dao.searchAllByType(dto, isJoinType); ObjectKeeper.discardObject(UpdateBackDAO.getInstance()); return allValue; } /** * 插入公用方法 * @param dto * 以下值是不用设置的 * dto.setId();//序列自动生成 * dto.setBakdata();//自动根据dto.getBosid()备份模板外部表数据(XML) * dto.setStat(); //默认为"STAR" * @return */ public static boolean insertUpdateBAk(TCrmUpdatebakDTO dto) { ObjectKeeper.keepObject(UpdateBackDAO.getInstance()); IUpdateBackDAO dao = UpdateBackDAO.getInstance(); boolean insertBool = dao.insertUpdateBAk(dto); ObjectKeeper.discardObject(UpdateBackDAO.getInstance()); return insertBool; } public static void main(String[] args) { // getTableTypeSql(); TCrmUpdatebakDTO dto = new TCrmUpdatebakDTO(); dto.setBosid("88994"); dto.setBostype("test"); dto.setTitle("UpdateBack"); dto.setUpdatetype("UpdateBackDAO ADD"); dto.setBakdate(new java.util.Date()); dto.setStat("STAR"); //System.out.println(insertUpdateBAk(dto)); List testList = UpdateBackDAO.getInstance().searchAllByType(dto, ""); for (int i = 0; i < testList.size(); i++) { dto = new TCrmUpdatebakDTO(); dto = (TCrmUpdatebakDTO) testList.get(i); System.out.println(i + "=" +dto.getId()); } } }
SqlStringUtil.java
package com.legion.project.crmproject.common; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import org.apache.log4j.Logger; import com.legion.jdbc.test.ConnectionDBImpl; import common.dao.DAOFactory; /** * <p>Title: 数据操作工具类</p> * * <p>Description: 用于当前表字段组装,返回String</p> * * <p>Copyright: Copyright (c) 2008</p> * * <p>Company: Legion Technology</p> * * @author dsy * @version 1.0 * @date 2008-12-04 */ public class SqlStringUtil { private static final Logger log = Logger.getLogger(SqlStringUtil.class.getName()); protected static Connection getConnection() { Connection conn = null; try { //conn = DAOFactory.getInstance().getCrmConnection(); conn = ConnectionDBImpl.getConnection(); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * @desc: 用于查询当前实例[bosid]所关联模板数据的外部数据表工具方法 * @return */ public static String getBomSql(String bosid) { log.info("[SqlStringUtil].[getBomSql()] begin.................."); Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; String packageSql = ""; try { conn = getConnection(); pstmt = conn.prepareStatement(getSqlKey("BOMEXTTBL","",bosid)); rs = pstmt.executeQuery(); while(rs.next()) { String extTable = rs.getString(1); packageSql = getSqlKey("PACKAGETBL", extTable, bosid); } } catch (SQLException e) { log.error("[SqlStringUtil].[getBomSql()]={"+e.getMessage()+"}"); } finally { close(conn,pstmt); } log.info("[SqlStringUtil].[getBomSql()] success.................."); return packageSql; } protected static void close(Connection conn,PreparedStatement pstmt) { try { if(conn != null) { conn.close(); conn = null; } if(pstmt != null) { pstmt.close(); pstmt = null; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //TODO:BOMEXTTBL=获取模板外数据表名 //TODO:PACKAGETBL=需要打包的sql private static String getSqlKey(String key,String extTable, String bosid) { Map sqlValue = new HashMap(); StringBuffer bomExtTable = new StringBuffer(); bomExtTable.append("SELECT EXTTBL ").append("\n") .append("FROM T_BB_CBOBASE t, T_BB_BOMDES d").append("\n") .append("WHERE t.bosid = '").append(bosid).append("'").append("\n") .append("AND t.bomid = d.bomid").append("\n"); //TODO:此sql不能换行 StringBuffer packTable = new StringBuffer(); packTable.append("dbms_xmlquery.getXML('") .append("SELECT * ")//.append("\n") .append("FROM ").append(extTable).append(" T ")//.append("\n") .append("WHERE t.bosid = ''").append(bosid).append("''") .append("')");//.append("\n"); sqlValue.put("BOMEXTTBL", bomExtTable.toString()); sqlValue.put("PACKAGETBL", packTable.toString()); return (String)sqlValue.get(key); } public static void main(String[] args) { getBomSql("88994"); } }
IUpdateBackDAO.java
package com.legion.project.crmproject.common; import java.util.List; /** * <p>Title: 数据备份操作接口类</p> * * <p>Description: 用于备份数据查询,插入,删除操作</p> * * <p>Copyright: Copyright (c) 2008</p> * * <p>Company: Legion Technology</p> * * @author dsy * @version 1.0 * @date 2008-12-04 */ public interface IUpdateBackDAO { /** * 插入公用方法 * @param dto * 以下值是不用设置的 * dto.setId();//序列自动生成 * dto.setBakdata();//自动根据dto.getBosid()备份模板外部表数据(XML) * dto.setStat(); //默认为"STAR" * @return */ public abstract boolean insertUpdateBAk(TCrmUpdatebakDTO dto); /** * 查询公用方法 * @param dto * isJoinType = "SELECT"时,根据dto中的值,自动组装查询条件 * @param isJoinType * isJoinType = "SELECT"时,根据dto中参数是否有值来自动组装where * isJoinType = ""时,查询全部数据 * @return List */ public abstract List searchAllByType(TCrmUpdatebakDTO dto, String isJoinType); /** * 删除公用方法 * @param dto * isJoinType = "DELETE"时,根据dto中的值,自动组装删除条件 * @param isJoinType * isJoinType = "DELETE"时,根据dto中参数是否有值来自动组装where * isJoinType = ""时,删除全部数据 * @return boolean */ public abstract boolean deleteAllByType(TCrmUpdatebakDTO dto, String isJoinType); }
UpdateBackDAO.java
package com.legion.project.crmproject.common; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import com.legion.project.crmproject.common.BaseDao; /** * <p>Title: 数据备份操作工具实现类</p> * * <p>Description: 用于备份数据查询,插入,删除操作</p> * * <p>Copyright: Copyright (c) 2008</p> * * <p>Company: Legion Technology</p> * * @author dsy * @version 1.0 * @date 2008-12-04 */ public class UpdateBackDAO implements IUpdateBackDAO { private static final Logger log = Logger.getLogger(UpdateBackDAO.class.getName()); private static IUpdateBackDAO dao = null; public static IUpdateBackDAO getInstance() { if(dao == null) { dao = new UpdateBackDAO(); } return dao; } private static Connection getConnection() { Connection conn = null; try { //conn = DAOFactory.getInstance().getCrmConnection(); conn = SqlStringUtil.getConnection(); } catch (Exception e) { e.printStackTrace(); } return conn; } /* (non-Javadoc) * @see com.legion.backup.IUpdateBackDAO#insertUpdateBAk(com.legion.backup.TCrmUpdatebakDTO) */ public boolean insertUpdateBAk(TCrmUpdatebakDTO dto) { log.info("[UpdateBackDAO].[insertUpdateBAk()] begin.............."); boolean insertBool = false; Connection conn = null; PreparedStatement pstmt = null; String sql = genTableSql("INSERT",dto,""); conn = getConnection(); try { pstmt = conn.prepareStatement(sql); pstmt.setString(1, dto.getBosid()); pstmt.setString(2, dto.getBostype()); pstmt.setString(3, dto.getTitle()); pstmt.setString(4, dto.getUpdatetype()); pstmt.setDate(5, new java.sql.Date(dto.getBakdate().getTime())); pstmt.setString(6, "system"); pstmt.setString(7, ""); pstmt.setString(8, ""); pstmt.setString(9, "STAR"); insertBool = pstmt.executeUpdate() > 0 ? true : false; log.info("[UpdateBackDAO].[insertUpdateBAk()]={"+dto.getBosid()+"={"+SqlStringUtil.getBomSql(dto.getBosid())+"}}"); } catch (SQLException e) { log.error("[UpdateBackDAO].[insertUpdateBAk()]={"+dto.getBosid()+"={"+SqlStringUtil.getBomSql(dto.getBosid())+"}}"); try { conn.rollback(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } log.error("[UpdateBackDAO].[insertUpdateBAk()]" + e.getMessage()); } finally { close(conn,pstmt); } log.info("[UpdateBackDAO].[insertUpdateBAk()] success.............."); return insertBool; } /* (non-Javadoc) * @see com.legion.backup.IUpdateBackDAO#searchAllByType(com.legion.backup.TCrmUpdatebakDTO, java.lang.String) */ public List searchAllByType(TCrmUpdatebakDTO dto,String isJoinType) { log.info("[UpdateBackDAO].[searchAllByType()] begin.............."); List allValue = new ArrayList(); boolean insertBool = false; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; TCrmUpdatebakDTO setDto = null; String sql = genTableSql("SELECT",dto,isJoinType); try { conn = getConnection(); pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); log.info("[TCrmUpdatebakDTO].[setDTO()] begin.................."); while(rs.next()) { setDto = new TCrmUpdatebakDTO(); setDto.setDTO(rs); allValue.add(setDto); } log.info("[TCrmUpdatebakDTO].[setDTO()] success.................."); } catch (SQLException e) { log.info("[UpdateBackDAO].[searchAllByType()]={"+e.getMessage()+"}"); } finally { close(conn,pstmt); } log.info("[UpdateBackDAO].[searchAllByType()] success.............."); return allValue; } /* (non-Javadoc) * @see com.legion.backup.IUpdateBackDAO#deleteAllByType(com.legion.backup.TCrmUpdatebakDTO, java.lang.String) */ public boolean deleteAllByType(TCrmUpdatebakDTO dto,String isJoinType) { log.info("[UpdateBackDAO].[deleteAllByType()] begin.............."); List allValue = new ArrayList(); boolean deleteBool = false; Connection conn = null; PreparedStatement pstmt = null; String sql = genTableSql("DELETE",dto,isJoinType); try { conn = getConnection(); pstmt = conn.prepareStatement(sql); deleteBool = pstmt.executeUpdate() > 0 ? true : false; } catch (SQLException e) { log.info("[UpdateBackDAO].[deleteAllByType()]={"+e.getMessage()+"}"); } finally { close(conn,pstmt); } log.info("[UpdateBackDAO].[deleteAllByType()] success.............."); return deleteBool; } /* private static java.sql.Date transDate(java.util.Date utilDate) { java.sql.Date transDate = new java.sql.Date(utilDate.getTime()); return transDate; }*/ /* * 用于查询当前实例所有字段方法,并按一定的数据结构进行存储 * Object[0] = ID [NUMBER ]=ID * Object[1] = BOSID [VARCHAR2]=备份实例ID * Object[2] = BOSTYPE [VARCHAR2]=实例类型 * Object[3] = TITLE [VARCHAR2]=备份标题 * Object[4] = UPDATETYPE [VARCHAR2]=修改类型 * Object[5] = BAKDATA [CLOB ]=备份数据 * Object[6] = BAKDATE [DATE ]=备份日期 * Object[7] = BAKEMPID [VARCHAR2]=备份人 * Object[8] = SRESERVED1 [VARCHAR2]=字符预留1 * Object[9] = SRESERVED2 [VARCHAR2]=字符预留2 * Object[10] = STAT [VARCHAR2]=状态 */ private static Object[] getTableColumnSql() { List colValue = new ArrayList(); Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; String sql = "SELECT COLUMN_NAME FROM User_Tab_Cols WHERE table_name = 'T_CRM_UPDATEBAK'"; conn = getConnection(); try { pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()) { colValue.add(rs.getString(1)); } } catch (SQLException e) { //not happen } finally { close(conn, pstmt); } return (Object[])colValue.toArray(); } private static String genTableSql(String sqlType, TCrmUpdatebakDTO dto,String isJoinType) { Map sqlGen = new HashMap(); Object[] value = null; if(sqlType.equals("SELECT")||sqlType.equals("INSERT")) { value = getTableColumnSql(); sqlGen.put("SELECT", getSelectSql(value,dto,isJoinType)); sqlGen.put("INSERT", getInsertSql(value,dto.getBosid())); } if(sqlType.equals("DELETE")) { sqlGen.put("DELETE", getDeleteSql(dto,isJoinType)); } return (String) sqlGen.get(sqlType); } // TODO:拼装sql-->delete查询 private static String getDeleteSql(TCrmUpdatebakDTO dto, String isJoinType) { StringBuffer selectSql = new StringBuffer(); selectSql.append("DELETE FROM T_CRM_UPDATEBAK "); if(!isJoinType.equalsIgnoreCase("delete")) { return selectSql.toString(); } else { getWhereJoin(selectSql, "ID", dto.getId()); getWhereJoin(selectSql, "BOSID", dto.getBosid()); getWhereJoin(selectSql, "BOSTYPE", dto.getBostype()); getWhereJoin(selectSql, "TITLE", dto.getTitle()); getWhereJoin(selectSql, "BAKDATE", dto.getBakdate()); getWhereJoin(selectSql, "BAKEMPID", dto.getBakempid()); getWhereJoin(selectSql, "STAT", dto.getStat()); } return selectSql.toString(); } //TODO:拼装sql-->select查询 private static String getSelectSql(Object[] value, TCrmUpdatebakDTO dto, String isJoinType) { StringBuffer selectSql = new StringBuffer(); selectSql.append("SELECT "); for (int i = 0; i < value.length; i++) { if(i == 0) { selectSql.append(value[i]); } else { selectSql.append(",").append(value[i]); } } selectSql.append(" FROM T_CRM_UPDATEBAK "); if(!isJoinType.equalsIgnoreCase("query")) { return selectSql.toString(); } else { getWhereJoin(selectSql, "ID", dto.getId()); getWhereJoin(selectSql, "BOSID", dto.getBosid()); getWhereJoin(selectSql, "BOSTYPE", dto.getBostype()); getWhereJoin(selectSql, "TITLE", dto.getTitle()); getWhereJoin(selectSql, "BAKDATE", dto.getBakdate()); getWhereJoin(selectSql, "BAKEMPID", dto.getBakempid()); getWhereJoin(selectSql, "STAT", dto.getStat()); } return selectSql.toString(); } /** * String类型重载 * @param whereSql * @param columnName * @param whereKey */ private static void getWhereJoin(StringBuffer whereSql, String columnName, String whereKey) { if(whereKey != null&&!"".equals(whereKey) ) { checkWhereKey(whereSql); whereSql.append(columnName) .append("='") .append(whereKey) .append("' ").append("\n"); } } /** * Date关键字类型重载 * @param whereSql * @param columnName * @param whereKey */ private static void getWhereJoin(StringBuffer whereSql, String columnName, Date whereKey) { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); if(whereKey != null) { checkWhereKey(whereSql); whereSql.append("to_char(") .append(columnName) .append(",'yyyyMMdd')") .append("='") .append(format.format(whereKey)) .append("' ").append("\n"); } } /** * Long关键字类型重载 * @param whereSql * @param columnName * @param whereKey */ private static void getWhereJoin(StringBuffer whereSql, String columnName, Long whereKey) { if(whereKey != null) { checkWhereKey(whereSql); whereSql.append(columnName) .append("='") .append(whereKey) .append("' ").append("\n"); } } /** * 检测sql中是否有where关键,有则组装And * @param whereSql */ private static void checkWhereKey(StringBuffer whereSql) { if(whereSql.indexOf("WHERE") == -1) { whereSql.append("\n").append(" WHERE "); } else { whereSql.append(" AND "); } } // TODO:拼装sql-->Insert查询 private static String getInsertSql(Object[] value,String bosid) { StringBuffer insertSql = new StringBuffer(); insertSql.append("INSERT INTO T_CRM_UPDATEBAK("); StringBuffer splitSql = new StringBuffer(); for (int i = 0; i < value.length; i++) { if(i == 0) { insertSql.append(value[i]); splitSql.append("SEQ_T_COM_SYSLOGNUMB.nextval"); } else { String backData = (String) value[i]; if(backData.equalsIgnoreCase("BAKDATA")) { insertSql.append(",").append(value[i]); splitSql.append(",").append(SqlStringUtil.getBomSql(bosid)); } else { insertSql.append(",").append(value[i]); splitSql.append(",").append("?"); } } } insertSql.append(") ") .append("VALUES(") .append(splitSql.toString()) .append(")"); return insertSql.toString(); } protected static void close(Connection conn, PreparedStatement pstmt) { SqlStringUtil.close(conn, pstmt); } public static void main(String[] args) { //getTableTypeSql(); TCrmUpdatebakDTO dto = new TCrmUpdatebakDTO(); dto.setBosid("88994"); dto.setBostype("UpdateBackDAO test"); dto.setTitle("UpdateBackDAO"); dto.setUpdatetype("UpdateBackDAO ADD"); dto.setBakdate(new java.util.Date()); dto.setStat("STAR"); //System.out.println(insertUpdateBAk(dto)); UpdateBackDAO.getInstance().deleteAllByType(dto,""); // List testList = UpdateBackDAO.getInstance().searchAllByType(dto, ""); // for (int i = 0; i < testList.size(); i++) { // dto = new TCrmUpdatebakDTO(); // dto = (TCrmUpdatebakDTO) testList.get(i); // System.out.println(i + "=" +dto.getId()); // } System.out.println(); } }
TCrmUpdatebakDTO.java
package com.legion.project.crmproject.common; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Date; import org.apache.log4j.Logger; /** * <p>Title: TCrmUpdatebak entity.</p> * * <p>Description: 当前实例备份表</p> * * <p>Copyright: Copyright (c) 2008</p> * * <p>Company: Legion Technology</p> * * @author dsy * @version 1.0 * @date 2008-12-04 */ public class TCrmUpdatebakDTO implements java.io.Serializable { private static final Logger log = Logger.getLogger(TCrmUpdatebakDTO.class.getName()); private Long id; private String bosid = ""; private String bostype = ""; private String title = ""; private String updatetype = ""; private String bakdata = ""; private Date bakdate; private String bakempid = ""; private String sreserved1 = ""; private String sreserved2 = ""; private String stat = ""; public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getBosid() { return this.bosid; } public void setBosid(String bosid) { this.bosid = bosid; } public String getBostype() { return this.bostype; } public void setBostype(String bostype) { this.bostype = bostype; } public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } public String getUpdatetype() { return this.updatetype; } public void setUpdatetype(String updatetype) { this.updatetype = updatetype; } public String getBakdata() { return this.bakdata; } public void setBakdata(String bakdata) { this.bakdata = bakdata; } public Date getBakdate() { return this.bakdate; } public void setBakdate(Date bakdate) { this.bakdate = bakdate; } public String getBakempid() { return this.bakempid; } public void setBakempid(String bakempid) { this.bakempid = bakempid; } public String getSreserved1() { return this.sreserved1; } public void setSreserved1(String sreserved1) { this.sreserved1 = sreserved1; } public String getSreserved2() { return this.sreserved2; } public void setSreserved2(String sreserved2) { this.sreserved2 = sreserved2; } public String getStat() { return this.stat; } public void setStat(String stat) { this.stat = stat; } /* * 用于查询当前实例所有字段方法,并按一定的数据结构进行存储 * Object[0] = ID [NUMBER ]=ID * Object[1] = BOSID [VARCHAR2]=备份实例ID * Object[2] = BOSTYPE [VARCHAR2]=实例类型 * Object[3] = TITLE [VARCHAR2]=备份标题 * Object[4] = UPDATETYPE [VARCHAR2]=修改类型 * Object[5] = BAKDATA [CLOB ]=备份数据 * Object[6] = BAKDATE [DATE ]=备份日期 * Object[7] = BAKEMPID [VARCHAR2]=备份人 * Object[8] = SRESERVED1 [VARCHAR2]=字符预留1 * Object[9] = SRESERVED2 [VARCHAR2]=字符预留2 * Object[10] = STAT [VARCHAR2]=状态 */ public void setDTO(ResultSet rs) { try { this.setId(new Long(rs.getLong("ID"))); this.setBosid(rs.getString("BOSID")); this.setBostype(rs.getString("BOSTYPE")); this.setTitle(rs.getString("TITLE")); this.setUpdatetype(rs.getString("UPDATETYPE")); this.setBakdate(rs.getDate("BAKDATE")); this.setBakempid(rs.getString("BAKEMPID")); this.setStat(rs.getString("STAT")); } catch (SQLException e) { log.error("[TCrmUpdatebakDTO].[setDTO()]={"+e.getMessage()+"}"); } } }