mybatis第三天学习

今天学习内容主要分成两部分

  第一部分还是动态sql,新学了几个新标签if where 还有集合用的forch标签

主要的作用是多条件的查询 用where标签进行包裹 ,可以替代where 1= 1

if标签用作判断 可以多条件进行查询

比如 <if test= " #{username} =! null">

 and username = ${username}

</if>

 

select * from user where 1=1  and id in();

 

 

注意要用and进行连接

!--根据条件查询 -->
<select id="findUserByCondition" resultMap="userMap" parameterType="com.itheima.domain.User">
select * from user
<where>
<if test="username !=null">
and username = #{username}
</if>
<if test="sex != null">
and sex= #{sex}
</if>
</where>
</select>

<!--通过多个id 来查询多条内容
输入的内容封装到parameterType 返回的内容封装到resultMap
-->
<select id="findUserByIds" resultMap="userMap" parameterType="com.itheima.domain.queryVo">
select * from user
<where>
<if test="ids !=null and ids.size()> 0">
<foreach collection="ids" open="and id in(" close=")" separator="," item="id">
#{id}
</foreach>
</if>
</where>
</select>

 

posted @ 2021-01-31 00:13  煎饼侠是我  阅读(47)  评论(0)    收藏  举报