- viwo
- 等级: 初级会员
- 性别:
- 文章: 37
- 积分: 94
- 来自: 大连
|
java 代码
- package com.viwo.sqlmap.config;
-
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import org.apache.commons.beanutils.BeanUtils;
- import org.apache.commons.digester.Digester;
-
- import org.apache.log4j.Logger;
-
-
- import com.viwo.sqlmap.factory.ConnectionFactory;
-
-
-
-
-
-
-
- public class SqlMapConfig
- {
- private final static Logger logger = Logger.getLogger(SqlMapConfig.class);
- private Map selectMap = new HashMap();
- private Map executeMap = new HashMap();
- private static Map configMap = new HashMap();
- private Connection connection = null;
- private Statement statement = null;
-
-
-
-
-
-
-
-
-
-
-
-
-
- public List QueryForList(String selectId,Object paramObject) throws Exception
- {
- logger.debug("[viwo]-->[enter QueryForList]");
- List list = new ArrayList();
-
- try
- {
- ConfigSelect configSelect =(ConfigSelect)selectMap.get(selectId);
- if(configSelect!=null)
- {
- String sql = configSelect.getSql();
- String type = configSelect.getType();
- String param = configSelect.getParam();
- if(paramObject.getClass() != Class.forName(param))
- {
- return null;
- }
- String[] sqls = sql.split("#");
- if(sqls.length>1)
- {
- for(int i=0;i {
- if(i%2!=0)
- {
- String tmpValue = BeanUtils.getProperty(paramObject, sqls[i]);
- sql = sql.replaceFirst("#"+sqls[i]+"#", "'"+tmpValue+"'");
- }
- }
- }
- logger.debug("[viwo]-->[QueryForList]-->[sql:]"+sql);
-
- ResultSet rs = statement.executeQuery(sql);
- ResultSetMetaData meta = rs.getMetaData();
- int columnCount = meta.getColumnCount();
- while(rs.next())
- {
- Object newItem = Class.forName(type).newInstance();
- for(int i=1;i<=columnCount;i++)
- {
- Object rsItem = null;
- String columnType = meta.getColumnTypeName(i);
- if(columnType.equals("numeric"))
- rsItem = new Double(rs.getDouble(meta.getColumnName(i)));
- else if(columnType.equals("int"))
- rsItem = new Integer(rs.getInt(meta.getColumnName(i)));
- else
- rsItem = rs.getString(meta.getColumnName(i));
-
- BeanUtils.setProperty(newItem, meta.getColumnName(i), rsItem);
- }
- list.add(newItem);
- }
- }
-
- }
- catch (Exception e)
- {
- logger.error("[viwo]-->[QueryForList]-->"+e.getMessage());
- throw new Exception(e);
- }
- return list;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public Object QueryForObject(String selectId,Object paramObject) throws Exception
- {
- logger.debug("[viwo]-->[enter QueryForObject]");
- try
- {
- ConfigSelect configSelect =(ConfigSelect)selectMap.get(selectId);
- if(configSelect!=null)
- {
- String sql = configSelect.getSql();
- String type = configSelect.getType();
- String param = configSelect.getParam();
- if(paramObject.getClass() != Class.forName(param))
- {
- return null;
- }
- String[] sqls = sql.split("#");
- if(sqls.length>1)
- {
- for(int i=0;i {
- if(i%2!=0)
- {
- String tmpValue = BeanUtils.getProperty(paramObject, sqls[i]);
- sql = sql.replaceFirst("#"+sqls[i]+"#", "'"+tmpValue+"'");
- }
- }
- }
- logger.debug("[viwo]-->[QueryForObject]-->[sql:]"+sql);
-
- ResultSet rs = statement.executeQuery(sql);
- ResultSetMetaData meta = rs.getMetaData();
- int columnCount = meta.getColumnCount();
- if(rs.next())
- {
- Object newItem = Class.forName(type).newInstance();
- for(int i=1;i<=columnCount;i++)
- {
- Object rsItem = null;
- String columnType = meta.getColumnTypeName(i);
- if(columnType.equals("numeric"))
- rsItem = new Double(rs.getDouble(meta.getColumnName(i)));
- else if(columnType.equals("int"))
- rsItem = new Integer(rs.getInt(meta.getColumnName(i)));
- else
- rsItem = rs.getString(meta.getColumnName(i));
-
- BeanUtils.setProperty(newItem, meta.getColumnName(i), rsItem);
- }
- return newItem;
- }
- }
-
- }
- catch (Exception e)
- {
- logger.error("[viwo]-->[QueryForObject]-->"+e.getMessage());
- throw new Exception(e);
- }
-
- return null;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public int executeStatement(String executeId,Object paramObject) throws Exception
- {
- logger.debug("[viwo]-->[enter executeStatement]");
- try
- {
- ConfigExecute configExecute =(ConfigExecute)executeMap.get(executeId);
- if(configExecute!=null)
- {
- String sql = configExecute.getSql();
- String param = configExecute.getParam();
- if(paramObject.getClass() != Class.forName(param))
- {
- return -1;
- }
- String[] sqls = sql.split("#");
- if(sqls.length>1)
- {
- for(int i=0;i {
- if(i%2!=0)
- {
- String tmpValue = BeanUtils.getProperty(paramObject, sqls[i]);
- sql = sql.replaceFirst("#"+sqls[i]+"#", "'"+tmpValue+"'");
- }
- }
- }
- logger.debug("[viwo]-->[executeStatement]-->[sql:]"+sql);
-
- return statement.executeUpdate(sql);
- }
-
- }
- catch (Exception e)
- {
- e.printStackTrace();
- logger.error("[viwo]-->[executeStatement]-->"+e.getMessage());
-
- }
- return -1;
- }
-
- public void addSelect(ConfigSelect configSelect)
- {
- selectMap.put(configSelect.getId(), configSelect);
- }
- public void addExecute(ConfigExecute configExecute)
- {
- executeMap.put(configExecute.getId(), configExecute);
- }
-
- public Map getSelectMap() {
- return selectMap;
- }
-
-
-
-
-
-
-
-
-
-
- public static synchronized SqlMapConfig getInstance(String path) throws Exception
- {
- logger.info("[viwo]-->[enter getInstance]-->[path:]"+path);
- SqlMapConfig sqlMapConfig = null;
- if(configMap.get(path)!=null)
- {
- sqlMapConfig = (SqlMapConfig)configMap.get(path);
- }
- else
- {
- Digester digester = new Digester();
- digester.setValidating(false);
- digester.addObjectCreate("sqlmap-config", "com.viwo.sqlmap.config.SqlMapConfig");
-
- digester.addObjectCreate("sqlmap-config/select", "com.viwo.sqlmap.config.ConfigSelect");
- digester.addSetProperties("sqlmap-config/select");
- digester.addCallMethod("sqlmap-config/select/sql", "setSql", 0);
- digester.addSetNext("sqlmap-config/select", "addSelect", "com.viwo.sqlmap.config.ConfigSelect");
-
- digester.addObjectCreate("sqlmap-config/execute", "com.viwo.sqlmap.config.ConfigExecute");
- digester.addSetProperties("sqlmap-config/execute");
- digester.addCallMethod("sqlmap-config/execute/sql", "setSql", 0);
- digester.addSetNext("sqlmap-config/execute", "addExecute", "com.viwo.sqlmap.config.ConfigExecute");
-
- try
- {
- sqlMapConfig = (SqlMapConfig)digester.parse(Thread.currentThread().getContextClassLoader().getResource("/"+path).getFile());
- configMap.put(path, sqlMapConfig);
- }
- catch (Exception e)
- {
- logger.error("[viwo]-->[getInstance]-->"+e.getMessage());
- throw new Exception(e);
- }
- }
-
- sqlMapConfig.connection = ConnectionFactory.getConnection();
- sqlMapConfig.statement = sqlMapConfig.connection.createStatement();
-
- return sqlMapConfig;
-
- }
-
-
-
-
-
- public void beginTransaction() throws Exception
- {
- try
- {
- this.connection.setAutoCommit(false);
- }
- catch (SQLException e)
- {
- logger.error("[viwo]-->[beginTransaction]--beginTransaction error");
- throw new Exception(e);
- }
- }
-
-
-
-
-
- public void commitTransaction() throws Exception
- {
- try
- {
- this.connection.commit();
- }
- catch (SQLException e)
- {
- logger.error("[viwo]-->[commitTransaction]--commitTransaction error");
- throw new Exception(e);
- }
- }
-
-
-
-
- public void rollbackTransaction() throws Exception
- {
- try
- {
- this.connection.rollback();
- }
- catch (SQLException e)
- {
- logger.error("[viwo]-->[rollbackTransaction]--rollbackTransaction error");
- throw new Exception(e);
- }
- }
-
-
-
- public void closeConnection()
- {
- if(statement!=null)
- {
- try {
- statement.close();
- }
- catch (SQLException e)
- {
-
- }
- }
- if(connection!=null)
- {
- try {
- connection.close();
- }
- catch (SQLException e)
- {
-
- }
- }
- }
-
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|