mybatis使用foreach循环语句查询数据
mybatis使用foreach循环语句查询数据
·
if (session.getAttribute("user") != null) {
Object user = session.getAttribute("user");
ObjectMapper objectMapper = new ObjectMapper();
Users users = objectMapper.convertValue(user, Users.class);
List<Map<String, String>> deptGroupDetailed = users.getUser().getDeptGroupDetailed();
List<String> codeList = deptGroupDetailed.stream().map(i -> i.get("DEPT_CODE")).distinct().collect(Collectors.toList());
blPageBean.setUserCodeList(codeList);
}
首先取出来数据,然后存到实体类中。
private List<String> userCodeList;
实体类格式
<select id="getAllDepts" resultType="java.util.Map">
SELECT DEPT_CODE as "deptCode" ,DEPT_NAME as "deptName"
FROM TB_FEEMON_DIC_DEPARTMENT
<if test="userCodeList != null and userCodeList.size() > 0">
WHERE TB_FEEMON_DIC_DEPARTMENT.DEPT_CODE in
<foreach collection="userCodeList" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</select>
然后在xml中写sql语句即可。 userCodeList 对应为实体类字段名称。
更多推荐
已为社区贡献3条内容
所有评论(0)