2017-01-09 10:53
@giegie guava没研究过,还有前几天在写东西的时候发现arrayList 可以直接转array 反倒少了new array必须填生成多少个的麻烦啦,貌似guava的包不小,反倒不如java简便
2016-12-30 22:11
public class PageUtil {
public static final int DEFAULT_PAGE_SIZE = 10;
public static PageParam getPageParam(List> paramList,String tableName,String select,String orders){
PageParam pageParam = new PageParam();
int page_size = DEFAULT_PAGE_SIZE;
int page_no = 1;
String sqlExceptSelect;
Object[] param = null;
int param_count = 0;
for(Map paramMap : paramList){
for(String key:paramMap.keySet()){
if("page_size".equals(key)){
if(paramMap.get(key)[0] != null && !("".equals(paramMap.get(key)[0])))
page_size = Integer.parseInt(paramMap.get(key)[0]);
}else if("page_no".equals(key)){
if(paramMap.get(key)[0] != null && !("".equals(paramMap.get(key)[0])))
page_no = Integer.parseInt(paramMap.get(key)[0]);
}else{
if(paramMap.get(key)[0] != null && !("".equals(paramMap.get(key)[0])))
param_count ++;
}
}
}
StringBuilder sqlExceptSelect_buder = new StringBuilder();
sqlExceptSelect_buder.append("FROM "+tableName+" ");
if(param_count > 0){
param = new Object[param_count];
sqlExceptSelect_buder.append("WHERE 1=1 ");
int j = 0;
for(Map paramMap : paramList){
for(String key : paramMap.keySet()){
if(!("page_size".equals(key)) && !("page_no".equals(key))){
String[] value = paramMap.get(key);
if(value[0] != null && !("".equals(value[0]))){
sqlExceptSelect_buder.append("AND ");
sqlExceptSelect_buder.append(key + " ");
//sqlExceptSelect_buder.append((value[1] == null || "".equals(value[1]))?"=":value[1] + " ");
if(value.length > 1){
if(value[1] == null || "".equals(value[1])){
sqlExceptSelect_buder.append("= ");
sqlExceptSelect_buder.append("? ");
param[j] = value[0];
}else if("like".equals(value[1].toLowerCase())){
sqlExceptSelect_buder.append("like ");
sqlExceptSelect_buder.append("?");
sqlExceptSelect_buder.append(" ");
param[j] = "%"+value[0]+"%";
}else{
sqlExceptSelect_buder.append(value[1]+" ");
sqlExceptSelect_buder.append("? ");
param[j] = value[0];
}
}else{
sqlExceptSelect_buder.append("= ");
sqlExceptSelect_buder.append("? ");
param[j] = value[0];
}
j++;
}
}
}
}
}
if(orders != null && !("".equals(orders))){
sqlExceptSelect_buder.append(" "+orders);
}
sqlExceptSelect = sqlExceptSelect_buder.toString();
pageParam.setPageNumber(page_no);
pageParam.setPageSize(page_size);
pageParam.setParam(param);
pageParam.setSqlExceptSelect(sqlExceptSelect);
if(select != null && !("".equals(select))){
pageParam.setSelect(select);
}else{
pageParam.setSelect("SELECT *");
}
return pageParam;
}
public static List> getParammapList(Map paraMap){
List> paramList = new ArrayList>();
if(paraMap != null){
Set keySet = paraMap.keySet();
for(String key :keySet){
Map paramMap = new HashMap();
String format_key = key.replace("[]", "");
paramMap.put(format_key, paraMap.get(key));
paramList.add(paramMap);
}
return paramList;
}else{
return null;
}
}
}
2016-12-30 22:11
public class PageParam {
int pageNumber;
int pageSize;
String select;
String sqlExceptSelect;
Object[] param;
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public String getSelect() {
return select;
}
public void setSelect(String select) {
this.select = select;
}
public String getSqlExceptSelect() {
return sqlExceptSelect;
}
public void setSqlExceptSelect(String sqlExceptSelect) {
this.sqlExceptSelect = sqlExceptSelect;
}
public Object[] getParam() {
return param;
}
public void setParam(Object[] param) {
this.param = param;
}
}