前几天看了Nutz的框架写的真不错.方便..既有springMVC的优雅.也有Hibernate的简洁
但是在查询的时候.没有封装类似springside的页面查询.于是闲着趁礼拜六日自己写了个通用的action..写的时候一直很迷茫.我是不是又回到jsp时代了..暂且作为一个分支吧...
具体的可行性..待定.希望大家给点意见
首先是个通用的action 他返回的是json格式..
下面是使用的简介
查询说明:
/superjson/list/Person?page.pageNo=1&Cnd_AND_age_GT=20&Cnd_AND_name_LIKE=ansj
这个意思就是.查找Person对象.pageNo=1:从第一页 Cnd_AND_age_GT=20 年龄大于20 Cnd_OR_name_LIKE=ansj 或者名字like ansj
目前支持EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , ASC, DESC ;
查询单一对象
/superjson/findOne/Person?Cnd_AND_id_EQ=1
这个意思就是.查找Person对象. Cnd_AND_id_EQ=1 id=1的对象.可以有其他条件但是只
返回第一个对象
保存
/superjson/save/Person/person?person.name=ansj&person.age=20
这个意思就是插入Person对象.person对象的参数前缀是ansj. 这样就构造一个person对象并且保存
更新
/superjson/update/Person/person?person.name=ansj&person.age=20&person.id=1
这个意思就是更新Person对象.person对象的参数前缀是ansj. 这样就构造一个person对象并且更新
删除
/superjson/delete/Person/person?Cnd_AND_id_EQ=1*Cnd_OR_name_LIKE=ansj
删除一批对象.这个条件是id=1 或者name like ansj
package com.enilu.shop.controller;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.nutz.dao.Condition;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import com.enilu.shop.util.InjectByClass;
import com.enilu.shop.util.Page;
import com.enilu.shop.util.PageSqlUtil;
/**
* 通用的JSON返回JSON的字符串格式
* @author ansj
* @mail [email protected]
*/
@IocBean
@InjectName
public class SuperJSONAction extends BaseAction {
static final String BEANPATH = "com.enilu.shop.bean.";
/**
* 通用的查找action 构建网址必须按照此格式/superjson/list/类名?page.pageSize=10...
* 注意.pageNo=0的时候就是查询全部
*
* @param className
* 要查询的对象名
* @param page
* 查询的也没信息
* @return json
* @throws ClassNotFoundException
* 传入的类名称不正确
* @throws InterruptedException
*/
@SuppressWarnings("unchecked")
@At("/superjson/list/*")
@Ok("json")
public Object list(String className, @Param("::page.") Page page,
HttpServletRequest request) throws ClassNotFoundException,
InterruptedException {
Condition condition = PageSqlUtil.getCnd(request);
Class c = Class.forName(BEANPATH + className);
List list = this.basicDao.searchByPage(c, condition, page.getPageNo(),
page.getPageSize());
int allCount = basicDao.searchCount(c);
page.setResult(list);
page.setTotalCount(allCount);
return page;
}
/**
* 通用的查找action 构建网址必须按照此格式/superjson/list/类名?page.pageSize=10...
* 注意.pageNo=0的时候就是查询全部
*
* @param className
* 要查询的对象名
* @param page
* 查询的也没信息
* @return json
* @throws ClassNotFoundException
* 传入的类名称不正确
* @throws InterruptedException
*/
@SuppressWarnings("unchecked")
@At("/superjson/findOne/*")
@Ok("json")
public Object findOne(String className, HttpServletRequest request) throws ClassNotFoundException,
InterruptedException {
StringBuilder sb = new StringBuilder() ;
Condition condition = PageSqlUtil.getCnd(request);
Class c = Class.forName(BEANPATH + className);
List list = this.basicDao.search(c, condition);
if(list.size()>0){
return list.get(0) ;
}
return "ERROR" ;
}
/**
* 保存
* 网址参数例子:/superjson/save/类名/对象名?对象名.参数=值
* @return 成功或者失败
* @throws ClassNotFoundException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws InstantiationException
*/
@SuppressWarnings("unchecked")
@At("/superjson/save/?/?/*")
@Ok("json")
public String save(String className, String objName,
HttpServletRequest request) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
InvocationTargetException {
Class c = Class.forName(BEANPATH + className);
Object obj = InjectByClass.getObject(c, objName, request);
this.basicDao.save(obj);
return "SUCCESS";
}
/**
* 保存
* 网址参数例子:/superjson/update/类名/对象名?对象名.参数=值
* @return 成功或者失败
* @throws ClassNotFoundException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws InstantiationException
*/
@SuppressWarnings("unchecked")
@At("/superjson/update/?/?/*")
@Ok("json")
public String update(String className, String objName,
HttpServletRequest request) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
InvocationTargetException {
Class c = Class.forName(BEANPATH + className);
Object obj = InjectByClass.getObject(c, objName, request);
this.basicDao.update(obj);
return "SUCCESS";
}
/**
* 删除
* 网址参数例子:/superjson/delete/类名?对象名.参数=值
* @return 成功或者失败
* @throws ClassNotFoundException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws InstantiationException
*/
@SuppressWarnings("unchecked")
@At("/superjson/delete/*")
@Ok("json")
public String delete(String className, HttpServletRequest request) throws ClassNotFoundException,
InterruptedException {
Condition condition = PageSqlUtil.getCnd(request);
Class c = Class.forName(BEANPATH + className);
List list = this.basicDao.search(c, condition);
basicDao.deleteAll(list) ;
return "SUCCESS";
}
}
他依赖一下两个工具类..和apache的jar包..beanUtil..当然不想用jar包就把beanUtil实现一下我是美意见的..我比较懒就用她的jar了
package com.enilu.shop.util;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.util.cri.SimpleCriteria;
public class PageSqlUtil {
/**
* Cnd_OR_KEY_OP Cnd_DESC_KEY Cnd_ASC_KEY
*
* @param request
* 网页发送过来的request
* @return Cnd
*/
@SuppressWarnings("unchecked")
public static Condition getCnd(HttpServletRequest request) {
Map<String, Object> map = request.getParameterMap();
Iterator<Entry<String, Object>> it = map.entrySet().iterator();
Entry<String, Object> entity = null;
String key = null;
QuerySub querySub = new QuerySub();
String[] strs = null;
while (it.hasNext()) {
entity = it.next();
key = entity.getKey();
strs = key.split("_");
if ("Cnd".equals(strs[0])) {
querySub.add(strs, entity.getValue());
}
}
return querySub.getCondition();
}
/**
* 查询串拼接 Cnd_OR_KEY_OP
*
* @author Ansj
*/
static class QuerySub {
StringBuilder sb = new StringBuilder(" where 1=1 ");
StringBuilder orderSb = new StringBuilder();
SimpleCriteria sc = new SimpleCriteria();
// Cnd_OR_KEY_OP
public void add(String[] strs, Object obj) {
MatchType mt = MatchType.valueOf(strs[strs.length-1]);
Object value = null ;
String op = null ;
switch (mt) {
case EQ:
op = "=" ;
break;
case N:
op = "<>" ;
break;
case IN:
op = "IN" ;
value = obj ;
break ;
case NIN:
op = "NOT IN" ;
value = obj ;
break ;
case GT:
op = ">" ;
break;
case LT:
op = "<" ;
break;
case GTE:
op = ">=" ;
break;
case LTE:
op = "<=" ;
break;
case LIKE:
op = "LIKE" ;
case NLIKE:
op = "NOT LIKEs" ;
case IS:
op = "IS" ;
break;
case ISN:
op = "IS NOT" ;
break;
case ASC:
sc.getOrderBy().asc(strs[2]) ;
break;
case DESC:
sc.getOrderBy().desc(strs[2]) ;
break;
}
if(value==null){
value = ((Object[])obj)[0] ;
if(StringUtils.isBlank(value.toString())){
return ;
}
}
if(op!=null){
if("AND".equalsIgnoreCase(strs[1])){
sc.where().and(Cnd.exp(strs[2], op, value)) ;
}else if("OR".equalsIgnoreCase(strs[1])){
sc.where().or(Cnd.exp(strs[2], op, value)) ;
}
}
}
public SimpleCriteria getCondition() {
return sc ;
}
}
/**
* 比较类型 N开头表示not 默认的比较或者like对于字符串都是忽略大小写的
*
*/
enum MatchType {
EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , ASC, DESC ;
}
}
package com.enilu.shop.util;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
public class InjectByClass {
@SuppressWarnings("unchecked")
public static Object getObject(Class c , String objName , HttpServletRequest request) throws InstantiationException, IllegalAccessException, InvocationTargetException{
Object obj = c.newInstance() ;
objName = objName+".";
Map<String, Object> map = request.getParameterMap();
Iterator<Entry<String, Object>> it = map.entrySet().iterator();
Entry<String, Object> entity = null;
String key = null;
String[] strs = null;
while (it.hasNext()) {
entity = it.next();
key = entity.getKey();
if(key.startsWith(objName)){
strs = key.split("\\.") ;
BeanUtils.setProperty(obj, strs[1], entity.getValue()) ;
}
}
return obj ;
}
}