- 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;
- /**
- * DAO using this class, don't need write jdbc operations.
- *
- * @author viwo
- *
- */
- 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;
- /**
- * get a List object from database
- *
- * fit for this sql: select username,password from users
- *
- * the " name, password " and its values are packed in an object list of return
- * list.
- *
- * @param selectId
- * @param paramObject
- * @return List
- * @throws Exception
- */
- 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);
- //PreparedStatement ps = connection.prepareStatement(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;
- }
- /**
- * get an object from database
- *
- * fit for this sql: select username,password from users where username=#username#
- *
- * the " name, password " and its values are packed in an object of return
- * object.
- *
- * @param selectId
- * @param paramObject
- * @return Object
- * @throws Exception
- */
- 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);
- //PreparedStatement ps = connection.prepareStatement(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);
- }
- //如果沒有查出結果或者selectId非法則返回null。
- return null;
- }
- /**
- * execute a statement from sqlmap-config.xml by executeId
- *
- * fit for this sql: insert into users values(#username#,#password#)
- *
- * the " #name#, #password# " will be set value by paramObject
- *
- * @param selectId
- * @param paramObject
- * @return int
- * @throws Exception
- */
- 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);
- //PreparedStatement ps = connection.prepareStatement(sql);
- return statement.executeUpdate(sql);
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- logger.error("[viwo]-->[executeStatement]-->"+e.getMessage());
- //throw new Exception(e);
- }
- 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;
- }
- /**
- * a static synchronized method to get the SqlMapConfig from a gived path
- *
- * this method will initialize the properity connection
- *
- * @param path
- * @return SqlMapConfig
- * @throws Exception
- */
- 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;
- }
- /**
- * begin transaction
- * @throws Exception
- */
- public void beginTransaction() throws Exception
- {
- try
- {
- this.connection.setAutoCommit(false);
- }
- catch (SQLException e)
- {
- logger.error("[viwo]-->[beginTransaction]--beginTransaction error");
- throw new Exception(e);
- }
- }
- /**
- * commit transaction
- * @throws Exception
- */
- public void commitTransaction() throws Exception
- {
- try
- {
- this.connection.commit();
- }
- catch (SQLException e)
- {
- logger.error("[viwo]-->[commitTransaction]--commitTransaction error");
- throw new Exception(e);
- }
- }
- /**
- * rollback transaction
- * @throws Exception
- */
- public void rollbackTransaction() throws Exception
- {
- try
- {
- this.connection.rollback();
- }
- catch (SQLException e)
- {
- logger.error("[viwo]-->[rollbackTransaction]--rollbackTransaction error");
- throw new Exception(e);
- }
- }
- /**
- * close the property of connection
- */
- public void closeConnection()
- {
- if(statement!=null)
- {
- try {
- statement.close();
- }
- catch (SQLException e)
- {
- }
- }
- if(connection!=null)
- {
- try {
- connection.close();
- }
- catch (SQLException e)
- {
- }
- }
- }
- }
"iBATIS"的簡易實現
java 代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.