给JFinal的Model添加search,简化简单多条件查询(完善了MAP的处理)

看了http://my.oschina.net/myaniu/blog/137065关于jFinal的Model的优化,进一步做了一些处理
 public M searchFirst(String key, Object value) {
        List<M> mList = search(key, value, "");
        return mList != null && mList.size() > 0 ? mList.get(0) : null;
    }

    public List<M> search(String key, Object value) {
        return search(key, value, "");
    }

    public List<M> search(String key, Object value, String orderBy) {

        String sql = "select * from " + TableInfoMapping.me().getTableInfo(getClass()).getTableName() + " where " + key + "=? " + orderBy;
        return find(sql, value);
    }

    public M searchFirst(Map<Object, Object> maps) {
        List<M> mList = search(maps, "");
        return mList != null && mList.size() > 0 ? mList.get(0) : null;
    }

    public List<M> search(Map<Object, Object> maps) {
        return search(maps, "");
    }

    public List<M> search(Map<Object, Object> maps, String orderBy) {

        StringBuilder sb = new StringBuilder();
        sb.append("select * from ").append(TableInfoMapping.me().getTableInfo(getClass()).getTableName()).append(" where 1=1 ");
        List<Object> values = setMap(maps, sb);
        sb.append(" ").append(orderBy);
        return find(sb.toString(), values.toArray());
    }
    private List<Object> setMap(Map<Object, Object> maps, StringBuilder sb) {
        List<Object> values = new ArrayList<Object>();
        for (Entry<Object, Object> entry : maps.entrySet()) {
            if (entry.getValue() != null) {
                if (entry.getKey() instanceof String) {
                    String key = entry.getKey().toString();
                    if (TableInfoMapping.me().getTableInfo(getClass()).hasColumnLabel(key)) {
                        sb.append(" and ").append(key).append("=?");// 这里没有处理模糊查询
                        Object val[] = (Object[]) entry.getValue();// request.getParameterMap中map的值是数组
                        values.add(val[0]);
                    }
                } else {
                    // 查询条件的key应该都是String,如果不是请自己处理
                }
            }
        }
        return values;
    }

    public List<M> searchByCache(String cacheName, Object key, Map<Object, Object> maps) {
        return this.searchByCache(cacheName, key, maps, "");
    }

    public List<M> searchByCache(String cacheName, Object key, Map<Object, Object> maps, String orderBy) {

        ICache cache = DbKit.getCache();
        List<M> result = cache.get(cacheName, key);
        if (result == null) {
            result = search(maps);
            cache.put(cacheName, key, result);
        }
        return result;
    }

    public Page<M> searchPaginate(int pageNumber, int pageSize, Map<Object, Object> maps) {
        return this.searchPaginate(pageNumber, pageSize, maps, "");
    }

    public Page<M> searchPaginate(int pageNumber, int pageSize, Map<Object, Object> maps, String orderBy) {

        StringBuilder sb = new StringBuilder();
        sb.append("from ").append(TableInfoMapping.me().getTableInfo(getClass()).getTableName()).append(" where 1=1");
        List<Object> values = setMap(maps, sb);
        return paginate(pageNumber, pageSize, "select *", sb.toString(), values.toArray());
    }

    public Page<M> searchPaginateByCache(String cacheName, Object key, int pageNumber, int pageSize, Map<Object, Object> maps) {
        return this.searchPaginateByCache(cacheName, key, pageNumber, pageSize, maps, "");
    }

    public Page<M> searchPaginateByCache(String cacheName, Object key, int pageNumber, int pageSize, Map<Object, Object> maps, String orderBy) {

        ICache cache = DbKit.getCache();
        Page<M> result = cache.get(cacheName, key);
        if (result == null) {
            result = searchPaginate(pageNumber, pageSize, maps, orderBy);
            cache.put(cacheName, key, result);
        }
        return result;
    }
调用示例:
   public void index() {

        setAttr("blogPage", Blog.dao.searchPaginate(getParaToInt(0, 1), 10, getRequest().getParameterMap()));

        render("blog.html");

    }

你可能感兴趣的:(Model,search,jFinal)