Ibatis dao


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;
}

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值