概述
1. 使用IN实现多条件查询,传入的参数是String数组,如 issuePriority = ["ABC","BCD","EFG"] 2. 在MyBatis xml文件中,使用foreach,其中,需要使用$ 而不是#, {item}需要用单引号括起来,如'${item}' <if test="issuePriority != null and issuePriority.length > 0 "> and ISSUE_PRIORITY in <foreach collection="issuePriority" item="item" index="index" open="(" close=")" separator=","> '${item}' </foreach> </if> <!-- 根據查詢條件,查詢需求信息 --> <select id="getDailyWorkingIssueListWithArrayParameters" resultMap="DailyWorkingIssue"> SELECT * FROM MES_KPI_ISSUE_T WHERE 1=1 <if test="issueId != null and issueId != ''"> and ISSUE_ID = #{issueId} </if> <if test="issueSubject != null and issueSubject != ''"> and ISSUE_SUBJECT = #{issueSubject} </if> <if test="issuePriority != null and issuePriority.length > 0 "> and ISSUE_PRIORITY in <foreach collection="issuePriority" item="item" index="index" open="(" close=")" separator=","> '${item}' </foreach> </if> <if test="beginDate != null "> AND date_format(BEGIN_DATE,'%Y-%m-%d') >= date_format(#{beginDate},'%Y-%m-%d') </if> <if test="endDate != null "> AND date_format(BEGIN_DATE,'%Y-%m-%d') <= date_format(#{endDate},'%Y-%m-%d') </if> </select>
最后生成的SQL语句
SELECT * FROM MES_KPI_ISSUE_T WHERE 1=1 and BUSINESS_LEVEL in ( 'L6' ) and BUSINESS_CUSTOMER in ( 'AMAZON' , 'BAIDU' )
SELECT * FROM MES_KPI_ISSUE_T WHERE 1=1 and ISSUE_PRIORITY in ( 'A' )
最后
以上就是优雅发夹为你收集整理的MyBatis 利用foreach处理数组参数传入的全部内容,希望文章能够帮你解决MyBatis 利用foreach处理数组参数传入所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复