一、前言
通过mybatis的foreach实现查询SQL语句包含关系in的逻辑,下面分别在mapper映射配置、dao代码及服务层实现类示例说明
二、代码示例
1.mysql-mapper映射xml中示例如下
<select id="getUserList" resultType="com.xwood.sso.web.responseResult.UsersResult" parameterType="java.util.Map" >@b@ select * from tb_user t @b@ <where>@b@ <if test="null != userIdList and userIdList.size > 0">@b@ t.user_id in @b@ <foreach collection="userIdList" open="(" close=")" separator="," item="item">@b@ #{item,jdbcType=VARCHAR}@b@ </foreach> @b@ </if> @b@ </where> @b@ </select>
2.dao接口如下
public interface UserDAO extends BaseMapper { @b@ public List<UsersResult> getUserList(Map<String, Object> map);@b@}
3.service层代码
public List<UsersResult> getUsersList() throws Exception{@b@ @b@ Map<String, Object> map=new HashMap<String, Object>(); @b@ @b@ List<String> userIdList=new ArrayList<String>();@b@ userIdList.add("1");@b@ userIdList.add("2");@b@ map.put("userIdList", userIdList); @b@ @b@ return userDAO.getUserList(map); @b@ @b@}