com.dexcoder.dal.spring.JdbcDaoImpl Maven / Gradle / Ivy
package com.dexcoder.dal.spring;
import java.util.List;
import java.util.Map;
import org.springframework.util.CollectionUtils;
import com.dexcoder.commons.utils.StrUtils;
import com.dexcoder.dal.BoundSql;
import com.dexcoder.dal.JdbcDao;
import com.dexcoder.dal.build.AbstractSqlBuilder;
import com.dexcoder.dal.build.Criteria;
import com.dexcoder.dal.handler.MappingHandler;
/**
* jdbc操作dao
*
* Created by liyd on 3/3/15.
*/
@SuppressWarnings("unchecked")
public class JdbcDaoImpl extends AbstractJdbcDaoImpl implements JdbcDao {
public Long insert(Object entity) {
MappingHandler handler = this.getMappingHandler();
Criteria criteria = Criteria.insert(entity.getClass());
String nativePKValue = handler.getPkNativeValue(entity.getClass(), getDialect());
if (StrUtils.isNotBlank(nativePKValue)) {
String pkFieldName = handler.getPkFieldName(entity.getClass());
criteria.into(AbstractSqlBuilder.NATIVE_TOKENS[2] + pkFieldName + AbstractSqlBuilder.NATIVE_TOKENS[3],
nativePKValue);
}
final BoundSql boundSql = criteria.build(entity, true, getMappingHandler());
return this.insert(boundSql, entity.getClass());
}
public Long insert(Criteria criteria) {
final BoundSql boundSql = criteria.build(true, getMappingHandler());
return this.insert(boundSql, criteria.getEntityClass());
}
public void save(Object entity) {
final BoundSql boundSql = Criteria.insert(entity.getClass()).build(entity, true, getMappingHandler());
jdbcTemplate.update(boundSql.getSql(), boundSql.getParameters().toArray());
}
public void save(Criteria criteria) {
final BoundSql boundSql = criteria.build(true, getMappingHandler());
jdbcTemplate.update(boundSql.getSql(), boundSql.getParameters().toArray());
}
public int update(Criteria criteria) {
BoundSql boundSql = criteria.build(true, getMappingHandler());
return jdbcTemplate.update(boundSql.getSql(), boundSql.getParameters().toArray());
}
public int update(Object entity) {
BoundSql boundSql = Criteria.update(entity.getClass()).build(entity, true, getMappingHandler());
return jdbcTemplate.update(boundSql.getSql(), boundSql.getParameters().toArray());
}
public int update(Object entity, boolean isIgnoreNull) {
BoundSql boundSql = Criteria.update(entity.getClass()).build(entity, isIgnoreNull, getMappingHandler());
return jdbcTemplate.update(boundSql.getSql(), boundSql.getParameters().toArray());
}
public int delete(Criteria criteria) {
BoundSql boundSql = criteria.build(true, getMappingHandler());
return jdbcTemplate.update(boundSql.getSql(), boundSql.getParameters().toArray());
}
public int delete(Object entity) {
BoundSql boundSql = Criteria.delete(entity.getClass()).build(entity, true, getMappingHandler());
return jdbcTemplate.update(boundSql.getSql(), boundSql.getParameters().toArray());
}
public int delete(Class> clazz, Long id) {
Criteria criteria = Criteria.delete(clazz);
BoundSql boundSql = criteria.where(criteria.getPkField(getMappingHandler()), new Object[] { id }).build(true,
getMappingHandler());
return jdbcTemplate.update(boundSql.getSql(), boundSql.getParameters().toArray());
}
public List queryList(Criteria criteria) {
BoundSql boundSql = criteria.build(true, getMappingHandler());
List> list = jdbcTemplate.query(boundSql.getSql(), boundSql.getParameters().toArray(),
this.getRowMapper(criteria.getEntityClass()));
return (List) list;
}
public List queryList(Class> clazz) {
BoundSql boundSql = Criteria.select(clazz).build(true, getMappingHandler());
List> list = jdbcTemplate.query(boundSql.getSql(), boundSql.getParameters().toArray(),
this.getRowMapper(clazz));
return (List) list;
}
public List queryList(T entity) {
BoundSql boundSql = Criteria.select(entity.getClass()).build(entity, true, getMappingHandler());
List> list = jdbcTemplate.query(boundSql.getSql(), boundSql.getParameters().toArray(),
this.getRowMapper(entity.getClass()));
return (List) list;
}
public List queryList(T entity, Criteria criteria) {
BoundSql boundSql = criteria.build(entity, true, getMappingHandler());
List> list = jdbcTemplate.query(boundSql.getSql(), boundSql.getParameters().toArray(),
this.getRowMapper(entity.getClass()));
return (List) list;
}
public int queryCount(Class> clazz) {
BoundSql boundSql = Criteria.select(clazz).addSelectFunc("count(*)").build(null, true, getMappingHandler());
return jdbcTemplate.queryForObject(boundSql.getSql(), boundSql.getParameters().toArray(), Integer.class);
}
public int queryCount(Object entity, Criteria criteria) {
BoundSql boundSql = criteria.addSelectFunc("count(*)", true, false, true).build(entity, true,
getMappingHandler());
return jdbcTemplate.queryForObject(boundSql.getSql(), boundSql.getParameters().toArray(), Integer.class);
}
public int queryCount(Object entity) {
BoundSql boundSql = Criteria.select(entity.getClass()).addSelectFunc("count(*)")
.build(entity, true, getMappingHandler());
return jdbcTemplate.queryForObject(boundSql.getSql(), boundSql.getParameters().toArray(), Integer.class);
}
public int queryCount(Criteria criteria) {
BoundSql boundSql = criteria.addSelectFunc("count(*)", true, false, true).build(true, getMappingHandler());
return jdbcTemplate.queryForObject(boundSql.getSql(), boundSql.getParameters().toArray(), Integer.class);
}
public T get(Class clazz, Long id) {
Criteria criteria = Criteria.select(clazz);
BoundSql boundSql = criteria.where(criteria.getPkField(getMappingHandler()), new Object[] { id }).build(true,
getMappingHandler());
//采用list方式查询,当记录不存在时返回null而不会抛出异常
List list = jdbcTemplate.query(boundSql.getSql(), this.getRowMapper(clazz), boundSql.getParameters()
.toArray());
if (CollectionUtils.isEmpty(list)) {
return null;
}
return list.iterator().next();
}
public T get(Criteria criteria, Long id) {
BoundSql boundSql = criteria.where(criteria.getPkField(getMappingHandler()), new Object[] { id }).build(true,
getMappingHandler());
//采用list方式查询,当记录不存在时返回null而不会抛出异常
List list = (List) jdbcTemplate
.query(boundSql.getSql(), this.getRowMapper(criteria.getEntityClass()), id);
if (CollectionUtils.isEmpty(list)) {
return null;
}
return list.iterator().next();
}
public T querySingleResult(T entity) {
BoundSql boundSql = Criteria.select(entity.getClass()).build(entity, true, getMappingHandler());
//采用list方式查询,当记录不存在时返回null而不会抛出异常
List> list = jdbcTemplate.query(boundSql.getSql(), boundSql.getParameters().toArray(),
this.getRowMapper(entity.getClass()));
if (CollectionUtils.isEmpty(list)) {
return null;
}
return (T) list.iterator().next();
}
public T querySingleResult(Criteria criteria) {
BoundSql boundSql = criteria.build(true, getMappingHandler());
//采用list方式查询,当记录不存在时返回null而不会抛出异常
List> list = jdbcTemplate.query(boundSql.getSql(), boundSql.getParameters().toArray(),
this.getRowMapper(criteria.getEntityClass()));
if (CollectionUtils.isEmpty(list)) {
return null;
}
return (T) list.iterator().next();
}
public T queryObject(Criteria criteria) {
final BoundSql boundSql = criteria.build(true, getMappingHandler());
return (T) jdbcTemplate.queryForObject(boundSql.getSql(), boundSql.getParameters().toArray(), Object.class);
}
public List queryObjectList(Criteria criteria, Class elementType) {
final BoundSql boundSql = criteria.build(true, getMappingHandler());
return jdbcTemplate.queryForList(boundSql.getSql(), elementType, boundSql.getParameters().toArray());
}
public Map queryRowMap(Criteria criteria) {
BoundSql boundSql = criteria.build(true, getMappingHandler());
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy