建造者模式组装mybatis参数Example()

参考:github,   https://github.com/liuxiaochen0625/MyBatis-Spring-Boot-master.git

从controller组装tk.mybatis.mapper.entity.Example 对象,操作起来较为麻烦,不符合我们日常书写习惯,因而改造一下。

调用方法:

WhereBuilder builder = new WhereBuilder(UserInfo.class);
Example example = builder.and("username", OP.EQ, username).or("username", OP.EQ, "tom").and("password", OP.EQ, password).build();

链式操作,是不是很方便?~

具体WhereBuilder构造是参考android xutils的WhereBuilder和StringBuffer写的,如下:

public class WhereBuilder {
    private final Example example; //定义examle对象,用于返回

    public WhereBuilder(Class<?> tClass){this.example = new Example(tClass);}//构造方法中传入Class参数,实例化example

    public WhereBuilder and(String columnName,String op,Object value){  //and
        Example.Criteria criteria = example.createCriteria();
        assembleParams(criteria,columnName,op,value);
        return this;
    }

    public WhereBuilder or(String columnName,String op,Object value){ //or
        Example.Criteria criteria = example.createCriteria();
        assembleParams(criteria,columnName,op,value);
        example.or(criteria);   //or 需要example调用or(Example.Criteria criteria) 方法
        return this;
    }

    private void assembleParams(Example.Criteria criteria,String columnName,String op,Object value){  //组装参数
        switch (op) {
            case OP.LIKE:
                if (!CommonUtils.isEmpty(value)) {
                    criteria.andLike(columnName, "%" + value + "%");
                }
                break;
            case OP.LEFT_LIKE:
                if (!CommonUtils.isEmpty(value)) {
                    criteria.andLike(columnName, "%" + value);
                }
                break;
            case OP.LIKE_RIGHT:
                if (!CommonUtils.isEmpty(value)) {
                    criteria.andLike(columnName, value + "%");
                }
                break;
            case OP.EQ:
                if (!CommonUtils.isEmpty(value)) {
                    criteria.andEqualTo(columnName, value);
                }
                break;
            case OP.NE:
                if (!CommonUtils.isEmpty(value)) {
                    criteria.andNotEqualTo(columnName, value);
                }
                break;
            case OP.GT:
                if (!CommonUtils.isEmpty(value)) {
                    criteria.andGreaterThan(columnName, value);
                }
                break;
            case OP.NL:
                if (!CommonUtils.isEmpty(value)) {
                    criteria.andGreaterThanOrEqualTo(columnName, value);
                }
                break;
            case OP.LT:
                if (!CommonUtils.isEmpty(value)) {
                    criteria.andLessThan(columnName, value);
                }
                break;
            case OP.NG:
                if (!CommonUtils.isEmpty(value)) {
                    criteria.andLessThanOrEqualTo(columnName, value);
                }
                break;
            case OP.NULL:
                criteria.andIsNull(columnName);
                break;
            case OP.NOTNULL:
                criteria.andIsNotNull(columnName);
                break;
            case OP.IN:
                if (!CommonUtils.isEmpty(value)) {
                    if (value instanceof ArrayList)
                        criteria.andIn(columnName, (ArrayList) value);
                }
                break;
            case OP.BETWEEN:
                if (!CommonUtils.isEmpty(value)) {
                    if (value instanceof ArrayList) {
                        criteria.andBetween(columnName, ((ArrayList) value).get(0),
                                ((ArrayList) value).get(1));
                    }
                }
                break;
            case OP.NOTBETWEEN:
                if (!CommonUtils.isEmpty(value)) {
                    if (value instanceof ArrayList) {
                        criteria.andNotBetween(columnName, ((ArrayList) value).get(0),
                                ((ArrayList) value).get(1));
                    }
                }
                break;
            case OP.NOTIN:
                if (!CommonUtils.isEmpty(value)) {
                    if (value instanceof ArrayList)
                        criteria.andNotIn(columnName, (ArrayList) value);
                }
                break;
            default:
        }

    }

    public Example build(){  //返回example实例
        return example;
    }

}

 

posted @ 2016-10-13 13:48  anybus  阅读(5232)  评论(0编辑  收藏  举报