传入复杂的map数据

先来看一下需要传入的数据结构

/**
 *实际数据结构复杂很多,不过通过这个可以说明问题
 */
{
"keywords":"string",//String类型
"clazzs":[{
        clazz_id:1, //int
        clazz_user:"软件" //String
    },{
        clazz_id:2, //int
        clazz_user:"网络" //String
    },{
        clazz_id:3, //int
        clazz_user:"移动" //String
    }]  //list类型
}   //map结构

编写dao层接口

    /**
     * 通过班级名称模糊查询班级
     * @param cname
     * @return
     */
    ArrayList<Clazz> getClazzListByName(String cname);

    /**
     * 通过学院,班级,角色列表分页获取用户信息获取接口
     */
    ArrayList<User> getFilterUser(Map<String,Object> parames);

mapper.xml

        <!--根据班级名称模糊查询班级-->
    <select id="getClazzListByName" parameterType="string" resultType="clazz">
        SELECT * FROM t_clazz WHERE clazz_name LIKE '%${value}%'<!--这里基本数据类型${}里面必须为value-->
    </select>


<select id="getFilterUser"  resultMap="userClazzFacultyRoleMap">
        SELECT user_id,user_name,user_sex,user_phone,faculty_id,clazz_id,role_id,state_id FROM `t_user`
        <where>
                        <!--这里test="clazzs != null and clazzs.size() 相当于test="map.get("clazzs ") != null and map.get("clazzs ").size()-->
            <if test="clazzs != null and clazzs.size() > 0">
                and clazz_id in
                                <!--注意collection="clazzs"属性-->
                <foreach collection="clazzs" open="(" close=")" separator="," index="index" item="citem">
                    #{citem.clazz_id}<!--这里从每一项(citem)中通过 "." 运算付拿到数据-->
                </foreach>
            </if>
            <if test="keywords != null">
                and user_name like '%${keywords}%' <!--这里${keywords}相当于map.get("keywords")-->
            </if>
            <if test="sex != null and sex > -1">
                and user_sex = #{sex}
            </if>
            <if test="state_id != null and state_id > -1">
                and state_id = #{state_id}
            </if>

            <if test="fids != null and fids.size() > 0">
                and faculty_id in
                <foreach collection="fids" open="(" close=")" separator="," index="index" item="fid">
                    #{fid}
                </foreach>
            </if>
            <if test="rids != null and rids.size() > 0">
                and role_id in
                <foreach collection="rids" open="(" close=")" separator="," index="index" item="rid">
                    #{rid}
                </foreach>
            </if>
        </where>
        <choose>
            <when test="start != null  and length != null and start >= 0 and length > 0">
                limit #{start},#{length}
            </when>
            <otherwise>
                limit 0,0
            </otherwise>
        </choose>
    </select>

编写测试类

@Test
    public void testScreenUser(){
        Map<String,Object> map = new HashMap<>();
        map.put("fids",Arrays.asList(1));
        String keywords="";
        List<Clazz> clazzs =null;
        if(keywords != null && !keywords.equals("")){
            clazzs =userDao.getClazzListByName(keywords);
        }
        if(clazzs !=null && clazzs.size()>0){
            map.put("clazzs",clazzs);
        }else {
            map.put("keywords",keywords);
        }
        map.put("start",0);
        map.put("length",50);
        System.out.println(userDao.getFilterUser(map));
    }
01-06 03:49