1. 程式人生 > 實用技巧 >題解 SP16254 【RMID2 - Running Median Again】

題解 SP16254 【RMID2 - Running Median Again】

foreach:

@Data
public class Blog {
    private String id;
    private String title;
    private String author;
    private Date createTime;
    private int views;
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"
> <mapper namespace="com.nbg.dao.BlogMapper"> <select id="getBlogByForeach" parameterType="map" resultType="blog"> select * from blog <where> <foreach collection="vs" open="views in (" close=")" separator="," item="v"> #{v}
</foreach> </where> </select> </mapper>
    @Test
    public void getBlogByForeach() {
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(
1); list.add(2); list.add(3); map.put("vs",list); for (Blog blogByForeach : mapper.getBlogByForeach(map)) { System.out.println(blogByForeach); } session.close(); }