mybatis各种类型参数以及动态参数

	a.使用if
	<select id="getStudentByName" resultType="com.test.pojo.Student">
        SELECT * FROM `student`
        <where>
            <if test="name!=null and name!=''">
                `stu_name` like concat('%',#{name},'%')
            </if>
        </where>
    </select>
    <select id="getStudentByName" resultType="com.test.pojo.Student">
        SELECT * FROM `student`
        <where>
            <if test="true">
                1=1
            </if>
            <if test="number1!=null and number1!=''">
                and `stu_name` like concat('%',#{number1},'%')
            </if>
            <if test="number2!=-1">
                 and gradeid = #{number2}
            </if>
        </where>
    </select>
    <!--多个if无法判断and什么时候出现的时候可以在前面添加一个1=1-->
    <!--这里<where>表示可有可无,如果里面有很多if,但凡有一个if的test满足判断条件,where就会出现-->
	<!--多个if的话从上到下依次执行-->
	b.使用choose即switch
	<select id="getStudentByGrade" resultType="com.test.pojo.Student">
        select * from student
        <where>
            <if test="true">
                1=1
            </if>
            <choose>
                <when test="gradeId>=1">
                    and gradeId = 1
                </when>
                <when test="gradeId==2">
                    and gradeId = #{aaa}
                </when>
                <otherwise>
                    and gradeId = 3
                </otherwise>
            </choose>
        </where>
    </select>
    <!--最前面加一个1=1这样可以防止后面是否出现and导致的语法错误-->
    <!--choose虽然类似switch但也可以看作多if判断,可以处理区间,从上往下依次判断条件,如果满足when就处理哪个wen里面的数据,并且不会再判断其他的when,如果when都不满足,那么执行otherwise即多if里面的else-->
	c.<set>
<update id="updateAuthorIfNecessary">
  update Author
    <set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio},</if>
    </set>
  where id=#{id}
  <!--这个例子中,set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号-->
	
2.使用forEach	
	a.数组使用使用forEach
     List<Student> getStudentAndTrim(@Param("array") Integer[] array,@Param("array2") Integer[] array2);
	</select>
    <select id="getStudentAndTrim" resultType="com.test.pojo.Student">
        select * from student
        <where>
    <foreach collection="dao接口参数名" item="array"  open="id in(" close=")" separator=",">
            #{array}
        </foreach>
        </where>
    </select>
    <!--如果传入了多个数组,那么需要在dao接口中写入@Param,然后在xml文件中item的值需要与要使用的@Param值相对应-->
    b.使用集合forEach
    List<Student> getStudentAndTrim(@Param("list") List<Integer> list,@Param("list22") List<Integer> list2);
    <select id="getStudentAndTrim" resultType="com.test.pojo.Student">
        select * from student
        <where>
    <foreach collection="list" item="list"  open="id in(" close=")" separator=",">
            #{list}
        </foreach>
        </where>
    </select>
<!--coolection表示数据类型,值是固定写法,如果是数组那么就是array,如果是集合那么就是list,item表示用户自定义变量,可通过item调用每次循环的值,spearator表示分隔符,就是表示两个数据之间以什么来分割,open表示循环开始前什么在最前面,close表示循环结束后什么在最后。如果传入了多个集合,那么需要在dao接口中写入@Param,然后在xml文件中item的值需要与要使用的@Param值相对应-->
	c.使用Map集合forEach
	Map<String,Object> map = new HashMap<>();
    List<String> list = Arrays.asList("张三","张琪","李四");
    map.put("name",list);
    
    List<Student> getStudentByMapFor(Map<String,Object> map);
    
     <select id="getStudentByMapFor" resultType="com.test.pojo.Student">
        select * from `student` where stu_name in
        <foreach collection="name" item="stu" open="(" separator="," close=")">
            #{stu}
        </foreach>
    </select>
    <!--思路:声明一个map集合,然后把<key,value>中value的值改为Object类型,然后把数组放进value当中,在xml中collection为自定义的的key的值根据key获得value的集合,然后输出item-->
    	(1) 传多个map处理
    	List<Student> getStudentByMapFor(@Param("map1") Map<String,Object> map1,@Param("map2") Map<String,Object> map2);
    	
    	<select id="getStudentByMapFor" resultType="com.test.pojo.Student">
        select * from `student` where stu_name in
        <foreach collection="map1.name" item="stu" open="(" separator="," close=")">
            #{stu}
        </foreach>
    </select>

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值