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的键值。如:
补充:也可以获取到遍历集合的顺序,比如:
<select id="listFirstChargeReport" resultType="map" parameterType="map"> SELECT user_name,gmt_created, '1' AS 当天 <foreach collection="syncDates" item="syncDate" index="indexNum" open="," separator=","> IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '${syncDate}'),'-') AS 第${indexNum + 1}天 </foreach> FROM first_charge c WHERE gmt_created LIKE '${gmtCreated}%' </select>
java传入的参数如下:
Map<String, Object> tmpCondition = new HashMap<>(); tmpCondition.put("gmtCreated", DateFormatUtils.format(new Date(), "yyyy-MM-dd")); List<String> syncDates = getDates(DateFormatUtils.format(new Date(), "yyyy-MM-dd"), 15); tmpCondition.put("syncDates", syncDates); private List<String> getDates(String dateStr, int i) { List<String> result = new ArrayList<>(); try { Date parseDate = DateUtils.parseDate(dateStr, "yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); calendar.setTime(parseDate); while (i > 0) { calendar.add(Calendar.DAY_OF_MONTH, -1); i--; Date time = calendar.getTime(); String formatedTime = DateFormatUtils.format(time, "yyyy-MM-dd"); result.add(formatedTime); } } catch (ParseException e) { // ignore } return result; }
生成的SQL如下:
SELECT user_name,gmt_created, '1' AS 当天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-09-05'),'-') AS 第1天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-09-04'),'-') AS 第2天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-09-03'),'-') AS 第3天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-09-02'),'-') AS 第4天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-09-01'),'-') AS 第5天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-31'),'-') AS 第6天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-30'),'-') AS 第7天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-29'),'-') AS 第8天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-28'),'-') AS 第9天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-27'),'-') AS 第10天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-26'),'-') AS 第11天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-25'),'-') AS 第12天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-24'),'-') AS 第13天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-23'),'-') AS 第14天 , IFNULL((SELECT CASE WHEN bet_amount + 0 > 0 THEN '1' END FROM member m WHERE m.user_name = c.user_name AND sync_Date = '2019-08-22'),'-') AS 第15天 FROM first_charge c WHERE gmt_created LIKE '2019-09-06%' LIMIT ?
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】
分类:
Mybatis
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了