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' )