All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.yooryan.advancequery.AdvanceQueryModel Maven / Gradle / Ivy

There is a newer version: 1.2.4
Show newest version
package com.github.yooryan.advancequery;

import com.github.yooryan.advancequery.toolkit.Assert;
import lombok.Getter;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.session.Configuration;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @author linyunrui
 */
public class AdvanceQueryModel {
        /**
         * 参数名称
         */
        private List paramNames;
        /**
         * 参数值
         */
        private List params;
        /**
         * 消费次数
         */
        private final int consumerCount;
        /**
         * 分页方言 sql
         */
        @Getter
        private final String dialectSql;
        /**
         * 提供 Configuration
         */
        private Configuration configuration;
        /**
         * 用 List 消费参数值
         */
        private Consumer> paramNameConsumer = i -> {
        };
        /**
         * 用 Map 消费参数值
         */
        private Consumer> paramMapConsumer = i -> {
        };


        public AdvanceQueryModel(String dialectSql, List paramNames, List params) {
            this.dialectSql = dialectSql;
            this.paramNames = paramNames;
            this.params = params;
            this.consumerCount = paramNames.size();
        }

        /**
         *
         * @return this
         */
        public AdvanceQueryModel setConsumer() {
            this.paramNameConsumer = j -> {
                for (int i = 0; i < consumerCount; i++) {
                    j.add(new ParameterMapping.Builder(configuration,paramNames.get(i), Object.class).build());
                }
            };
            this.setParamMapConsumer();
            return this;
        }

        /**
         *
         * @return this
         */
        public AdvanceQueryModel setConsumerChain() {
            return setConsumer();
        }

        /**
         * 把内部所有需要消费的都消费掉
         *
         * @param parameterMappings    ParameterMapping 集合
         * @param configuration        Configuration
         * @param additionalParameters additionalParameters map
         */
        public void consumers(List parameterMappings, Configuration configuration,
                              Map additionalParameters) {
            Assert.notNull(configuration, "configuration must notNull !");
            Assert.notNull(parameterMappings, "parameterMappings must notNull !");
            Assert.notNull(additionalParameters, "additionalParameters must notNull !");
            this.configuration = configuration;
            this.paramNameConsumer.accept(parameterMappings);
            this.paramMapConsumer.accept(additionalParameters);
        }

        /**
         * 设置消费 Map 的方式
         */
        private void setParamMapConsumer() {
            this.paramMapConsumer = j -> {
                for (int i = 0; i < consumerCount; i++) {
                    j.put(paramNames.get(i),params.get(i));
                }
            };
        }
    }