十二、mybaitis学习——动态sql(使用中级)
一、动态sql标签
MyBatis 的强大特性之一便是它的动态 SQL。可以根据不同条件拼接 SQL 语句。
动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似。主要由以下几种元素。
- if 判断
- where 一般用于where条件配合if使用
- choose (when, otherwise) :分支选择
- foreach 用于集合循环便利
- trim 一般用于insert语句
- set 一般用于update语句
- bind 一般用于模糊查询
- include 引用sql标签
- sql标签 为include调用提供引用源
二、 where 、if
where if标签的使用 if标签体中每个and必须写,
1.where标签会自动根据sql语法去除首个and
2.if标签判断
test属性:判断是否添加该条件下的动态sql
<!-- where if标签的使用 每个and必须写,
1.where标签会自动根据sql语法去除首个and
2.if标签判断
test属性:判断是否添加该条件下的动态sql
-->
<select id="getUserUnderCondition" resultType="user"
parameterType="user">
select * from user
<where>
<if test="id!=null ">
and id >= #{id}
</if>
<if test="username!=null and username != '' ">
<bind name="_username" value="'%' +username+'%' " />
and username like #{_username}
</if>
<if test="sex!=null and sex != '' ">
and sex != #{sex}
</if>
<if test="address != null and address != '' ">
and address like #{address}
</if>
<if test="birthday!=null">
and birthday > #{birthday}
</if>
</where>
</select>
三、choose (when, otherwise) :分支选择
<!-- Choose分支选择 when otherwise标签的使用
1.when相当于java代码中的 if else if
2.otherwise相当于java代码中的else
3.otherwise一定要写
-->
<select id="getUserUnderChoose" resultType="user" parameterType="user">
select * from user
<where>
<choose>
<when test="username!=null and username != '' ">
username = #{username}
</when>
<when test="sex!=null and sex != '' ">
sex = #{sex}
</when>
<when test=""></when>
<otherwise>
1=1
</otherwise>
</choose>
</where>
</select>
四、 foreach
<!-- foreach标签的使用
注意:foreach的分割符 不可以自己手动写,
1.separator属性会自动填充
2.collection:要便利的集合在bean中的字段名,如果是非引用类型直接写类型名
3.open 起始要动态添加的sql片段
4.close 结束要动态添加的sql片段
-->
<delete id="deleteAll" parameterType="QueryVo">
delete from user
<where>
<foreach collection="idList" open="id in (" close=")" item="id"
separator=",">
#{id}
</foreach>
</where>
</delete>
<!-- foreach标签的使用 非引用类型parameterType可以不写 -->
<delete id="deleteAllIdList" parameterType="list">
delete from user
<where>
<foreach collection="list" open="id in (" close=")" item="id"
separator=",">
#{id}
</foreach>
</where>
</delete>
五、trim 一般用于insert语句
<!-- 动态sql之trim标签:常用那个在insert语句中, 利用trim标签处理前后缀 trim属性:
1.prifix:动态sql开始时的sql片段
2.suffix:动态sql结束后的sql片段
3.prefixOverrides:去除起始的第一个
4.suffixOverrides:去除结束的最后一个
注意:trim的工作机制是,去除第一个或者最后一个要处理的对象, 因此在每个sql片段中必须先都写上",",即本例中的逗号 -->
<insert id="insertUser" parameterType="user">
insert into user
<trim prefix="(" suffix=")" prefixOverrides=",">
<if test="username!=null and username != '' ">
,username
</if>
<if test="sex!=null and sex != '' ">
,sex
</if>
<if test="birthday!=null">
,birthday
</if>
</trim>
<trim prefix="values(" suffix=")" suffixOverrides=",">
<if test="username!=null and username != '' ">
#{username},
</if>
<if test="sex!=null and sex != '' ">
#{sex},
</if>
<if test="birthday!=null">
#{birthday},
</if>
</trim>
</insert>
六、set 一般用于update语句
<!-- 动态sql之set使用:用于update方法 set配合if条件可以实现根据传入参数,动态生成修改语句,并且动态去除最后一个逗号,
注意:每个if条件体中的逗号是必须写的 -->
<update id="updateUser" parameterType="user">
update user
<set>
<if test="username!=null and username != '' ">
username=#{username},
</if>
<if test="birthday!=null">
birthday=#{birthday},
</if>
<if test="sex!=null and sex != ''">
sex=#{sex},
</if>
</set>
<where>
id = #{id}
</where>
</update>
七、 bind 一般用于模糊查询
bind 元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文。
!-- 模糊查询 bind的使用:
1.使用bind的标签,可以实现调用预处理对象进行模糊查询
2.bind标签中取值时直接使用键名或者属性名,不可以写表达式
3.bind标签不能获取基本类型的传参,可借助javaBean和map等封装后进行获取 -->
<select id="fuzzyQuery" resultType="user" parameterType="map">
<bind name="uName" value=" '%'+ username +'%' "></bind>
select * from user
<where>
username like #{uName};
</where>
</select>
${value}字符串拼接,模糊查询,他使用的是普通的statement,非预处理对象
<!-- 字符串拼接方式的模糊查询, '%${value}%' 格式为固定写法,存在sql注入风险 -->
<select id="fuzzyQuery1" resultType="user">
select * from user
<where>
username like '%${value}%'
</where>
</select>
八、 include 引用sql标签
<!-- 1.sql标签,声明了一个id为table的可引用sql片段
2.include可以通过sql片段的id引用对应sql片段 -->
<sql id="table"> user</sql>
<select id="findAll">
select * from <include refid=""></include>
</select>