当前位置 : 主页 > 编程语言 > c++ >

java jdbc 基础dao

来源:互联网 收集:自由互联 发布时间:2021-07-03
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 {/** * 基础接口之一 批量添加接口 * * @param sql * @param va
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
 
   {

	/**
	 * 基础接口之一
  
批量添加接口 * * @param sql * @param values * @return */ public int[] batchAdd(String sql, List > values); /** * 基础接口之一
批量删除记录接口 * * @param sql * @param values * @return */ public int del(String sql, List values); /** * 基础接口之一
更新接口 * * @param sql * @param values * @return */ public int update(String sql, List values); /** * 基础接口之一
查询一个long值 * * @param sql * @param values * @return */ public long getLong(String sql, List values); /** * 基础接口之一
查询一个int值 * * @param sql * @param values * @return */ public int getInt(String sql, List values); /** * 基础接口之一
sql查询通用接口 * * @param sql * @param values * @return */ public List search(String sql, List values); /** * 基础接口之一
sql查询通用接口 * * @param sql * @param values * @param e * @return */ public List search(String sql, List values, Class e); /** * 分页查询 * * @param sql * @param values * @param pageBean * @return */ public PageBean search(String sql, List values, PageBean pageBean); /** * 分页查询 * * @param sql * @param values * @param pageBean * @return */ public PageBean search(String sql, List values, PageBean pageBean, Class e); /** * 根据主键修改数据 * * @param t * @return */ public T update(T t); /** * 批量添加接口 * * @param list * @return */ public int[] batchSave(List list); /** * 获取一条sql执行后返回的记录数 * * @param sql * @param values * @return */ public int getCount(String sql, List values); /** * 根据id值删除多条数据 * * @param ids * @return */ public int delByIds(List ids); /** * 根据id删除一条记录 * * @param id * @return */ public int del(ID id); /** * 根据id值查询多条数据 * * @param ids * @return */ public List getByIds(List ids); /** * 根据id值查询 * * @param id * @return */ public T get(ID id); /** * 获取本表所有记录 * * @return */ public List getAll(); /** * 获取主键列名 * * @return */ public String getPk(); /** * 获取表名 * * @return */ public String getTableName(); /** * 添加记录,并返回新增记录的主键
* 注意:
* 当主键生成策略为IDENTITY(数据库自增)时,SQL执行成功返回新数据主键,执行不成功返回0
* 主键生成策略为非IDENTITY(数据库自增)时,SQL执行返回值无法确定
* * @param sql * @param values * @return */ public ID addReturnId(String sql, List values); /** * 保存一个对象 * * @param t * @return */ public T save(T t); /** * sql查询通用接口 * * @param t * @return */ public List search(T t); /** * sql查询通用接口,忽略主键,仅返回查询结果集的第一个对象 * * @param t * @return */ public T searchOne(T t); /** * * @param t * @param pageBean * @return */ public PageBean search(T t, PageBean pageBean); /** * sql查询通用接口 * * @param sql * @param values * @param e * @return */ public List > searchForMap(String sql, List values); /** * map型分页查询接口 * * @param sql * @param values * @param pageBean * @return */ public PageBean > searchForMap(String sql, List values, PageBean > pageBean); } jdbc 基础dao 接口实现
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
                                     
                                       extends
		JdbcDaoSupport implements BaseDao
                                      
                                        { public final Log log = LogFactory.getLog(this.getClass()); private Class
                                       
                                         persistentClass; private String tableName = ""; private String pk = ""; private Map
                                        
                                          property2ColumnMap = new HashMap
                                         
                                          (); private Map
                                          
                                            column2PropertyMap = new HashMap
                                           
                                            (); private GenerationType strategy; protected List
                                            
                                              transientPropertys = new ArrayList
                                             
                                              (); protected BaseDaoMysqlImpl(Class
                                              
                                                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
                                               
                                                 getAll() { StringBuilder sb = new StringBuilder("select * from "); sb.append(this.getTableName()); return this.search(sb.toString(), null); } @Override public List
                                                
                                                  search(String sql, List
                                                  values) { if (StringUtils.isEmpty(sql)) return new ArrayList
                                                  
                                                   (); if (values == null) values = new ArrayList
                                                   (); logger.info("sql : " + sql + " values:" + values); List
                                                    
                                                      list = this.getJdbcTemplate().query(sql, values.toArray(), new ModelPropertyRowMapper
                                                     
                                                      (this.persistentClass, property2ColumnMap)); return list == null ? new ArrayList
                                                      
                                                       () : list; } @Override public PageBean
                                                       
                                                         search(T t, PageBean
                                                        
                                                          pageBean) { Map
                                                         
                                                           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
                                                          
                                                            search(Map
                                                           
                                                             map, PageBean
                                                            
                                                              pageBean) { ID id = (ID) map.get(this.getPk()); if (map != null && id != null) { map.remove(id); } List
                                                             
                                                               removekeys = new ArrayList
                                                              
                                                               (); for (Entry
                                                               
                                                                 entry : map.entrySet()) { if (entry.getValue() == null) { removekeys.add(entry.getKey()); } } for (String key : removekeys) { map.remove(key); } List
                                                                 values = new ArrayList(); StringBuilder sb = new StringBuilder("select * from "); sb.append(this.getTableName()); if (map.size() != 0) { sb.append(" where "); for (Entry
                                                                  
                                                                    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
                                                                   
                                                                     search(Map
                                                                    
                                                                      map) { if (map == null || map.isEmpty()) return null; @SuppressWarnings("unchecked") ID id = (ID) map.get(this.getPk()); if (id != null) { map.remove(id); } List
                                                                     
                                                                       removekeys = new ArrayList
                                                                      
                                                                       (); for (Entry
                                                                       
                                                                         entry : map.entrySet()) { if (entry.getValue() == null) { removekeys.add(entry.getKey()); } } for (String key : removekeys) { map.remove(key); } List
                                                                         values = new ArrayList(); StringBuilder sb = new StringBuilder("select * from "); sb.append(this.getTableName()); if (map.size() != 0) { sb.append(" where "); for (Entry
                                                                          
                                                                            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
                                                                           
                                                                             map) { if (map == null || map.isEmpty()) return null; @SuppressWarnings("unchecked") ID id = (ID) map.get(this.getPk()); if (id != null) { map.remove(id); } List
                                                                            
                                                                              removekeys = new ArrayList
                                                                             
                                                                              (); for (Entry
                                                                              
                                                                                entry : map.entrySet()) { if (entry.getValue() == null) { removekeys.add(entry.getKey()); } } for (String key : removekeys) { map.remove(key); } List
                                                                                values = new ArrayList(); StringBuilder sb = new StringBuilder("select * from "); sb.append(this.getTableName()); if (map.size() != 0) { sb.append(" where "); for (Entry
                                                                                 
                                                                                   entry : map.entrySet()) { sb.append(entry.getKey()); sb.append("=? "); values.add(entry.getValue()); sb.append(" and "); } this.deleteLastStr(sb, "and"); } List
                                                                                  
                                                                                    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
                                                                                    values) { if (StringUtils.isEmpty(sql)) return 0; if (values == null) values = new ArrayList(); 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 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
                                                                                      
                                                                                        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
                                                                                       
                                                                                         columns = new ArrayList
                                                                                        
                                                                                         (); List
                                                                                          values = new ArrayList(); for (Entry
                                                                                           
                                                                                             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
                                                                                            
                                                                                             > values) { if (StringUtils.isEmpty(sql)) return new int[0]; if (values == null) values = new ArrayList
                                                                                             
                                                                                              >(); if (log.isInfoEnabled()) { log.info("sql : " + sql + " values:" + values); } List
                                                                                              
                                                                                                batchArgs = new ArrayList
                                                                                               
                                                                                                (); for (List
                                                                                                 ol : values) { batchArgs.add(ol.toArray()); } return this.getJdbcTemplate().batchUpdate(sql, batchArgs); } @Override public int update(String sql, List values) { if (StringUtils.isEmpty(sql)) return 0; if (values == null) values = new ArrayList(); 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 values = new ArrayList(); values.add(id); return this.del(sb.toString(), values); } @Override public int delByIds(List
                                                                                                     
                                                                                                       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
                                                                                                       values = new ArrayList(); values.addAll(ids); return this.del(sb.toString(), values); } @Override public int del(String sql, List values) { if (log.isInfoEnabled()) { log.info("sql : " + sql + " values:" + values); } return this.getJdbcTemplate().update(sql, values.toArray()); } @Override public int getInt(String sql, List values) { if (StringUtils.isEmpty(sql)) return 0; if (values == null) values = new ArrayList(); if (log.isInfoEnabled()) { log.info("sql : " + sql + " values:" + values); } return this.getJdbcTemplate().queryForInt(sql, values.toArray()); } @Override public long getLong(String sql, List values) { if (StringUtils.isEmpty(sql)) return 0; if (values == null) values = new ArrayList(); if (log.isInfoEnabled()) { log.info("sql : " + sql + " values:" + values); } return this.getJdbcTemplate().queryForLong(sql, values.toArray()); } @Override public int getCount(String sql, List values) { if (StringUtils.isEmpty(sql)) return 0; if (values == null) values = new ArrayList(); StringBuilder sb = new StringBuilder("select count(*) from ("); sb.append(sql); sb.append(") as _tn"); return this.getInt(sb.toString(), values); } protected int[] batchAdd(List
                                                                                                               
                                                                                                                > mapList) { if (mapList == null || mapList.isEmpty()) return new int[0]; Map
                                                                                                                
                                                                                                                  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
                                                                                                                 
                                                                                                                  > values = new ArrayList
                                                                                                                  
                                                                                                                   >(); for (Map
                                                                                                                   
                                                                                                                     m : mapList) { if (m != null && !m.isEmpty()) { List
                                                                                                                     l = new ArrayList(); for (String str : columnNames) { l.add(m.get(str)); } values.add(l); } } return this.batchAdd(sb.toString(), values); } protected int updateById(Map
                                                                                                                      
                                                                                                                        map) { if (map == null || map.size() == 0) return 0; Serializable id = (Serializable) map.get(this.getPk()); if (id == null || "".equals(id)) return 0; List
                                                                                                                        values = new ArrayList(); StringBuilder sb = new StringBuilder("update "); sb.append(this.getTableName()); sb.append(" set "); map.remove(this.getPk()); for (Entry
                                                                                                                         
                                                                                                                           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
                                                                                                                          
                                                                                                                            search(String sql, List
                                                                                                                            values, PageBean
                                                                                                                            
                                                                                                                              pageBean) { if (StringUtils.isEmpty(sql)) { List
                                                                                                                             
                                                                                                                               reList = new ArrayList
                                                                                                                              
                                                                                                                               (); pageBean.setRowCount(0); pageBean.setList(reList); return pageBean; } if (values == null) values = new ArrayList
                                                                                                                               (); 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 values = new ArrayList(); values.add(id); List
                                                                                                                                  
                                                                                                                                    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
                                                                                                                                   
                                                                                                                                     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
                                                                                                                                    
                                                                                                                                      getByIds(List
                                                                                                                                     
                                                                                                                                       ids) { List
                                                                                                                                      
                                                                                                                                        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
                                                                                                                                        values = new ArrayList(); values.addAll(ids); list = this.search(sb.toString(), values); } else { list = new ArrayList
                                                                                                                                         
                                                                                                                                          (); } return list; } @Override public T update(T t) { if (t == null) { throw new DaoException("模型对象为空!"); } Map
                                                                                                                                          
                                                                                                                                            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
                                                                                                                                           
                                                                                                                                             list) { if (list == null) { throw new DaoException("模型对象为空!"); } List
                                                                                                                                            
                                                                                                                                             > mapList = new ArrayList
                                                                                                                                             
                                                                                                                                              >(); for (T t : list) { try { Map
                                                                                                                                              
                                                                                                                                                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 
                                                                                                                                               
                                                                                                                                                 List
                                                                                                                                                
                                                                                                                                                  search(String sql, List
                                                                                                                                                  values, Class
                                                                                                                                                  
                                                                                                                                                    e) { if (StringUtils.isEmpty(sql)) return new ArrayList
                                                                                                                                                   
                                                                                                                                                    (); if (values == null) values = new ArrayList
                                                                                                                                                    (); logger.info("sql : " + sql + " values:" + values); return this.getJdbcTemplate().query(sql, values.toArray(), new ModelPropertyRowMapper
                                                                                                                                                     
                                                                                                                                                      (e, property2ColumnMap)); } @Override public 
                                                                                                                                                      
                                                                                                                                                        PageBean
                                                                                                                                                       
                                                                                                                                                         search(String sql, List
                                                                                                                                                         values, PageBean
                                                                                                                                                         
                                                                                                                                                           pageBean, Class
                                                                                                                                                          
                                                                                                                                                            e) { if (StringUtils.isEmpty(sql)) { List
                                                                                                                                                           
                                                                                                                                                             reList = new ArrayList
                                                                                                                                                            
                                                                                                                                                             (); pageBean.setRowCount(0); pageBean.setList(reList); return pageBean; } if (values == null) values = new ArrayList
                                                                                                                                                             (); 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
                                                                                                                                                              
                                                                                                                                                                search(T t) { if (t == null) return new ArrayList
                                                                                                                                                               
                                                                                                                                                                (); Map
                                                                                                                                                                
                                                                                                                                                                  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
                                                                                                                                                                 
                                                                                                                                                                   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
                                                                                                                                                                  
                                                                                                                                                                   > searchForMap(String sql, List
                                                                                                                                                                    values) { if (StringUtils.isEmpty(sql)) return new ArrayList
                                                                                                                                                                    
                                                                                                                                                                     >(); if (values == null) values = new ArrayList
                                                                                                                                                                     (); if (log.isInfoEnabled()) { log.info("sql : " + sql + " values:" + values); } return this.getJdbcTemplate().queryForList(sql, values.toArray()); } @Override public PageBean
                                                                                                                                                                      
                                                                                                                                                                       > searchForMap(String sql, List
                                                                                                                                                                        values, PageBean
                                                                                                                                                                        
                                                                                                                                                                         > pageBean) { if (StringUtils.isEmpty(sql)) { List
                                                                                                                                                                         
                                                                                                                                                                          > reList = new ArrayList
                                                                                                                                                                          
                                                                                                                                                                           >(); pageBean.setRowCount(0); pageBean.setList(reList); return pageBean; } if (values == null) values = new ArrayList
                                                                                                                                                                           (); 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
                                                                                                                                                                            
                                                                                                                                                                              toModelMap(T t){ Map
                                                                                                                                                                             
                                                                                                                                                                               modelMap = null; try { Map
                                                                                                                                                                              
                                                                                                                                                                                map = BeanUtilsBean.describe(t); modelMap = new HashMap
                                                                                                                                                                               
                                                                                                                                                                                (); for(Entry
                                                                                                                                                                                
                                                                                                                                                                                  e: map.entrySet()){ modelMap.put(property2ColumnMap.get(e.getKey()), e.getValue()); } } catch (Exception e) { throw new DaoException(persistentClass.getName() + "解析异常!", e); } return modelMap; } } 
                                                                                                                                                                                
                                                                                                                                                                               
                                                                                                                                                                              
                                                                                                                                                                             
                                                                                                                                                                            
                                                                                                                                                                          
                                                                                                                                                                         
                                                                                                                                                                        
                                                                                                                                                                      
                                                                                                                                                                    
                                                                                                                                                                  
                                                                                                                                                                 
                                                                                                                                                                
                                                                                                                                                               
                                                                                                                                                              
                                                                                                                                                            
                                                                                                                                                           
                                                                                                                                                          
                                                                                                                                                         
                                                                                                                                                       
                                                                                                                                                      
                                                                                                                                                     
                                                                                                                                                   
                                                                                                                                                  
                                                                                                                                                
                                                                                                                                               
                                                                                                                                              
                                                                                                                                             
                                                                                                                                            
                                                                                                                                           
                                                                                                                                          
                                                                                                                                         
                                                                                                                                      
                                                                                                                                     
                                                                                                                                    
                                                                                                                                   
                                                                                                                                  
                                                                                                                              
                                                                                                                             
                                                                                                                            
                                                                                                                          
                                                                                                                         
                                                                                                                      
                                                                                                                   
                                                                                                                  
                                                                                                                 
                                                                                                                
                                                                                                               
                                                                                                     
                                                                                               
                                                                                              
                                                                                             
                                                                                            
                                                                                           
                                                                                        
                                                                                       
                                                                                      
                                                                                  
                                                                                 
                                                                              
                                                                             
                                                                            
                                                                           
                                                                          
                                                                       
                                                                      
                                                                     
                                                                    
                                                                   
                                                                  
                                                               
                                                              
                                                             
                                                            
                                                           
                                                          
                                                         
                                                        
                                                       
                                                      
                                                     
                                                    
                                                  
                                                
                                               
                                              
                                             
                                            
                                           
                                          
                                         
                                        
                                       
                                      
                                     
网友评论