zoukankan      html  css  js  c++  java
  • mybatis传入参数的几种方法

    传入复杂的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));
        }
    
    
  • 相关阅读:
    黄聪:获取当天最新信息的mysql语句php
    黄聪:开源PHP智能中文分词扩展:PHPCWS
    黄聪:Wordpress用get_current_screen函数来选择性加载插件中的JS和CSS
    黄聪: Javascript跨域访问解决方案
    黄聪:WordPress 角色和权限指南
    黄聪:PHP自动判断字符串是gb2312还是utf8编码
    黄聪:mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法
    黄聪:python中defaultdict标准字典的使用
    黄聪:IE6下css大bug:文字神秘消失,鼠标选择了才出现
    黄聪:C#模拟网站页面POST数据提交表单(一)WebClient (转)
  • 原文地址:https://www.cnblogs.com/shaoyu/p/11797222.html
Copyright © 2011-2022 走看看