关注互联网应用及运维技术的个人博客

MyBatis常见的动态语句Sql使用技巧

if && where

<!--动态语句sql的配置池-->

<!--if语句的设置-->
<select id="selectSname" parameterType="student" resultType="student">
select stuNo,stuName from student2
<where>
<if test="stuNo != null">
and stuNo = #{stuNo}
</if>
<if test="stuName !=null">
and stuName = #{stuName}
</if>
</where>
</select>

foreach

集合


<!--集合-->
<select id="selectwithlist" parameterType="list" resultType="student">

select * from student2
<where>

<foreach collection="list" open="and stuno in (" close=")" item="stuNo" separator=",">
#{stuNo}
</foreach>
</where>


</select>

数组

<!--同时查询1,2,3-->
<select id="selectforeach" parameterType="int[]" resultType="student">

select stuNo,stuName from student2
<where>

<foreach collection="array" open="and stuno in (" close=")" item="stuNo" separator=",">
#{stuNo}
</foreach>
</where>

</select>

对象集合

<!--对象集合-->
<select id="selectwithObject" parameterType="Object[]" resultType="student">
select * from student2
<where>
<foreach collection="array" open="and stuno in (" close=")" item="student" separator=",">

#{student.stuNo}
</foreach>
</where>
</select>
赞(0)
未经允许不得转载:飞天狒狒 » MyBatis常见的动态语句Sql使用技巧

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址