类图关系链
简约说明:
- ListAdapter 接口 继承 FindAdapter接口 (接口扩展)
- DefaultFindAdapter接口 实现 FindAdapter接口
- DefalutListAdapter接口 继承 DefaultFindAdapter接口
- DefalutListAdapter接口 实现 ListAdapter 接口
FindAdapter
package com.ea.manage.base.common;
import java.util.Map;
public interface FindAdapter<T> {
public static final String ORDER_TYPE_ASC = "ASC";
/**
* 把传入的对象转化为Map
* @param clazz
* @return
*/
public Map<String, Object> convert(T clazz);
/**
* 逐个设置动态参数
* @param key
* @param value
*/
public FindAdapter<T> setFiled(String key, Object value);
/**
* 批量设置动态参数
* @param dynamicFileds
*/
public FindAdapter<T> setDynamicFileds(Map<String, Object> dynamicFileds);
}
ListAdapter extends FindAdapter
ListAdapter接口继承FindAdapter接口 (接口扩展,接口可以多继承)
package com.ea.manage.base.common;
/**
* Created by Administrator on 2015/10/19 0019.
*/
public interface ListAdapter<T> extends FindAdapter<T> {
/**
* 获取最大页数
* @return
*/
public int getMaxPageNo();
/**
* 设置排序字段
* @param orderItem
*/
public ListAdapter<T> setOrderItem(String orderItem);
/**
* 设置排序类型
* @param orderType
*/
public ListAdapter<T> setOrderType(String orderType);
/**
* 开始页数
* @param pageNo
*/
public ListAdapter<T> setPageNo(int pageNo);
/**
* 获取总条数
* @return
*/
public int getItemTotal();
/**
* 设置总条数
* @param itemTotal
*/
public ListAdapter<T> setItemTotal(int itemTotal);
/**
* 设置查询返回条数
* @param pageSize
*/
public ListAdapter<T> setPageSize(Integer pageSize);
}
DefaultFindAdapter implements FindAdapter
package com.ea.manage.base.common;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class DefaultFindAdapter<T> implements FindAdapter<T>{
private static final transient Logger logger = LoggerFactory.getLogger(DefaultFindAdapter.class);
public Map<String, Object> convert(T clazz) {
//把Bean转化成Map
Map<String,Object> rtnMap = bean2ParamterMap(clazz);
//动态自动的内容设置到Paramter Map中
if(dynamicFileds != null && dynamicFileds.size() > 0 ){
Iterator<String> it = dynamicFileds.keySet().iterator();
while(it.hasNext()){
String key = it.next();
rtnMap.put("dynamicFileds_"+key, dynamicFileds.get(key));
}
}
return rtnMap;
}
/**
* 把对象属性设置到参数中去
* @param clazz
* @return
* @throws Exception
*/
public Map<String , Object> bean2ParamterMap(T clazz){
Map<String,Object> rtnMap = new HashMap<String,Object>();
//修复 NullpointException Bug
if(clazz == null){
return rtnMap;
}
try{
BeanInfo bi = Introspector.getBeanInfo(clazz.getClass());
PropertyDescriptor[] pd = bi.getPropertyDescriptors();
for(int i = 0 ; i < pd.length ; i++){
String name = pd[i].getName();
Method method = pd[i].getReadMethod();
Object value = method.invoke(clazz);
if(!"class".equals(name)){
rtnMap.put(name, value);
}
}
}catch(IntrospectionException ie){
logger.error("BeanInfo error :",ie);
}catch(Exception e){
logger.error("Method error :",e);
}
return rtnMap;
}
>
/**
* 动态字段
*/
protected Map<String,Object> dynamicFileds;
public FindAdapter<T> setDynamicFileds(Map<String, Object> dynamicFileds) {
if(dynamicFileds == null){
dynamicFileds = new HashMap<String,Object>();
}
this.dynamicFileds.putAll(dynamicFileds);
return this;
}
public Map<String,Object> getDynamicFileds(){
if(null != dynamicFileds && dynamicFileds.size()>0){
return dynamicFileds;
}else{
return new HashMap<String,Object>();
}
}
/**
* 向动态字段中添加内容
* @param key
* @param value
*/
public FindAdapter<T> setFiled(String key,Object value){
if(null == dynamicFileds){
dynamicFileds = new HashMap<String,Object>();
}
dynamicFileds.put(key, value);
return this;
}
/**
* 获取动态字段的内容
* @param key
* @return
*/
public Object getFiled(String key){
if(null == dynamicFileds){
return null;
}
return dynamicFileds.get(key);
}
}
DefaultListAdapter extends DefaultFindAdapter implements ListAdapter
package com.ea.manage.base.common;
import java.util.HashMap;
import java.util.Map;
public class DefaultListAdapter<T> extends DefaultFindAdapter<T> implements ListAdapter<T>{
@Override
public Map<String, Object> convert(T clazz) {
Map<String,Object> rtnMap = super.convert(clazz);
//list参数设置到map中去
rtnMap = covertListParater(rtnMap);
return rtnMap;
}
/**
* 获取最大页数
* @return
*/
public int getMaxPageNo(){
if(itemTotal == 0){
return 1;
}else{
return itemTotal/pageSize + (itemTotal/pageSize ==0?0:1);
}
}
//getter and setter methods
public Integer getBegin() {
if(pageNo <= 1){
begin = 0;
}else{
begin = (pageNo - 1) * pageSize;
}
return begin;
}
public Integer getPageSize() {
return pageSize;
}
public ListAdapter<T> setPageSize(Integer pageSize) {
this.pageSize = pageSize;
return this;
}
public int getPageNo() {
return pageNo;
}
public ListAdapter<T> setPageNo(int pageNo) {
this.pageNo = pageNo;
return this;
}
public int getItemTotal() {
return itemTotal;
}
public ListAdapter<T> setItemTotal(int itemTotal) {
this.itemTotal = itemTotal;
return this;
}
public String getOrderItem() {
return orderItem;
}
public ListAdapter<T> setOrderItem(String orderItem) {
orderItem = orderItem.trim();
if(orderItem.indexOf(" ") > -1){
throw new RuntimeException("setOrderItem error , orderItem has space! but is not allowed!");
}
this.orderItem = orderItem;
return this;
}
public String getOrderType() {
return orderType;
}
public ListAdapter<T> setOrderType(String orderType) {
if(!OrderType.validate(orderType)){
this.orderType = OrderType.DESC.getName();
}
this.orderType = orderType;
return this;
}
/**
* 把list查询参数传入到Paramter map中
* @param dynamicFileds
* @return
*/
protected Map<String , Object> covertListParater(Map<String , Object> dynamicFileds){
if(dynamicFileds == null){
dynamicFileds = new HashMap<String , Object>();
}
dynamicFileds.put("begin", getBegin());
dynamicFileds.put("pageSize", pageSize);
dynamicFileds.put("orderItem", orderItem);
dynamicFileds.put("orderType", orderType);
return dynamicFileds;
}
protected static final int PAGE_SIZE = 20;
//分页开始页数,和pageNo相关
protected Integer begin = 0;
//每页条数
protected Integer pageSize = PAGE_SIZE;
//第几页
protected int pageNo = 1;
protected int itemTotal = 0;
//排序的字段
protected String orderItem;
//排序类型
protected String orderType = OrderType.DESC.getName();
protected enum OrderType {
ASC("ASC"),
DESC("DESC");
private OrderType(String name){
this.name = name;
}
public static Map<String,OrderType> orderTypeMaps = new HashMap<String,OrderType>();
static{
for(OrderType orderType: OrderType.values()){
orderTypeMaps.put(orderType.getName(), orderType);
}
}
public static boolean validate(String orderType){
if(orderTypeMaps.get(orderType) == null){
return false;
}
return true;
}
private String name;
public String getName() {
return name;
}
}
}