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;
}
}
}
分享到:
相关推荐
iBatis DAO
ibatis dao 包ibatis dao 包ibatis dao 包ibatis dao 包ibatis dao 包ibatis dao 包ibatis dao 包ibatis dao 包ibatis dao 包ibatis dao 包
IBATISDAO库类操作IBATISDAO库类操作IBATISDAO库类操作IBATISDAO库类操作
生成Ibatis Dao的工具类,可以手动配置路径 去生成DAO ENTITY sqlMap
iBATIS DAO框架分析 对源码进行了一定的解析,使理解更加简单!
ibaits dao的源码分析,在源码的基础上添加注释说明
学习iBATIS DAO必备的jar包,可以与SqlMaps结合起来用。
NULL 博文链接:https://qiuqiu0034.iteye.com/blog/1155067
博文链接:https://imticg.iteye.com/blog/217727
资源名称:iBATIS框架源码剖析内容简介:iBATIS是一种比较流行的ORM框架,本书全面介绍其结构体系和分析其源程序代码,该框架的核心包括两个组件,一个是iBATIS DAO,另一个是iBATIS SQL Map。本书分为三个部分,第...
iBATIS-DAO-2.pdf
strut2+spring+ibatis 的ation层,service层,dao层自动生成,简单易用
ibatis-dao-2.jar gggggggggggg
iBATIS_DAO事务管理实现
ibatis-common-2.jar ibatis-sqlmap-2.jar ibatis-dao-2.jar
10.4.1 配置iBATIS DAO 186 10.4.2 创建DaoManager实例 187 10.4.3 定义事务管理器 187 10.4.4 加载映射 188 10.4.5 DAO实现编码 191 10.5 小结 193 第11章 DAO使用进阶 194 11.1 不是基于SQLMap的DAO实现 194 ...
iBATIS实现 DAO
iBATIS 2.2.0 is the last release that includes the iBATIS DAO framework. After 2.2.0, the DAO iBATIS framework is removed from the downloads. We suggest converting iBATIS framework DAOs to the Spring ...
jar包ssi,ssh需要的jar包,hibernate3.jar、ibatis-dao-2.jar、spring.jar、struts.jar、log4j-1.2.9.jar
ibatis-dao-2.2.0.jar