【MyBatis】實現in操作符在WHERE 子句中規定多個值


Mapper.xml中寫:

    <select id="selectIdsByDate"  resultType="java.lang.Long">
        select id from emp where cdate&lt;#{date,jdbcType=TIMESTAMP} limit 10000
    </select>
    
    <delete id="deleteByIds"> delete from emp where id in <foreach collection="list" open="(" close=")" separator="," item="id" index="i"> #{id} </foreach>
    </delete>

接口中這樣寫:

    List<Long> selectIdsByDate(String date);
    
    int deleteByIds(List<Long> ids);

代碼中則這樣用:

            EmpMapper mapper=session.getMapper(EmpMapper.class);
            
            int totalChanged=0;
            int index=0;
            while(true) {
                List<Long> ids=mapper.selectIdsByDate("2018-02-02"); if(ids.size()==0) {
                    break;
                }
                
                int changed=mapper.deleteByIds(ids);
                System.out.println("#"+index+" deleted="+changed);
                session.commit();
                totalChanged+=changed;
                
                index++;
            }

--END-- 2019年10月14日09:30:52


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM