验证中...
私信发送成功
检测到您已登录开源中国,是否 一键登录码云
语言: Java
分类: Web开发技术
最后更新于 2017-09-13 21:12
片段 1 片段 2
jdbc 基础dao 接口
原始数据 复制代码
package com.demo.dao;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import com.demo.bean.PageBean;
public interface BaseDao<T, ID extends Serializable> {
/**
* 基础接口之一</br> 批量添加接口
*
* @param sql
* @param values
* @return
*/
public int[] batchAdd(String sql, List<List<Object>> values);
/**
* 基础接口之一</br> 批量删除记录接口
*
* @param sql
* @param values
* @return
*/
public int del(String sql, List<Object> values);
/**
* 基础接口之一</br> 更新接口
*
* @param sql
* @param values
* @return
*/
public int update(String sql, List<Object> values);
/**
* 基础接口之一</br> 查询一个long值
*
* @param sql
* @param values
* @return
*/
public long getLong(String sql, List<Object> values);
/**
* 基础接口之一</br> 查询一个int值
*
* @param sql
* @param values
* @return
*/
public int getInt(String sql, List<Object> values);
/**
* 基础接口之一</br> sql查询通用接口
*
* @param sql
* @param values
* @return
*/
public List<T> search(String sql, List<Object> values);
/**
* 基础接口之一</br> sql查询通用接口
*
* @param sql
* @param values
* @param e
* @return
*/
public <E> List<E> search(String sql, List<Object> values, Class<E> e);
/**
* 分页查询
*
* @param sql
* @param values
* @param pageBean
* @return
*/
public PageBean<T> search(String sql, List<Object> values,
PageBean<T> pageBean);
/**
* 分页查询
*
* @param sql
* @param values
* @param pageBean
* @return
*/
public <E> PageBean<E> search(String sql, List<Object> values,
PageBean<E> pageBean, Class<E> e);
/**
* 根据主键修改数据
*
* @param t
* @return
*/
public T update(T t);
/**
* 批量添加接口
*
* @param list
* @return
*/
public int[] batchSave(List<T> list);
/**
* 获取一条sql执行后返回的记录数
*
* @param sql
* @param values
* @return
*/
public int getCount(String sql, List<Object> values);
/**
* 根据id值删除多条数据
*
* @param ids
* @return
*/
public int delByIds(List<ID> ids);
/**
* 根据id删除一条记录
*
* @param id
* @return
*/
public int del(ID id);
/**
* 根据id值查询多条数据
*
* @param ids
* @return
*/
public List<T> getByIds(List<ID> ids);
/**
* 根据id值查询
*
* @param id
* @return
*/
public T get(ID id);
/**
* 获取本表所有记录
*
* @return
*/
public List<T> getAll();
/**
* 获取主键列名
*
* @return
*/
public String getPk();
/**
* 获取表名
*
* @return
*/
public String getTableName();
/**
* 添加记录,并返回新增记录的主键<br/>
* 注意:<br/>
* 当主键生成策略为IDENTITY(数据库自增)时,SQL执行成功返回新数据主键,执行不成功返回0<br/>
* 主键生成策略为非IDENTITY(数据库自增)时,SQL执行返回值无法确定<br/>
*
* @param sql
* @param values
* @return
*/
public ID addReturnId(String sql, List<Object> values);
/**
* 保存一个对象
*
* @param t
* @return
*/
public T save(T t);
/**
* sql查询通用接口
*
* @param t
* @return
*/
public List<T> search(T t);
/**
* sql查询通用接口,忽略主键,仅返回查询结果集的第一个对象
*
* @param t
* @return
*/
public T searchOne(T t);
/**
*
* @param t
* @param pageBean
* @return
*/
public PageBean<T> search(T t, PageBean<T> pageBean);
/**
* sql查询通用接口
*
* @param sql
* @param values
* @param e
* @return
*/
public List<Map<String, Object>> searchForMap(String sql,
List<Object> values);
/**
* map型分页查询接口
*
* @param sql
* @param values
* @param pageBean
* @return
*/
public PageBean<Map<String, Object>> searchForMap(String sql,
List<Object> values, PageBean<Map<String, Object>> pageBean);
}
jdbc 基础dao 接口实现
原始数据 复制代码
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
package com.demo.dao.mysql;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Resource;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import com.demo.bean.PageBean;
import com.demo.dao.BaseDao;
import com.demo.dao.mapper.ModelPropertyRowMapper;
import com.demo.exception.DaoException;
import com.demo.utils.BeanUtilsBean;
public abstract class BaseDaoMysqlImpl<T, ID extends Serializable> extends
JdbcDaoSupport implements BaseDao<T, ID> {
public final Log log = LogFactory.getLog(this.getClass());
private Class<T> persistentClass;
private String tableName = "";
private String pk = "";
private Map<String, String> property2ColumnMap = new HashMap<String, String>();
private Map<String, String> column2PropertyMap = new HashMap<String, String>();
private GenerationType strategy;
protected List<String> transientPropertys = new ArrayList<String>();
protected BaseDaoMysqlImpl(Class<T> persistentClass) {
this.persistentClass = persistentClass;
Table table = AnnotationUtils.findAnnotation(persistentClass,
Table.class);
if (table == null) {
throw new DaoException(persistentClass.getName() + "没有定义@table");
}
this.tableName = table == null ? persistentClass.getName() : table.name() ;
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(persistentClass);
} catch (IntrospectionException e) {
throw new DaoException(e);
}
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
Id id = AnnotationUtils
.findAnnotation(pd.getReadMethod(), Id.class);
if (id != null) {
Column idColumn = AnnotationUtils.findAnnotation(
pd.getReadMethod(), Column.class);
if(idColumn != null){
pk = idColumn.name();
} else {
pk = pd.getName();
}
GeneratedValue gv = AnnotationUtils.findAnnotation(
pd.getReadMethod(), GeneratedValue.class);
if (gv == null) {
strategy = GenerationType.IDENTITY;
} else {
strategy = gv.strategy();
}
}
Column column = AnnotationUtils.findAnnotation(
pd.getReadMethod(), Column.class);
property2ColumnMap.put(pd.getName(), column == null ? pd.getName() : column.name() );
column2PropertyMap.put(column == null ? pd.getName() : column.name(), pd.getName());
Transient transient_ = AnnotationUtils.findAnnotation(
pd.getReadMethod(), Transient.class);
if (transient_ != null) {
transientPropertys.add(pd.getName());
}
}
if ("".equals(this.getPk())) {
throw new DaoException(persistentClass.getName() + "中没有在get方法上定义@Id");
}
}
protected BaseDaoMysqlImpl() {
}
@Override
public String getTableName() {
return tableName;
}
@Override
public String getPk() {
return pk;
}
@Resource(name = "jdbcTemplate")
public void setJb(JdbcTemplate jb) {
super.setJdbcTemplate(jb);
}
@Override
public List<T> getAll() {
StringBuilder sb = new StringBuilder("select * from ");
sb.append(this.getTableName());
return this.search(sb.toString(), null);
}
@Override
public List<T> search(String sql, List<Object> values) {
if (StringUtils.isEmpty(sql))
return new ArrayList<T>();
if (values == null)
values = new ArrayList<Object>();
logger.info("sql : " + sql + " values:" + values);
List<T> list = this.getJdbcTemplate().query(sql, values.toArray(),
new ModelPropertyRowMapper<T>(this.persistentClass, property2ColumnMap));
return list == null ? new ArrayList<T>() : list;
}
@Override
public PageBean<T> search(T t, PageBean<T> pageBean) {
Map<String, Object> map = null;
try {
map = toModelMap(t);
} catch (Exception e) {
throw new DaoException(persistentClass.getName() + "解析异常!", e);
}
for (String proterty : transientPropertys) {
map.remove(proterty);
}
return this.search(map, pageBean);
}
@SuppressWarnings("unchecked")
protected PageBean<T> search(Map<String, Object> map, PageBean<T> pageBean) {
ID id = (ID) map.get(this.getPk());
if (map != null && id != null) {
map.remove(id);
}
List<String> removekeys = new ArrayList<String>();
for (Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() == null) {
removekeys.add(entry.getKey());
}
}
for (String key : removekeys) {
map.remove(key);
}
List<Object> values = new ArrayList<Object>();
StringBuilder sb = new StringBuilder("select * from ");
sb.append(this.getTableName());
if (map.size() != 0) {
sb.append(" where ");
for (Entry<String, Object> entry : map.entrySet()) {
sb.append(entry.getKey());
sb.append("=? ");
values.add(entry.getValue());
sb.append(" and ");
}
}
this.deleteLastStr(sb, "and");
this.search(sb.toString(), values, pageBean);
return pageBean;
}
protected List<T> search(Map<String, Object> map) {
if (map == null || map.isEmpty())
return null;
@SuppressWarnings("unchecked")
ID id = (ID) map.get(this.getPk());
if (id != null) {
map.remove(id);
}
List<String> removekeys = new ArrayList<String>();
for (Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() == null) {
removekeys.add(entry.getKey());
}
}
for (String key : removekeys) {
map.remove(key);
}
List<Object> values = new ArrayList<Object>();
StringBuilder sb = new StringBuilder("select * from ");
sb.append(this.getTableName());
if (map.size() != 0) {
sb.append(" where ");
for (Entry<String, Object> entry : map.entrySet()) {
sb.append(entry.getKey());
sb.append("=? ");
values.add(entry.getValue());
sb.append(" and ");
}
this.deleteLastStr(sb, "and");
}
return this.search(sb.toString(), values);
}
protected T searchOne(Map<String, Object> map) {
if (map == null || map.isEmpty())
return null;
@SuppressWarnings("unchecked")
ID id = (ID) map.get(this.getPk());
if (id != null) {
map.remove(id);
}
List<String> removekeys = new ArrayList<String>();
for (Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() == null) {
removekeys.add(entry.getKey());
}
}
for (String key : removekeys) {
map.remove(key);
}
List<Object> values = new ArrayList<Object>();
StringBuilder sb = new StringBuilder("select * from ");
sb.append(this.getTableName());
if (map.size() != 0) {
sb.append(" where ");
for (Entry<String, Object> entry : map.entrySet()) {
sb.append(entry.getKey());
sb.append("=? ");
values.add(entry.getValue());
sb.append(" and ");
}
this.deleteLastStr(sb, "and");
}
List<T> list = this.search(sb.toString(), values);
if (list != null && list.size() != 0) {
return list.get(0);
} else {
return null;
}
}
protected int add(String sql, List<Object> values) {
if (StringUtils.isEmpty(sql))
return 0;
if (values == null)
values = new ArrayList<Object>();
if (log.isInfoEnabled()) {
log.info("sql : " + sql + " values:" + values);
}
return this.getJdbcTemplate().update(sql, values.toArray());
}
@SuppressWarnings("unchecked")
@Override
public ID addReturnId(final String sql, final List<Object> values) {
JdbcTemplate template = this.getJdbcTemplate();
KeyHolder keyHolder = new GeneratedKeyHolder();
if (log.isInfoEnabled()) {
log.info("sql : " + sql + " values:" + values);
}
template.update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection con)
throws SQLException {
int i = 0;
PreparedStatement ps = con.prepareStatement(sql,
Statement.RETURN_GENERATED_KEYS);
for (i = 0; i < values.size(); i++) {
Object value = values.get(i);
if (value != null) {
if (value instanceof java.lang.Integer) {
ps.setInt(i + 1, (Integer) value);
} else if (value instanceof java.lang.Long) {
ps.setLong(i + 1, (Long) value);
} else if (value instanceof java.util.Date) {
ps.setDate(i + 1,
new java.sql.Date(((Date) value).getTime()));
ps.setTimestamp(i + 1, new java.sql.Timestamp(
((Date) value).getTime()));
} else if (value instanceof java.lang.String) {
ps.setString(i + 1, value.toString());
} else if (value instanceof java.lang.Double) {
ps.setDouble(i + 1, (Double) value);
} else if (value instanceof java.lang.Byte) {
ps.setByte(i + 1, (Byte) value);
} else if (value instanceof java.lang.Character) {
ps.setString(i + 1, value.toString());
} else if (value instanceof java.lang.Float) {
ps.setFloat(i + 1, (Float) value);
} else if (value instanceof java.lang.Boolean) {
ps.setBoolean(i + 1, (Boolean) value);
} else if (value instanceof java.lang.Short) {
ps.setShort(i + 1, (Short) value);
} else {
ps.setObject(i + 1, value);
}
} else {
ps.setNull(i + 1, Types.NULL);
}
}
return ps;
}
}, keyHolder);
return (ID) (Integer) keyHolder.getKey().intValue();
}
@SuppressWarnings("unchecked")
protected ID addReturnId(Map<String, Object> map) {
if (map == null || map.size() == 0)
return null;
if (strategy.equals(GenerationType.IDENTITY)) {
map.remove(this.getPk());
}
StringBuilder sb = new StringBuilder("insert into ");
sb.append(this.getTableName());
List<String> columns = new ArrayList<String>();
List<Object> values = new ArrayList<Object>();
for (Entry<String, Object> e : map.entrySet()) {
columns.add(e.getKey());
values.add(e.getValue());
}
sb.append("(");
sb.append(StringUtils.join(columns, ','));
sb.append(") values(");
String[] paras = new String[values.size()];
Arrays.fill(paras, "?");
sb.append(StringUtils.join(paras, ','));
sb.append(")");
if (strategy.equals(GenerationType.IDENTITY)) {
ID id = this.addReturnId(sb.toString(), values);
return (ID) id;
} else if (strategy.equals(GenerationType.AUTO)) {
int count = this.add(sb.toString(), values);
if (count != 0) {
return (ID) map.get(this.getPk());
} else {
return null;
}
}
return null;
}
@Override
public int[] batchAdd(String sql, List<List<Object>> values) {
if (StringUtils.isEmpty(sql))
return new int[0];
if (values == null)
values = new ArrayList<List<Object>>();
if (log.isInfoEnabled()) {
log.info("sql : " + sql + " values:" + values);
}
List<Object[]> batchArgs = new ArrayList<Object[]>();
for (List<Object> ol : values) {
batchArgs.add(ol.toArray());
}
return this.getJdbcTemplate().batchUpdate(sql, batchArgs);
}
@Override
public int update(String sql, List<Object> values) {
if (StringUtils.isEmpty(sql))
return 0;
if (values == null)
values = new ArrayList<Object>();
if (log.isInfoEnabled()) {
log.info("sql : " + sql + " values:" + values);
}
return this.getJdbcTemplate().update(sql, values.toArray());
}
@Override
public int del(ID id) {
if (id == null)
return 0;
StringBuilder sb = new StringBuilder("delete from ");
sb.append(this.getTableName());
sb.append(" where ");
sb.append(this.getPk());
sb.append("=?");
List<Object> values = new ArrayList<Object>();
values.add(id);
return this.del(sb.toString(), values);
}
@Override
public int delByIds(List<ID> ids) {
if (ids == null || ids.isEmpty())
return 0;
StringBuilder sb = new StringBuilder("delete from ");
sb.append(this.getTableName());
sb.append(" where ");
sb.append(this.getPk());
sb.append(" in (");
Serializable[] ss = new Serializable[ids.size()];
Arrays.fill(ss, "?");
sb.append(StringUtils.join(ss, ','));
sb.append(")");
List<Object> values = new ArrayList<Object>();
values.addAll(ids);
return this.del(sb.toString(), values);
}
@Override
public int del(String sql, List<Object> values) {
if (log.isInfoEnabled()) {
log.info("sql : " + sql + " values:" + values);
}
return this.getJdbcTemplate().update(sql, values.toArray());
}
@Override
public int getInt(String sql, List<Object> values) {
if (StringUtils.isEmpty(sql))
return 0;
if (values == null)
values = new ArrayList<Object>();
if (log.isInfoEnabled()) {
log.info("sql : " + sql + " values:" + values);
}
return this.getJdbcTemplate().queryForInt(sql, values.toArray());
}
@Override
public long getLong(String sql, List<Object> values) {
if (StringUtils.isEmpty(sql))
return 0;
if (values == null)
values = new ArrayList<Object>();
if (log.isInfoEnabled()) {
log.info("sql : " + sql + " values:" + values);
}
return this.getJdbcTemplate().queryForLong(sql, values.toArray());
}
@Override
public int getCount(String sql, List<Object> values) {
if (StringUtils.isEmpty(sql))
return 0;
if (values == null)
values = new ArrayList<Object>();
StringBuilder sb = new StringBuilder("select count(*) from (");
sb.append(sql);
sb.append(") as _tn");
return this.getInt(sb.toString(), values);
}
protected int[] batchAdd(List<Map<String, Object>> mapList) {
if (mapList == null || mapList.isEmpty())
return new int[0];
Map<String, Object> map = mapList.get(0);
if (map == null || map.size() == 0)
return new int[0];
if (strategy.equals(GenerationType.AUTO)) {
map.remove(this.getPk());
}
String[] columnNames = new String[map.size()];
columnNames = map.keySet().toArray(columnNames);
StringBuilder sb = new StringBuilder("insert into ");
sb.append(this.getTableName());
sb.append("(");
sb.append(StringUtils.join(columnNames, ','));
sb.append(") values(");
String[] paras = new String[columnNames.length];
Arrays.fill(paras, "?");
sb.append(StringUtils.join(paras, ','));
sb.append(")");
List<List<Object>> values = new ArrayList<List<Object>>();
for (Map<String, Object> m : mapList) {
if (m != null && !m.isEmpty()) {
List<Object> l = new ArrayList<Object>();
for (String str : columnNames) {
l.add(m.get(str));
}
values.add(l);
}
}
return this.batchAdd(sb.toString(), values);
}
protected int updateById(Map<String, Object> map) {
if (map == null || map.size() == 0)
return 0;
Serializable id = (Serializable) map.get(this.getPk());
if (id == null || "".equals(id))
return 0;
List<Object> values = new ArrayList<Object>();
StringBuilder sb = new StringBuilder("update ");
sb.append(this.getTableName());
sb.append(" set ");
map.remove(this.getPk());
for (Entry<String, Object> e : map.entrySet()) {
sb.append(e.getKey());
sb.append("=?, ");
values.add(e.getValue());
}
deleteLastStr(sb, ",");
sb.append(" where ");
sb.append(this.getPk());
sb.append("=?");
values.add(id);
map.put(this.getPk(), id);
return this.update(sb.toString(), values);
}
@Override
public PageBean<T> search(String sql, List<Object> values,
PageBean<T> pageBean) {
if (StringUtils.isEmpty(sql)) {
List<T> reList = new ArrayList<T>();
pageBean.setRowCount(0);
pageBean.setList(reList);
return pageBean;
}
if (values == null)
values = new ArrayList<Object>();
int startRow = pageBean.getStartRow();
int offset = pageBean.getPageSize();
StringBuilder sb = new StringBuilder(sql);
if (StringUtils.isNotEmpty(pageBean.getGroupby())) {
sb.append(" group by ");
sb.append(pageBean.getGroupby());
}
int count = this.getCount(sb.toString(), values);
pageBean.setRowCount(count);
if (!StringUtils.isEmpty(pageBean.getOrderBy())) {
sb.append(" order by ");
sb.append(pageBean.getOrderBy());
if (!StringUtils.isEmpty(pageBean.getOrderType())) {
sb.append(" ");
sb.append(pageBean.getOrderType());
}
}
sb.append(" limit ");
sb.append(startRow);
sb.append(",");
sb.append(offset);
pageBean.setList(this.search(sb.toString(), values));
return pageBean;
}
@Override
public T get(ID id) {
if (id == null)
return null;
StringBuilder sb = new StringBuilder("select * from ");
sb.append(this.getTableName());
sb.append(" where ");
sb.append(this.getPk());
sb.append("=?");
List<Object> values = new ArrayList<Object>();
values.add(id);
List<T> list = this.search(sb.toString(), values);
if (list == null || list.size() == 0)
return null;
else
return list.get(0);
}
@Override
public T save(T t) {
if (t == null) {
throw new DaoException("模型对象为空!保存失败");
}
Map<String, Object> map = toModelMap(t);
for (String proterty : transientPropertys) {
map.remove(proterty);
}
ID id = this.addReturnId(map);
if (strategy.equals(GenerationType.IDENTITY)) {
try {
BeanUtils.setProperty(t, column2PropertyMap.get(pk), id);
} catch (Exception e) {
log.error(persistentClass.getName() + "解析异常!", e);
}
}
return t;
}
@Override
public List<T> getByIds(List<ID> ids) {
List<T> list = null;
if (ids != null && !ids.isEmpty()) {
StringBuilder sb = new StringBuilder("select * from ");
sb.append(this.getTableName());
sb.append(" where ");
sb.append(this.getPk());
sb.append(" in (");
Serializable[] ss = new Serializable[ids.size()];
Arrays.fill(ss, "?");
sb.append(StringUtils.join(ss, ','));
sb.append(")");
List<Object> values = new ArrayList<Object>();
values.addAll(ids);
list = this.search(sb.toString(), values);
} else {
list = new ArrayList<T>();
}
return list;
}
@Override
public T update(T t) {
if (t == null) {
throw new DaoException("模型对象为空!");
}
Map<String, Object> map = null;
try {
map = toModelMap(t);
for (String proterty : transientPropertys) {
map.remove(proterty);
}
} catch (Exception e) {
throw new DaoException(persistentClass.getName() + "解析异常!", e);
}
int count = this.updateById(map);
if (count == 0) {
throw new DaoException(persistentClass.getName() + "更新失败" + t);
}
return t;
}
@Override
public int[] batchSave(List<T> list) {
if (list == null) {
throw new DaoException("模型对象为空!");
}
List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
for (T t : list) {
try {
Map<String, Object> map = toModelMap(t);
for (String proterty : transientPropertys) {
map.remove(proterty);
}
mapList.add(map);
} catch (Exception e) {
throw new DaoException(persistentClass.getName() + "解析异常!", e);
}
}
return this.batchAdd(mapList);
}
@Override
public <E> List<E> search(String sql, List<Object> values, Class<E> e) {
if (StringUtils.isEmpty(sql))
return new ArrayList<E>();
if (values == null)
values = new ArrayList<Object>();
logger.info("sql : " + sql + " values:" + values);
return this.getJdbcTemplate().query(sql, values.toArray(),
new ModelPropertyRowMapper<E>(e, property2ColumnMap));
}
@Override
public <E> PageBean<E> search(String sql, List<Object> values,
PageBean<E> pageBean, Class<E> e) {
if (StringUtils.isEmpty(sql)) {
List<E> reList = new ArrayList<E>();
pageBean.setRowCount(0);
pageBean.setList(reList);
return pageBean;
}
if (values == null)
values = new ArrayList<Object>();
int startRow = pageBean.getStartRow();
int offset = pageBean.getPageSize();
StringBuilder sb = new StringBuilder(sql);
if (StringUtils.isNotEmpty(pageBean.getGroupby())) {
sb.append(" group by ");
sb.append(pageBean.getGroupby());
}
int count = this.getCount(sb.toString(), values);
pageBean.setRowCount(count);
if (!StringUtils.isEmpty(pageBean.getOrderBy())) {
sb.append(" order by ");
sb.append(pageBean.getOrderBy());
if (!StringUtils.isEmpty(pageBean.getOrderType())) {
sb.append(" ");
sb.append(pageBean.getOrderType());
}
}
sb.append(" limit ");
sb.append(startRow);
sb.append(",");
sb.append(offset);
pageBean.setList(this.search(sb.toString(), values, e));
return pageBean;
}
@Override
public List<T> search(T t) {
if (t == null)
return new ArrayList<T>();
Map<String, Object> map;
try {
map = toModelMap(t);
for (String proterty : transientPropertys) {
map.remove(proterty);
}
} catch (Exception e) {
throw new DaoException(persistentClass.getName() + "解析异常!", e);
}
return this.search(map);
}
@Override
public T searchOne(T t) {
if (t == null)
return null;
Map<String, Object> map;
try {
map = toModelMap(t);
map.remove("class");
for (String proterty : transientPropertys) {
map.remove(proterty);
}
} catch (Exception e) {
throw new DaoException(persistentClass.getName() + "解析异常!", e);
}
return this.searchOne(map);
}
@Override
public List<Map<String, Object>> searchForMap(String sql,
List<Object> values) {
if (StringUtils.isEmpty(sql))
return new ArrayList<Map<String, Object>>();
if (values == null)
values = new ArrayList<Object>();
if (log.isInfoEnabled()) {
log.info("sql : " + sql + " values:" + values);
}
return this.getJdbcTemplate().queryForList(sql, values.toArray());
}
@Override
public PageBean<Map<String, Object>> searchForMap(String sql,
List<Object> values, PageBean<Map<String, Object>> pageBean) {
if (StringUtils.isEmpty(sql)) {
List<Map<String, Object>> reList = new ArrayList<Map<String, Object>>();
pageBean.setRowCount(0);
pageBean.setList(reList);
return pageBean;
}
if (values == null)
values = new ArrayList<Object>();
int startRow = pageBean.getStartRow();
int offset = pageBean.getPageSize();
StringBuilder sb = new StringBuilder(sql);
if (StringUtils.isNotEmpty(pageBean.getGroupby())) {
sb.append(" group by ");
sb.append(pageBean.getGroupby());
}
int count = this.getCount(sb.toString(), values);
pageBean.setRowCount(count);
if (!StringUtils.isEmpty(pageBean.getOrderBy())) {
sb.append(" order by ");
sb.append(pageBean.getOrderBy());
if (!StringUtils.isEmpty(pageBean.getOrderType())) {
sb.append(" ");
sb.append(pageBean.getOrderType());
}
}
sb.append(" limit ");
sb.append(startRow);
sb.append(",");
sb.append(offset);
pageBean.setList(this.searchForMap(sb.toString(), values));
return pageBean;
}
private void deleteLastStr(StringBuilder sb, String str) {
int index = sb.lastIndexOf(str);
if (index != -1) {
sb.delete(index, index + str.length());
}
}
private Map<String, Object> toModelMap(T t){
Map<String, Object> modelMap = null;
try {
Map<String, Object> map = BeanUtilsBean.describe(t);
modelMap = new HashMap<String, Object>();
for(Entry<String, Object> e: map.entrySet()){
modelMap.put(property2ColumnMap.get(e.getKey()), e.getValue());
}
} catch (Exception e) {
throw new DaoException(persistentClass.getName() + "解析异常!", e);
}
return modelMap;
}
}

评论列表( 0 )

你可以在登录后,对此项目发表评论