Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用enum解决排序字段问题 #28

Closed
x03570227 opened this issue Dec 15, 2014 · 1 comment
Closed

用enum解决排序字段问题 #28

x03570227 opened this issue Dec 15, 2014 · 1 comment

Comments

@x03570227
Copy link
Owner

Condition 类

    public enum Sort{

        ID("1", "js.id"), 
        GMT_BASETIME("2", "js.gmt_basetime"), 
        GMT_TRIGGER("3", "js.gmt_trigger") ;

        private String code;
        private String column;

        private Sort(String code, String column){
            this.code = code;
            this.column = column;
        }

        @Override
        public String toString(){
            return this.column;
        }
    }

    public String getSort(String code){
        if(Strings.isNullOrEmpty(code)){
            return Sort.ID.toString();
        }

        for(Sort sort: Sort.values()){
            if(code.equalsIgnoreCase(sort.code)){
                return sort.toString();
            }
        }

        return Sort.ID.toString();
    }

xml 配置

        <if test="page.sortColumn != null and page.sortColumn != '' ">
            order by ${page.sortColumn} ${page.dir}
        </if>
        limit #{page.start}, #{page.limit}

Class Pager 修改

    private String sortColumn;
    public String getSortColumn() {
        return sortColumn;
    }
    public void setSortColumn(String sortColumn) {
        this.sortColumn = sortColumn;
    }

    private enum DIR{
        DESC("desc"), ASC("asc");

        private String dir;

        private DIR(String dir){
            this.dir = dir;
        }
        @Override
        public String toString(){
            return this.dir;
        }
    }

    public String getDir() {

        if(Strings.isNullOrEmpty(dir)){
            return DIR.DESC.toString();
        }

        for(DIR d: DIR.values()){
            if(dir.equalsIgnoreCase(d.dir)){
                return dir;
            }
        }

        return DIR.DESC.toString();
    }

分页调用

page.setSortColumn(cond.getSort(page.getSort()));
@x03570227 x03570227 self-assigned this Dec 15, 2014
@x03570227 x03570227 added this to the v1.2.x milestone Dec 15, 2014
@x03570227
Copy link
Owner Author

将此分类方面整理到 gist

@x03570227 x03570227 modified the milestones: v1.2.x, v1.3.x Dec 16, 2014
@x03570227 x03570227 modified the milestone: v1.3.x Jan 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant