cn.tangjiabao.halodb.dbutils.dao.impl.AbstractDbUtilsHaloDao Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of haloDb Show documentation
Show all versions of haloDb Show documentation
Orm whitch The highest development efficiency and Efficiency is the fastest
package cn.tangjiabao.halodb.dbutils.dao.impl;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import cn.tangjiabao.halodb.core.AbstractHaloDao;
import cn.tangjiabao.halodb.core.map.HaloGetMap;
import cn.tangjiabao.halodb.core.utils.sql.SQLFormatter;
import cn.tangjiabao.halodb.dbutils.ProcRunner;
import cn.tangjiabao.halodb.dbutils.dao.IHaloDao;
import cn.tangjiabao.halodb.dbutils.dao.IHaloViewDao;
import cn.tangjiabao.halodb.dbutils.handlers.HaloBeanHandler;
import cn.tangjiabao.halodb.dbutils.handlers.HaloBeanListHandler;
import cn.tangjiabao.halodb.dbutils.handlers.HaloGetMapHandler;
import cn.tangjiabao.halodb.dbutils.handlers.HaloGetMapListHandler;
import cn.tangjiabao.halodb.utils.convert.ConvertUtils;
import cn.tangjiabao.halodb.utils.exception.ExceptionUtils;
import cn.tangjiabao.halodb.utils.logger.LogUtils;
import cn.tangjiabao.halodb.utils.string.StringUtils;
/**
* 依赖DbUtils的操作的抽象类
* @author [email protected]
* @date 2015-6-14 下午10:15:38
* @param
*/
public abstract class AbstractDbUtilsHaloDao extends AbstractHaloDao implements IHaloDao,IHaloViewDao{
@Resource
private DataSource dataSource;
private QueryRunner queryRunner;
private ProcRunner procRunner;
protected abstract Connection getConnection(DataSource dataSource);
//private static final Logger log = LoggerFactory.getLogger(AbstractHaloDao. class );
protected void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
private String generateMyCountSql(String sql) {
return StringUtils.format("select count(1) from ({0}) temp ", sql);
}
protected long countMySqlResult(String sql, Object[] params) {
String countSql = generateMyCountSql(sql);
Object result = findBy(countSql, 1, params);
return ConvertUtils.toLong(result);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object findBy(String sql, int columnIndex, Object[] params) {
queryRunner = new QueryRunner(dataSource);
Connection conn = getConnection(dataSource);
//Connection conn = DataSourceUtils.getConnection(dataSource);
Object object = null;
try {
object = queryRunner.query(conn,sql, new ScalarHandler(columnIndex), params);
} catch (SQLException e) {
logger.error("Error occured while attempting to query data", e);
}
return object;
}
protected List queryList(String sql, Object[] params, Map aliasMap) {
logger.info(LogUtils.format("生成的sql为:", SQLFormatter.format(sql)));
logger.info(LogUtils.attr("prams:", params));
queryRunner = new QueryRunner(dataSource);
Connection conn = getConnection(dataSource);
List list = new ArrayList();
try {
list = queryRunner.query(conn, sql, new HaloBeanListHandler(getEntityType(), aliasMap), params);
} catch (SQLException e) {
logger.error("Error occured while attempting to query data", e);
}
return list;
}
protected T queryOne(String sql, Object[] params, Map aliasMap) {
logger.info(LogUtils.format("生成的sql为:", SQLFormatter.format(sql)));
logger.info(LogUtils.attr("prams:", params));
queryRunner = new QueryRunner(dataSource);
Connection conn =getConnection(dataSource);
T bean = null;
try {
bean = queryRunner.query(conn, sql, new HaloBeanHandler(getEntityType(), aliasMap), params);
} catch (SQLException e) {
logger.error("Error occured while attempting to query data", e);
}
return bean;
}
protected HaloGetMap queryUnique(String sql, Object[] params, Map aliasMap) {
logger.info(LogUtils.format("生成的sql为:", SQLFormatter.format(sql)));
logger.info(LogUtils.attr("prams:", params));
queryRunner = new QueryRunner(dataSource);
Connection conn = getConnection(dataSource);
HaloGetMap map = new HaloGetMap();
try {
map = queryRunner.query(conn,sql, new HaloGetMapHandler(), params);
} catch (SQLException e) {
logger.error("Error occured while attempting to query data", e);
}
return map;
}
protected Object insert(String sql, Object[] params) {
logger.info(LogUtils.format("生成的sql为:", SQLFormatter.format(sql)));
logger.info(LogUtils.attr("prams:", params));
queryRunner = new QueryRunner(dataSource);
Connection conn = getConnection(dataSource);
Object object = null;
try {
object = queryRunner.insert(conn, sql,new ScalarHandler
© 2015 - 2024 Weber Informatics LLC | Privacy Policy