如何创建w动态WHERE子句

如何创建w动态WHERE子句

本文介绍了MyBatis - 如何创建w动态WHERE子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该服务获取一个包含三个值列表的未知对象[column,operator,value]例如,
EMAIL - like - TEST

The service gets an unknown object containing a list of three values ​​[column, operator, value] For example,EMAIL - like - "TEST"

根据结果列表构建我有的WHERE子句,但我也可以构建如下的条件(例如)

Based on the resulting list to build the WHERE clause I have but I would also be able to build such a condition as follows (for example)

WHERE(电子邮件比如'test'和user_id<> 5)OR(trans_id< 100 AND session_id> 500)

WHERE (email like 'test' AND user_id <> 5) OR (trans_id <100 AND session_id> 500)

有没有人可以帮我怎么做?

Does anyone can help me how to do it?

推荐答案

我自己长期缺席后一直在重新发现MyBatis(我一度熟悉iBatis)。 Rolf的例子看起来可能是.Net实现,我可能错了,但我不认为Java符号现在看起来像那样。 Rolf关于文字字符串的提示非常有用。

I have been rediscovering MyBatis after a long absence myself (I was familiar with iBatis at one time). Rolf's example looks like it might be the .Net implementation, I may be wrong but I don't think the Java notation looks like that now. Rolf's tip about the literal strings is very useful.

我创建了一个小类来保存你的列,运算符和值,并将它们传递给MyBatis来进行处理。列和运算符是字符串文字,但我将值保留为sql参数(我想MyBatis可以进行任何必要的类型转换)。

I've created a little class to hold your columns, operators and value and pass these into MyBatis to do the processing. The columns and operators are string literals but I have left the values as sql parameters (I imagine MyBatis would be able to do any necessary type conversion).

public class TestAnswer {
    public static void main(String[] args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            SqlSessionFactory sqlFactory = (SqlSessionFactory) ctx.getBean("sqlSessionFactory");
            MappedStatement statement = sqlFactory.getConfiguration().getMappedStatement("testAnswer");

            ArrayList<Clause> params1 = new ArrayList<Clause>();
            params1.add(new Clause("email","like","test"));
            params1.add(new Clause("user","<>",5));

            ArrayList<Clause> params2 = new ArrayList<Clause>();
            params2.add(new Clause("trans_id","<",100));
            params2.add(new Clause("session_id",">",500));

            HashMap params = new HashMap();
            params.put("params1", params1);
            params.put("params2", params2);

            BoundSql boundSql = statement.getBoundSql(params);
            System.out.println(boundSql.getSql());
    }

    static class Clause{
        private String column;
        private String operator;
        private Object value;

        public Clause(String column, String operator, Object value){
            this.column = column;
            this.operator = operator;
            this.value = value;
        }

        public void setColumn(String column) {this.column = column;}
        public void setOperator(String operator) {this.operator = operator;}
        public void setValue(Object value) {this.value = value;}
        public String getColumn() {return column;}
        public String getOperator() {return operator;}
        public Object getValue() {return value;}
    }
}

尽可能看,我使用Spring但我希望类似的东西可能在Spring环境之外工作。

As you can see, I use Spring but I expect something similar would probably work outside of the Spring environment.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    <mapper namespace="com.stackoverflow.TestMapper">

    <select id="testAnswer" parameterType="map" resultType="hashmap">
      select *
    FROM somewhere
        <where>
            <foreach item="clause" collection="params1" separator=" AND " open="(" close=")">
                ${clause.column} ${clause.operator} #{clause.value}
            </foreach>
            OR
            <foreach item="clause" collection="params2" separator=" AND " open="(" close=")">
                ${clause.column} ${clause.operator} #{clause.value}
            </foreach>
        </where>
    </select>

</mapper>

这篇关于MyBatis - 如何创建w动态WHERE子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:51