mybatis传入map参数,map中包含list(输入参数)

1.xml中配置:

<!-- 根据条件查询满足条件的ID集合开始 -->
    <select id="getQuestionsIdsForExamPaper" resultType="java.lang.String"
        parameterType="hashmap">
        select
        questionId from questions
        <where>
            <include refid="query_questionIds_where"></include>
        </where>
    </select>

    <!-- 查询试题ID的条件 -->
    <sql id="query_questionIds_where">
        <if test="type!=null">
            and type=#{type}
        </if>
        <if test="level!=null">
            and level=#{level}
        </if>
        <!-- 知识点 -->
        <if test="konwledges!=null">
            and knowledgeType in
            <foreach collection="konwledges" item="knowledge" separator=","
                open="(" close=")">
                #{knowledge}
            </foreach>
        </if>
        <if test="num!=null">
            ORDER BY RAND() LIMIT #{num}
        </if>
    </sql>

2.Java测试:

    // 测试查询ID集合
    @Test
    public void test3() throws SQLException {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("type", "单选题");
        condition.put("level", 1);
        condition.put("num", 3);
        List<String> konwledges = new ArrayList<String>();
        konwledges.add("安全生产管理知识");
        konwledges.add("电力安全规程制度");
        condition.put("num", 3);
        condition.put("konwledges", konwledges);
        List<String> IDs = questionsCustomMapper.getQuestionsIdsForExamPaper(condition);
        System.out.println(IDs.size());
    }

结果:

总结:

  map中的list同普通的一样,只是在遍历的时候collection要写出map中的List的键值。如:

时间: 2024-08-04 20:43:04

mybatis传入map参数,map中包含list(输入参数)的相关文章

将传入结构体 pMtInfo 中包含的数据内容转换成 JSON 字符串返回

upu_struct.h封装了有关  pMtInfo结构体的内容,用到的部分如下图所示: 利用jansson库实现将传入结构体 pMtInfo 中包含的数据内容转换成 JSON 字符串返回 代码如下: #include <stdio.h> #include <string.h> #include "jansson.h" #include "upu_struct.h" #include "upu_proto_parse.h"

解决Volley中的JsonObjectRequest jsonRequest参数无法被服务端读取的问题

服务端:SpringBoot 追溯到父类方法,发现Volley只是将 jsonRequest.toString().getBytes()作为request body发送到服务端,导致服务端无法识别 import com.android.volley.AuthFailureError; import com.android.volley.Response; import com.android.volley.VolleyLog; import com.android.volley.toolbox.

CMD接受输入参数(定时关机小例子)

为了方便操作,我大程序员经常会将一些重复的工作写成一个批处理. 但是,在批处理中如何接受输入参数呢?且看如下例子: @echo off set /p time=请输入关机时间: shutdown -s -f -t %time% 这是一个定时关机的批处理,其中接受一个时间参数,用户可以输入在多久之后关机. 下面逐行来看: @echo off ,DOS会依次执行文件中的命令,并将执行的命令自动输出在DOS中,如果我们不想将执行的命令显示出来,就可以使用echo off来关闭自动输出. 但是,echo

Ibatis中sqlmap参数map中还需要套list的情况如何写?

原始需求: 有若干个参数,需要作为ibatis拼装sql的参数传入,但是有个参数的值比较特殊,是若干种枚举值.具体到这个case,就是有有限个namespace.我每次需要通过传入多个namespace来查询DB记录. 准备需要传入sqlmap的参数的示例代码如下: Java代码   Map<String,Object> ibatisParam = new HashMap<String, Object>( ); ibatisParam.put( "keyA",&

Map.containsKey方法——判断Map集合对象中是否包含指定的键名

该方法判断Map集合对象中是否包含指定的键名.如果Map集合中包含指定的键名,则返回true,否则返回false. public static void main(String[] args) { Map map = new HashMap(); //定义Map对象 map.put("apple", "新鲜的苹果"); //向集合中添加对象 map.put("computer", "配置优良的计算机"); map.put(&q

MyBatis传入多个参数的问题

一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean"> select t.* from tableName t where t.id= #{id} </select> 其中方法名和ID一致,

mybatis传入多个参数

在开头: 需要查阅本文的基本都是需要传入多个参数的,这里记住一句话:无论你传的参数是什么样的,最后mybtis都会将你传入的转换为map的,那么既然这样,当我们要传入多个参数时,何不直接给与map类型即可,然后mapper.xml通过#{map.key}来获取值即可,这个特别适合动态搜索,或者多个参数的查询,并且可以在mapper的xml语句中通过if判断来实现若为空,则不添加查询条件, <if test="userId != null"> #{userId,jdbcTyp

mybatis传入参数类型parameterType和输出结果类型resultType详解

前言 Mybatis的Mapper文件中的select.insert.update.delete元素中都有一个parameterType和resultType属性,parameterType属性用于对应的mapper接口方法接受的参数类型,resultType用于指定sql输出的结果类型. resultType:指定sql输出结果类型,总共就两种: 1. 基本数据类型. 2. pojo类类型.mybatis将sql查询结果的一行记录数据映射为resultType指定类型的对象.如果有多条数据,则

【MyBatis】解析MyBatis传入参数的问题

一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean"> select t.* from tableName t where t.id= #{id} </select> 其中方法名和ID一致,