您的位置:首页 > 数据库

mybatis动态sql

2017-04-27 22:48 211 查看
在mybatis中,它提供了一些动态sql标签,可以让程序员更快的进行mybatis的开发,这些动态sql可以通过sql的可重用性。

提供了几种标签;

if标签/where标签:

<select id="findUserList" parameterType="com.sgn.mybatis.po.UserQueryVo" resultType="user">

select * from user

<!-- <where>标签:默认去掉后面第一and,如果没有参数则把自己干掉 -->

<where>

<!-- if 标签:可以对输入的参数进行判断 test:指定判断表达式-->

<if test="user != null">

<if test="user.username != null and user.username != ''">

and username like '%${user.username}%'

</if>

<if test="user.sex !=null and user.sex!=''">

and sex = #{user.sex}

</if>

</if>

</where>

</select>


sql片段的使用:
<sql id="query">

<!-- <where>标签:默认去掉后面第一and,如果没有参数则把自己干掉 -->

<where>

<!-- if 标签:可以对输入的参数进行判断 test:指定判断表达式-->

<if test="user != null">

<if test="user.username != null and user.username != ''">

and username like '%${user.username}%'

</if>

<if test="user.sex !=null and user.sex!=''">

and sex = #{user.sex}

</if>

</if>

</where>

</sql>

<select id="findUserList" parameterType="com.sgn.mybatis.po.UserQueryVo" resultType="user">

select * from user

<include refid="query"></include>

</select>


and id in

#{id}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: