`
udvs
  • 浏览: 14532 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论
阅读更多
package com.inch.common.db.ibatis;


import java.io.Reader;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class MyAppSqlConfig {
	private static final SqlMapClient sqlMap;
	private static final SqlMapClient mysqlMap;
	static {
		try {
			/**
			 * ���������ļ�·��
			 */
		//	String sqlmappath = "com/zjhcsoft/test/user/domain";
			String resource = "sqlmapconfig.xml";
			Reader reader = Resources.getResourceAsReader(resource);
			sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
			
			resource = "sqlmapconfig_mysql.xml";
			reader = Resources.getResourceAsReader(resource);
			mysqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);

		/*	Reader reader = Resources.getResourceAsReader(resource);
			sqlMap =  new XmlSqlMapClientBuilder().buildSqlMap(reader,sqlmappath);*/
		} catch (Exception e) {
			throw new RuntimeException(
				"Error initializing MyAppSqlConfig class.Cause :" + e.getMessage());
		}
	}
	public static SqlMapClient getSqlMapInstance() {
		return sqlMap;
	}
	
	public static SqlMapClient getMySqlMapInstance() {
		return mysqlMap;
	}
	
}



IbatisDao
package com.inch.common.db.ibatis;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.ibatis.sqlmap.client.SqlMapClient;
import com.inch.common.db.id.IDGeneratorFactory;
import com.inch.common.exception.ApplicationException;
import org.apache.log4j.Logger;

/**
 * oracle???
 * @author rtm 2005-9-4
 * @description
 * @spring.bean id="ibatisDao"
 */
public class IbatisDao {

	public static Logger logger = Logger.getLogger(IbatisDao.class);

	private SqlMapClient sqlMap;
	public static IbatisDao getDao(){
		return new IbatisDao();
	}
	
	public String generateStringId(){
		return (String)IDGeneratorFactory.generate(null);
	}

	/**
	 * @return
	 */
	public SqlMapClient getSqlMap() {
		//System.out.println("init sql Map");
		this.sqlMap = MyAppSqlConfig.getSqlMapInstance();
		//System.out.println(sqlMap.toString());
		return this.sqlMap;
	}
	
	/**
	 * @param sqlMapId
	 * @param param
	 * @return
	 */
	public int count(String sqlMapId, Object param){
		List list = null;
		int num = 0;
		try {
			long start = System.currentTimeMillis();
			 list = this.getSqlMap().queryForList(sqlMapId,param);
			 long time = System.currentTimeMillis() - start;
			// 如果结果记录条数超过80
			if ( list != null && list.size() > 80 ) {
				logger.error("[count]Performance Wanning: result.size=[" + list.size() + "]sqlMapId=[" + sqlMapId +"] param=" + param +"]");
			}
			if ( time > 500 ) {
				logger.error("[count]Performance Wanning: search time=[" + time + "]sqlMapId=[" + sqlMapId +"] param=" + param +"]");
			}
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new ApplicationException(e.getMessage());
		}
		if(list != null && list.size() > 0){
			try{
				if(list.size() > 1){
					/**
					 * ?????count???????size
					 */
					return list.size();
				}else{
					num = Integer.parseInt((String)list.get(0));
				}
			}catch(RuntimeException e){
				logger.error(e.getMessage());
				throw new ApplicationException(e.getMessage());
			}
		}
		return num;
	}

	/**
	 * ??????seq
	 * @param sqlMapId
	 * @return
	 */
	public Long getIdValue(String sqlMapId){
		try {
			return (Long)this.getSqlMap().queryForObject(sqlMapId,null);
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new ApplicationException(e.getMessage());
		}
	}

	/**
	 * ???????
	 * @param sqlMapId
	 * @param param
	 */
	public void update(String sqlMapId, Object param) {
		try {
			long start = System.currentTimeMillis();
			this.getSqlMap().update(sqlMapId, param);
			long time = System.currentTimeMillis() - start;
			if ( time > 500 ) {
				logger.error("[update]Performance Wanning: update time=[" + time + "]sqlMapId=[" + sqlMapId +"] param=" + param +"]");
			}
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new ApplicationException(e.getMessage());
		}
	}
	
	public String queryForString(String sqlMapId,Object param){
		return String.valueOf(this.queryForObject(sqlMapId,param));
	}

	/**
	 * ???
	 * 
	 * @param sqlMapId
	 * @param param
	 * @return
	 */
	public List queryForList(String sqlMapId, Object param) {
		List list = null;
		try {
			long start = System.currentTimeMillis();
			list = this.getSqlMap().queryForList(sqlMapId, param);
			long time = System.currentTimeMillis() - start;
			// 如果结果记录条数超过80
			if ( list != null && list.size() > 80 ) {
				logger.error("[queryForList]Performance Wanning: result.size=[" + list.size() + "]sqlMapId=[" + sqlMapId +"] param=" + param +"]");
			}
			if ( time > 500 ) {
				logger.error("[queryForList]Performance Wanning: search time=[" + time + "]sqlMapId=[" + sqlMapId +"] param=" + param +"]");
			}
			
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new ApplicationException(e.getMessage());
		}
		if (list == null) {
			list = new ArrayList();
		}
		return list;

	}

	/**
	 * ????????????
	 * 
	 * @param sqlMapId
	 * @param param
	 * @return
	 */
	public Object queryForObject(String sqlMapId, Object param) {
		Object value = null;
		try {
			long start = System.currentTimeMillis();
			value = this.getSqlMap().queryForObject(sqlMapId, param);
			long time = System.currentTimeMillis() - start;
			if ( time > 500 ) {
				logger.error("[queryForObject]Performance Wanning: search time=[" + time + "]sqlMapId=[" + sqlMapId +"] param=" + param +"]");
			}
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new ApplicationException(e.getMessage());
		}
		return value;

	}

	/**
	 * ????????????
	 * 
	 * @param sqlMapId
	 * @param param
	 * @return
	 */
	public boolean queryIfExist(String sqlMapId, Object param) {
		List list = null;
		try {
			long start = System.currentTimeMillis();
			list = this.getSqlMap().queryForList(sqlMapId, param);
			long time = System.currentTimeMillis() - start;
			// 如果结果记录条数超过80
			if ( list != null && list.size() > 80 ) {
				logger.error("[queryIfExist]Performance Wanning: result.size=[" + list.size() + "]sqlMapId=[" + sqlMapId +"] param=" + param +"]");
			}
			if ( time > 500 ) {
				logger.error("[queryIfExist]Performance Wanning: search time=[" + time + "]sqlMapId=[" + sqlMapId +"] param=" + param +"]");
			}
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new ApplicationException(e.getMessage());
		}
		if (list == null || list.size() == 0) {
			return false;
		} else {
			return true;
		}

	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics