SpringJdbc持久层封装,Spring jdbcTemplate封装,springJdbc泛型Dao,Spring baseDao封装
 
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
?Copyright 蕃薯耀 2017年7月6日
http://www.cnblogs.com/fanshuyao/
 
很久之前弄的Spring Jdbc持久化层的baseDao,现在做个记录。
基本的增、删、查、改、分页、排序都可以用,只是兼容一般,如主键必须是id,并没有深入再封装。
 封装基于Spring4+Mysql

- package com.lqy.spring.dao.impl;  
 
-   
 
- import java.io.Serializable;  
 
- import java.lang.reflect.Field;  
 
- import java.lang.reflect.Method;  
 
- import java.util.ArrayList;  
 
- import java.util.LinkedHashMap;  
 
- import java.util.List;  
 
- import java.util.Map;  
 
-   
 
- import org.springframework.beans.BeanWrapper;  
 
- import org.springframework.beans.factory.annotation.Autowired;  
 
- import org.springframework.jdbc.core.BeanPropertyRowMapper;  
 
- import org.springframework.jdbc.core.JdbcTemplate;  
 
-   
 
- import com.lqy.Utils.EntityUtils;  
 
- import com.lqy.Utils.StrUtils;  
 
- import com.lqy.spring.bean.Page;  
 
- import com.lqy.spring.dao.BaseDao;  
 
- import com.lqy.spring.editor.GenderEditor;  
 
- import com.lqy.spring.entity.SqlEntity;  
 
- import com.lqy.spring.enums.Gender;  
 
-   
 
- @SuppressWarnings({"unchecked","rawtypes"})  
 
- public class BaseDaoImpl<T> implements BaseDao<T> {  
 
-       
 
-       
 
-     @Autowired  
 
-     JdbcTemplate jdbcTemplate;  
 
-       
 
-     
 
-     
 
-     protected Class<T> entityClass = (Class<T>) EntityUtils.getEntityClass(this.getClass());  
 
-   
 
-     
 
-       
 
-     private BeanPropertyRowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(entityClass){  
 
-         @Override  
 
-         protected void initBeanWrapper(BeanWrapper bw) {  
 
-             bw.registerCustomEditor(Gender.class, new GenderEditor());  
 
-             super.initBeanWrapper(bw);  
 
-         }  
 
-     };  
 
-       
 
-     
 
-     @Override  
 
-     public T get(Serializable id) {  
 
-         String sql = getSql() + "and id=? ";  
 
-         return (T)jdbcTemplate.queryForObject(sql, rowMapper, id);  
 
-     }  
 
-   
 
-     
 
-     @Override  
 
-     public List<T> query() {  
 
-         return (List<T>) jdbcTemplate.query(getSql(), rowMapper);  
 
-     }  
 
-   
 
-     
 
-     @Override  
 
-     public List<T> query(Page page, String whereSql, List<Object> params) {  
 
-         List<Object> paramList = new ArrayList<Object>();  
 
-         if(!StrUtils.isEmpty(whereSql) && !StrUtils.isEmpty(params)){  
 
-             for (Object object : params) {  
 
-                 if(object instanceof Enum){  
 
-                     paramList.add(((Enum)object).ordinal());  
 
-                 }else{  
 
-                     paramList.add(object);  
 
-                 }  
 
-             }  
 
-         }  
 
-         String sql = getSql(page, whereSql, null);  
 
-         dealPage(page, sql, paramList);  
 
-           
 
-         if(!StrUtils.isEmpty(page)){  
 
-             paramList.add(page.getOffSize());  
 
-             paramList.add(page.getCurrentSize());  
 
-         }  
 
-         return (List<T>)jdbcTemplate.query(sql, rowMapper, paramList.toArray());  
 
-     }  
 
-   
 
-     
 
-     @Override  
 
-     public List<T> query(Page page, LinkedHashMap<String, String> orderby) {  
 
-         List<Object> paramsList = new ArrayList<Object>();  
 
-           
 
-         String sql = getSql(page, null, orderby);  
 
-         dealPage(page, sql, paramsList);  
 
-           
 
-         if(!StrUtils.isEmpty(page)){  
 
-             paramsList.add(page.getOffSize());  
 
-             paramsList.add(page.getCurrentSize());  
 
-         }  
 
-         return (List<T>)jdbcTemplate.query(sql, rowMapper, paramsList.toArray());  
 
-     }  
 
-   
 
-     
 
-     @Override  
 
-     public List<T> query(Page page, String whereSql, List<Object> params, LinkedHashMap<String, String> orderby) {  
 
-         List<Object> paramsList = new ArrayList<Object>();  
 
-         if(!StrUtils.isEmpty(whereSql) && !StrUtils.isEmpty(params)){  
 
-             for (Object object : params) {  
 
-                 if(object instanceof Enum){  
 
-                     paramsList.add(((Enum)object).ordinal());  
 
-                 }else{  
 
-                     paramsList.add(object);  
 
-                 }  
 
-             }  
 
-         }  
 
-           
 
-         String sql = getSql(page, whereSql, orderby);  
 
-         
 
-         dealPage(page, sql, paramsList);  
 
-           
 
-         if(!StrUtils.isEmpty(page)){  
 
-             paramsList.add(page.getOffSize());  
 
-             paramsList.add(page.getCurrentSize());  
 
-         }  
 
-           
 
-         return (List<T>)jdbcTemplate.query(sql, rowMapper, paramsList.toArray());  
 
-     }  
 
-   
 
-     
 
-     @Override  
 
-     public int update(String sql, List<Object> params) {  
 
-         
 
-         return jdbcTemplate.update(sql, params.toArray());  
 
-     }  
 
-       
 
-     
 
-     @Override  
 
-     public int update(T t) throws Exception{  
 
-         SqlEntity sqlEntity = getUpdateSql(t);  
 
-         
 
-         return jdbcTemplate.update(sqlEntity.getSql(), sqlEntity.getParams().toArray());  
 
-     }  
 
-       
 
-     
 
-     @Override  
 
-     public int update(T value,T template) throws Exception{  
 
-         SqlEntity sqlEntity = getUpdateSql(value,template);  
 
-         
 
-         return jdbcTemplate.update(sqlEntity.getSql(), sqlEntity.getParams().toArray());  
 
-     }  
 
-       
 
-     
 
-     @Override  
 
-     public int save(T t) throws Exception{  
 
-         SqlEntity sqlEntity = getSaveSql(t);  
 
-         return jdbcTemplate.update(sqlEntity.getSql(), sqlEntity.getParams().toArray());  
 
-     };  
 
-   
 
-     
 
-     @Override  
 
-     public int save(String sql, List<Object> params) {  
 
-         
 
-         return jdbcTemplate.update(sql, params.toArray());  
 
-     }  
 
-   
 
-     
 
-     @Override  
 
-     public int delete(Serializable id) {  
 
-         String sql="delete from " + StrUtils.changeName(this.entityClass.getSimpleName()) + " where id=?";  
 
-         return jdbcTemplate.update(sql, id);  
 
-     }  
 
-       
 
-     @SuppressWarnings("deprecation")  
 
-     @Override  
 
-     public int getCount(String whereSql, Object[] objects){  
 
-         String entityName = this.entityClass.getSimpleName();  
 
-         StringBuffer sql = new StringBuffer("select count(*) from ");  
 
-         sql.append(StrUtils.changeName(entityName));  
 
-         sql.append(" o ").append(whereSql);  
 
-         
 
-         
 
-         return jdbcTemplate.queryForInt(sql.toString(), objects);  
 
-                   
 
-     }  
 
-       
 
-     protected String getSql(){  
 
-         String entityName = this.entityClass.getSimpleName();  
 
-         StringBuffer sql = new StringBuffer("select * from ");  
 
-         sql.append(StrUtils.changeName(entityName));  
 
-         sql.append(" o where 1=1 ");  
 
-         return sql.toString();  
 
-     }  
 
-       
 
-     protected String getSql(String whereSql){  
 
-         String entityName = this.entityClass.getSimpleName();  
 
-         StringBuffer sql = new StringBuffer("select * from ");  
 
-         sql.append(StrUtils.changeName(entityName));  
 
-         sql.append(" o where 1=1 ");  
 
-         if(!StrUtils.isEmpty(whereSql)){  
 
-             sql.append(" ").append(whereSql);  
 
-         }  
 
-         return sql.toString();  
 
-     }  
 
-       
 
-     
 
-     protected String getSql(Page page, String whereSql, Map<String,String> orderby){  
 
-         String entityName = this.entityClass.getSimpleName();  
 
-         StringBuffer sql = new StringBuffer("select * from ");  
 
-         sql.append(StrUtils.changeName(entityName));  
 
-         sql.append(" o where 1=1 ");  
 
-         if(!StrUtils.isEmpty(whereSql)){  
 
-             sql.append(" ").append(whereSql);  
 
-         }  
 
-         if(!StrUtils.isEmpty(orderby)){  
 
-             sql.append(" ORDER BY ");  
 
-             for (String string : orderby.keySet()) {  
 
-                 String value = orderby.get(string);  
 
-                 if(StrUtils.isEmpty(value)){  
 
-                     value = "ASC";  
 
-                 }  
 
-                 sql.append("o.").append(string).append(" ").append(value.toUpperCase()).append(",");  
 
-             }  
 
-             if(sql.indexOf(",") > -1){  
 
-                 sql.deleteCharAt(sql.length()-1);  
 
-             }  
 
-         }  
 
-         if(!StrUtils.isEmpty(page)){  
 
-             sql.append(" limit ?,? ");  
 
-         }  
 
-         
 
-         return sql.toString();  
 
-     }  
 
-       
 
-     private SqlEntity getUpdateSql(T t) throws Exception{  
 
-         SqlEntity sqlEntity = new SqlEntity();  
 
-         sqlEntity.setParams(new ArrayList<Object>());  
 
-         Field[] fields = entityClass.getDeclaredFields();  
 
-         StringBuffer sql = new StringBuffer("");  
 
-         sql.append("update ").append(StrUtils.changeName(entityClass.getSimpleName())).append(" o set ");  
 
-         for (Field field : fields) {  
 
-             StringBuffer methodName = new StringBuffer("");  
 
-             
 
-             if(field.getType() == boolean.class){  
 
-                 if(field.getName().contains("is")){  
 
-                     methodName.append(field.getName());  
 
-                 }else{  
 
-                     methodName.append("is").append(StrUtils.firstCodeToUpperCase(field.getName()));  
 
-                 }  
 
-             }else{  
 
-                 methodName.append("get").append(StrUtils.firstCodeToUpperCase(field.getName()));  
 
-             }  
 
-             if(!"id".equals(field.getName())){  
 
-                 Method method = entityClass.getMethod(methodName.toString(), new Class[]{});  
 
-                 Object objectValue = method.invoke(t, new Object[]{});  
 
-                 if(objectValue instanceof Enum){  
 
-                     sqlEntity.getParams().add(((Enum)objectValue).ordinal());  
 
-                 }else{  
 
-                     sqlEntity.getParams().add(objectValue);  
 
-                 }  
 
-                 sql.append(" o.").append(StrUtils.changeName(field.getName())).append("= ?,");  
 
-             }  
 
-         }  
 
-         if(sql.indexOf(",") > -1){  
 
-             sql.deleteCharAt(sql.length() - 1);  
 
-         }  
 
-         sql.append(" where o.id=?");  
 
-         Method idMethod = entityClass.getMethod("getId", new Class[]{});  
 
-         sqlEntity.getParams().add(idMethod.invoke(t, new Object[]{}));  
 
-         sqlEntity.setSql(sql.toString());  
 
-         return sqlEntity;  
 
-     }  
 
-       
 
-     private SqlEntity getUpdateSql(T value, T template) throws Exception{  
 
-           
 
-         SqlEntity sqlEntity = new SqlEntity();  
 
-         sqlEntity.setParams(new ArrayList<Object>());  
 
-         Field[] fields = entityClass.getDeclaredFields();  
 
-         StringBuffer sql = new StringBuffer("");  
 
-         sql.append("update ").append(StrUtils.changeName(entityClass.getSimpleName())).append(" o set ");  
 
-         StringBuffer whereSql = new StringBuffer(" where ");  
 
-         for (Field field : fields) {  
 
-             StringBuffer methodName = new StringBuffer("");  
 
-             
 
-             if(field.getType() == boolean.class){  
 
-                 if(field.getName().contains("is")){  
 
-                     methodName.append(field.getName());  
 
-                 }else{  
 
-                     methodName.append("is").append(StrUtils.firstCodeToUpperCase(field.getName()));  
 
-                 }  
 
-             }else{  
 
-                 methodName.append("get").append(StrUtils.firstCodeToUpperCase(field.getName()));  
 
-             }  
 
-             if(!"id".equals(field.getName())){  
 
-                 Method method = entityClass.getMethod(methodName.toString(), new Class[]{});  
 
-                 Object objectValue = method.invoke(value, new Object[]{});  
 
-                 if(!StrUtils.isEmpty(objectValue)){  
 
-                     if(objectValue instanceof Enum){  
 
-                         sqlEntity.getParams().add(((Enum)objectValue).ordinal());  
 
-                     }else{  
 
-                         sqlEntity.getParams().add(objectValue);  
 
-                     }  
 
-                     
 
-                     sql.append(" o.").append(StrUtils.changeName(field.getName())).append("= ?,");  
 
-                 }  
 
-             }  
 
-         }  
 
-           
 
-         for (Field field : fields) {  
 
-             StringBuffer methodName = new StringBuffer("");  
 
-             if(field.getType() == boolean.class){  
 
-                 if(field.getName().contains("is")){  
 
-                     methodName.append(field.getName());  
 
-                 }else{  
 
-                     methodName.append("is").append(StrUtils.firstCodeToUpperCase(field.getName()));  
 
-                 }  
 
-             }else{  
 
-                 methodName.append("get").append(StrUtils.firstCodeToUpperCase(field.getName()));  
 
-             }  
 
-             Method method = entityClass.getMethod(methodName.toString(), new Class[]{});  
 
-             Object objectValue = method.invoke(template, new Object[]{});  
 
-             if(!StrUtils.isEmpty(objectValue)){  
 
-                 sqlEntity.getParams().add(objectValue);  
 
-                 whereSql.append(" o.").append(StrUtils.changeName(field.getName())).append("= ? and");  
 
-             }  
 
-         }  
 
-         if(sql.indexOf(",") > -1){  
 
-             sql.deleteCharAt(sql.length() - 1);  
 
-         }  
 
-         if(whereSql.indexOf("and") > -1){  
 
-             sql.append(whereSql.substring(0, whereSql.length()-3));  
 
-             whereSql = new StringBuffer();  
 
-         }else{  
 
-             sql.append(whereSql);  
 
-         }  
 
-         sqlEntity.setSql(sql.toString());  
 
-         return sqlEntity;  
 
-     }  
 
-       
 
-     private SqlEntity getSaveSql(T t) throws Exception{  
 
-         SqlEntity sqlEntity = new SqlEntity();  
 
-         sqlEntity.setParams(new ArrayList<Object>());  
 
-         Field[] fields = entityClass.getDeclaredFields();  
 
-         StringBuffer sql = new StringBuffer("");  
 
-         sql.append("insert into ").append(StrUtils.changeName(entityClass.getSimpleName())).append(" ( ");  
 
-         int paramLength = 0;  
 
-         for (Field field : fields) {  
 
-             StringBuffer methodName = new StringBuffer("");  
 
-             if(field.getType() == boolean.class){  
 
-                 if(field.getName().contains("is")){  
 
-                     methodName.append(field.getName());  
 
-                 }else{  
 
-                     methodName.append("is").append(StrUtils.firstCodeToUpperCase(field.getName()));  
 
-                 }  
 
-             }else{  
 
-                 methodName.append("get").append(StrUtils.firstCodeToUpperCase(field.getName()));  
 
-             }  
 
-             Method method = entityClass.getMethod(methodName.toString(), new Class[]{});  
 
-             Object value = method.invoke(t, new Object[]{});  
 
-             if(!StrUtils.isEmpty(value)){  
 
-                 if(value instanceof Enum){  
 
-                     sqlEntity.getParams().add(((Enum) value).ordinal());  
 
-                 }else{  
 
-                     sqlEntity.getParams().add(value);  
 
-                 }  
 
-                 sql.append("`").append(StrUtils.changeName(field.getName())).append("`").append(",");  
 
-                 paramLength ++;  
 
-             }  
 
-         }  
 
-         if(sql.indexOf(",") > -1){  
 
-             sql.deleteCharAt(sql.length() - 1);  
 
-         }  
 
-         sql.append(") values(");  
 
-         for (int i=0;i<paramLength;i++) {  
 
-             sql.append("?,");  
 
-         }  
 
-         if(sql.indexOf(",") > -1){  
 
-             sql.deleteCharAt(sql.length() - 1);  
 
-         }  
 
-         sql.append(")");  
 
-         
 
-         sqlEntity.setSql(sql.toString());  
 
-         return sqlEntity;  
 
-     }  
 
-       
 
-     private void dealPage(Page page, String sql, List<Object> params){  
 
-         String whereSql = "";  
 
-         if(sql != null && !sql.trim().equals("")){  
 
-             int whereIndex = sql.toLowerCase().indexOf("where");  
 
-             int orderIndex = sql.toLowerCase().indexOf("order");  
 
-             int limitIndex = sql.toLowerCase().indexOf("limit");  
 
-             if(whereIndex > -1){  
 
-                 whereSql = sql.substring(whereIndex, sql.length());  
 
-                 orderIndex = whereSql.toLowerCase().indexOf("order");  
 
-             }  
 
-             if(whereIndex > -1 && orderIndex > -1){  
 
-                 whereSql = whereSql.substring(0, orderIndex - 1);  
 
-                 limitIndex = whereSql.toLowerCase().indexOf("limit");  
 
-             }  
 
-             if(whereIndex > -1 && limitIndex > -1){  
 
-                 whereSql = whereSql.substring(0, limitIndex - 1);  
 
-             }  
 
-         }  
 
-         if(page.getTotalSizeNew()){  
 
-             page.setTotalSize(getCount(whereSql, params.toArray()));  
 
-         }  
 
-         setPage(page);  
 
-     }  
 
-       
 
-     private void setPage(Page page){  
 
-         page.setTotalPages(page.getTotalSize()%page.getCurrentSize()==0?page.getTotalSize()/page.getCurrentSize():(page.getTotalSize()/page.getCurrentSize()+1));  
 
-         page.setCurrentPage(page.getOffSize()/page.getCurrentSize()+1);  
 
-     }  
 
- }  
 
 
 
 
- package com.lqy.spring.dao;  
 
-   
 
- import java.io.Serializable;  
 
- import java.util.LinkedHashMap;  
 
- import java.util.List;  
 
-   
 
- import com.lqy.spring.bean.Page;  
 
-   
 
-   
 
- public interface BaseDao<T> {  
 
-   
 
-     public T get(Serializable id);  
 
-       
 
-     public List<T> query();  
 
-       
 
-     public List<T> query(Page page, String whereSql, List<Object> params);  
 
-       
 
-     public List<T> query(Page page, LinkedHashMap<String, String> orderby);  
 
-       
 
-     public List<T> query(Page page, String whereSql, List<Object> params, LinkedHashMap<String, String> orderby);  
 
-       
 
-     public int update(String sql, List<Object> params);  
 
-       
 
-     public int update(T t) throws Exception;  
 
-       
 
-     public int update(T value,T template) throws Exception;  
 
-       
 
-     public int save(T t) throws Exception;  
 
-       
 
-     public int save(String sql, List<Object> params);  
 
-       
 
-     public int delete(Serializable id);  
 
-       
 
-     public int getCount(String whereSql, Object[] objects);  
 
- }  
 
 
 
- package com.lqy.spring.service.impl;  
 
-   
 
- import java.io.Serializable;  
 
- import java.util.LinkedHashMap;  
 
- import java.util.List;  
 
-   
 
- import org.springframework.beans.factory.annotation.Autowired;  
 
- import org.springframework.transaction.annotation.Isolation;  
 
- import org.springframework.transaction.annotation.Transactional;  
 
-   
 
- import com.lqy.spring.bean.Page;  
 
- import com.lqy.spring.dao.BaseDao;  
 
- import com.lqy.spring.service.BaseService;  
 
-   
 
- @Transactional  
 
- public class BaseServiceImpl<T> implements BaseService<T> {  
 
-   
 
-     @Autowired  
 
-     BaseDao<T> baseDao;  
 
-       
 
-     
 
-     @Transactional(isolation=Isolation.READ_COMMITTED,  
 
-             readOnly=true)  
 
-     @Override  
 
-     public T get(Serializable id) {  
 
-         return baseDao.get(id);  
 
-     }  
 
-   
 
-     
 
-     @Transactional(isolation=Isolation.READ_COMMITTED,readOnly=true)  
 
-     @Override  
 
-     public List<T> query() {  
 
-         return baseDao.query();  
 
-     }  
 
-   
 
-     
 
-     @Transactional(isolation=Isolation.READ_COMMITTED,  
 
-             readOnly=true)  
 
-     @Override  
 
-     public List<T> query(Page page, String whereSql, List<Object> params) {  
 
-         return baseDao.query(page, whereSql, params);  
 
-     }  
 
-   
 
-     
 
-     @Transactional(isolation=Isolation.READ_COMMITTED,  
 
-             readOnly=true)  
 
-     @Override  
 
-     public List<T> query(Page page, LinkedHashMap<String, String> orderby) {  
 
-         return baseDao.query(page, orderby);  
 
-     }  
 
-   
 
-     
 
-     @Transactional(isolation=Isolation.READ_COMMITTED,  
 
-             readOnly=true)  
 
-     @Override  
 
-     public List<T> query(Page page, String whereSql, List<Object> params,  
 
-             LinkedHashMap<String, String> orderby) {  
 
-         return baseDao.query(page, whereSql, params, orderby);  
 
-     }  
 
-   
 
-     
 
-     @Override  
 
-     public int update(String sql, List<Object> params) {  
 
-         return baseDao.update(sql, params);  
 
-     }  
 
-   
 
-     
 
-     @Override  
 
-     public int update(T t) throws Exception {  
 
-         return baseDao.update(t);  
 
-     }  
 
-       
 
-     
 
-     @Override  
 
-     public int update(T value,T template) throws Exception{  
 
-         return baseDao.update(value,template);  
 
-     }  
 
-   
 
-     
 
-     @Override  
 
-     public int save(T t) throws Exception {  
 
-         return baseDao.save(t);  
 
-     }  
 
-   
 
-     
 
-     @Override  
 
-     public int save(String sql, List<Object> params) {  
 
-         return baseDao.save(sql, params);  
 
-     }  
 
-   
 
-     
 
-     @Override  
 
-     public int delete(Serializable id) {  
 
-         return baseDao.delete(id);  
 
-     }  
 
-   
 
- }  
 
 
 
- package com.lqy.spring.service;  
 
-   
 
- import java.io.Serializable;  
 
- import java.util.LinkedHashMap;  
 
- import java.util.List;  
 
-   
 
- import com.lqy.spring.bean.Page;  
 
-   
 
-   
 
- public interface BaseService<T> {  
 
-   
 
-     public T get(Serializable id);  
 
-       
 
-     public List<T> query();  
 
-       
 
-     public List<T> query(Page page, String whereSql, List<Object> params);  
 
-       
 
-     public List<T> query(Page page, LinkedHashMap<String, String> orderby);  
 
-       
 
-     public List<T> query(Page page, String whereSql, List<Object> params, LinkedHashMap<String, String> orderby);  
 
-       
 
-     public int update(String sql, List<Object> params);  
 
-       
 
-     public int update(T t) throws Exception;  
 
-       
 
-     public int update(T value,T template) throws Exception;  
 
-       
 
-     public int save(T t) throws Exception;  
 
-       
 
-     public int save(String sql, List<Object> params);  
 
-       
 
-     public int delete(Serializable id);  
 
- }  
 
 
 
- package com.lqy.spring.bean;  
 
-   
 
- public class Page {  
 
-   
 
-     private int currentSize;  
 
-     private int offSize;  
 
-     private int totalSize;  
 
-     private int totalPages;  
 
-     private int currentPage;  
 
-     private Boolean totalSizeNew;  
 
-       
 
-     
 
-     public Page() {  
 
-         super();  
 
-     }  
 
-       
 
-       
 
-     
 
-     public Page(int currentSize, int offSize) {  
 
-         super();  
 
-         this.currentSize = currentSize;  
 
-         this.offSize = offSize;  
 
-     }  
 
-   
 
-     
 
-     public Page(int currentSize, int offSize, boolean totalSizeNew) {  
 
-         super();  
 
-         this.currentSize = currentSize;  
 
-         this.offSize = offSize;  
 
-         this.totalSizeNew = totalSizeNew;  
 
-     }  
 
-   
 
-     
 
-     public Page(int currentSize, int offSize, int totalSize, int totalPages,  
 
-             int currentPage, boolean totalSizeNew) {  
 
-         super();  
 
-         this.currentSize = currentSize;  
 
-         this.offSize = offSize;  
 
-         this.totalSize = totalSize;  
 
-         this.totalPages = totalPages;  
 
-         this.currentPage = currentPage;  
 
-         this.totalSizeNew = totalSizeNew;  
 
-     }  
 
-   
 
-     public int getCurrentSize() {  
 
-         return currentSize;  
 
-     }  
 
-   
 
-     public void setCurrentSize(int currentSize) {  
 
-         this.currentSize = currentSize;  
 
-     }  
 
-   
 
-     public int getOffSize() {  
 
-         return offSize;  
 
-     }  
 
-   
 
-     public void setOffSize(int offSize) {  
 
-         this.offSize = offSize;  
 
-     }  
 
-   
 
-     public int getTotalSize() {  
 
-         return totalSize;  
 
-     }  
 
-   
 
-     public void setTotalSize(int totalSize) {  
 
-         this.totalSize = totalSize;  
 
-     }  
 
-   
 
-     public int getTotalPages() {  
 
-         return totalPages;  
 
-     }  
 
-   
 
-     public void setTotalPages(int totalPages) {  
 
-         this.totalPages = totalPages;  
 
-     }  
 
-   
 
-     public int getCurrentPage() {  
 
-         return currentPage;  
 
-     }  
 
-   
 
-     public void setCurrentPage(int currentPage) {  
 
-         this.currentPage = currentPage;  
 
-     }  
 
-   
 
-     public Boolean getTotalSizeNew() {  
 
-         return totalSizeNew;  
 
-     }  
 
-   
 
-     public void setTotalSizeNew(Boolean totalSizeNew) {  
 
-         this.totalSizeNew = totalSizeNew;  
 
-     }  
 
-   
 
-     @Override  
 
-     public String toString() {  
 
-         return "Page [currentSize=" + currentSize + ", offSize=" + offSize  
 
-                 + ", totalSize=" + totalSize + ", totalPages=" + totalPages  
 
-                 + ", currentPage=" + currentPage + ", totalSizeNew="  
 
-                 + totalSizeNew + "]";  
 
-     }  
 
-   
 
-       
 
-       
 
-       
 
-       
 
- }  
 
 
 
 
使用的一个例子:
- package com.lqy.spring.service.impl;  
 
-   
 
- import java.util.ArrayList;  
 
- import java.util.LinkedHashMap;  
 
- import java.util.List;  
 
-   
 
- import org.springframework.beans.factory.annotation.Autowired;  
 
- import org.springframework.stereotype.Service;  
 
- import org.springframework.transaction.annotation.Transactional;  
 
-   
 
- import com.lqy.Utils.StrUtils;  
 
- import com.lqy.exception.PersonMoneyNotEnoughException;  
 
- import com.lqy.spring.bean.Page;  
 
- import com.lqy.spring.bean.Person;  
 
- import com.lqy.spring.dao.PersonDao;  
 
- import com.lqy.spring.service.BookService;  
 
- import com.lqy.spring.service.PersonService;  
 
-   
 
-   
 
- @Service  
 
- public class PersonServiceImpl extends BaseServiceImpl<Person> implements PersonService {  
 
-       
 
-     @Autowired  
 
-     PersonDao personDao;  
 
-     @Autowired  
 
-     BookService bookService;  
 
-       
 
-     @Override  
 
-     public List<Person> getPersons(Page page, String name, Integer age, Integer statusType){  
 
-         StringBuffer whereSql = new StringBuffer();  
 
-         List<Object> params = new ArrayList<Object>();  
 
-         LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();  
 
-           
 
-         if(!StrUtils.isEmpty(name)){  
 
-             whereSql.append(" and o.name like ?");  
 
-             params.add("%"+name+"%");  
 
-         }  
 
-         if(!StrUtils.isEmpty(age)){  
 
-             whereSql.append(" and o.age = ?");  
 
-             params.add(age);  
 
-         }  
 
-         if(!StrUtils.isEmpty(statusType)){  
 
-             whereSql.append(" and o.statusType = ?");  
 
-             params.add(statusType);  
 
-         }  
 
-         orderby.put("create_time", "desc");  
 
-         orderby.put("id", "desc");  
 
-           
 
-         return personDao.query(page, whereSql.toString(), params, orderby);  
 
-     }  
 
-       
 
-     @Transactional  
 
-     public int buyBook(Integer personId, Double price) throws Exception{  
 
-         int result = -1;  
 
-         Person person = personDao.get(personId);  
 
-         if(!StrUtils.isEmpty(person)){  
 
-             Double leftMoney = person.getMoney() - price;  
 
-             if(leftMoney >= 0){  
 
-                 person.setMoney(leftMoney);  
 
-                 result = personDao.update(person);  
 
-             }else{  
 
-                 throw new PersonMoneyNotEnoughException();  
 
-             }  
 
-         }  
 
-         return result;  
 
-     }  
 
-       
 
-     @Transactional  
 
-     @Override  
 
-     public int buyBook(Integer personId, Integer bookId, Integer amount) throws Exception{  
 
-         int result = -1;  
 
-         Person person = personDao.get(personId);  
 
-         if(!StrUtils.isEmpty(person)){  
 
-             Double price = bookService.getBooksPrices(bookId, amount);  
 
-             Double leftMoney = person.getMoney() - price;  
 
-             if(leftMoney >= 0){  
 
-                 person.setMoney(leftMoney);  
 
-                 personDao.update(person);  
 
-                 bookService.sellBooks(bookId, amount);  
 
-                 result = 1;  
 
-             }else{  
 
-                 throw new PersonMoneyNotEnoughException();  
 
-             }  
 
-         }  
 
-         return result;  
 
-     }  
 
-       
 
- }  
 
 
 
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
?Copyright 蕃薯耀 2017年7月6日
http://www.cnblogs.com/fanshuyao/
SpringJdbc持久层封装,Spring jdbcTemplate封装,springJdbc泛型Dao,Spring baseDao封装
原文:http://www.cnblogs.com/fanshuyao/p/7127265.html