zoukankan      html  css  js  c++  java
  • 建造者模式组装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;
        }
    
    }
  • 相关阅读:
    spring 循环依赖问题
    spring data jpa 关键字 命名
    mongodb 添加字段并设置默认值
    java mongoTemplate的group统计
    java8 从对象集合中取出某个字段的集合
    springboot12-zuul
    springboot11-01-security入门
    springboot项目怎么部署到外部tomcat
    springboot10-springcloud-eureka 服务注册与发现,负载均衡客户端(ribbon,feign)调用
    UML
  • 原文地址:https://www.cnblogs.com/anybus/p/5956223.html
Copyright © 2011-2022 走看看