目录
一、动态sql的简述
什么是动态sql:在不同条件下拼接不同的sql
Mybatis框架的动态sql技术是一种根据特定条件动态拼接SQl语句的功能,存在的意义是为了解决拼接SQL语句字符串时的痛点问题。
比如我们要实现功能:根据 username 和 sex 来查询数据。如果username为空,那么将只根据sex来查询;反之只根据username来查询
<select id="selectUserByUsernameAndSex" parameterType="com.qcby.entity.User" resultType="com.qcby.entity.User">
select * from user where username = #{username} and sex = #{sex}
</select>
如果我们不使用动态sql语句,只输入username他就会返回一个空值
这就要用到动态sql
二、if标签
功能用于判断条件是否成立
比如在此处我们要知道是否为空。
<select id="selectUserByUsernameAndSex" parameterType="com.qcby.entity.User"
resultType="com.qcby.entity.User">
select * from user where
<if test="username != null and username = ''">
username=#{username}
</if>
<if test="sex != null and sex !=''">
and sex=#{sex}
</if>
</select>
可以看到if标签的使用非常简单,但是我们还是没有解决这个问题
当我们只查找性别的时候 前面有一个and————> sql语句会报错
三、where标签
功能:去掉where后面的and或者or
我们可以结合where和if使用
<select id="selectUserByUsernameAndSex" parameterType="com.qcby.entity.User"
resultType="com.qcby.entity.User">
select * from user
<where>
<if test="username != null">
username=#{username}
</if>
<if test="sex != null">
and sex=#{sex}
</if>
</where>
</select>
诺,功能完成了吧。接下来介绍 set if标签 这是用于update的
四、set标签
功能:去掉逗号
如:update user set username = '吉吉国王' ,address='森林','sex'=’男‘
如果我们不想修改全部属性,就需要用到set标签,它可以帮助我们去掉多余的,
不然我们只能把每个值都输一遍,然后修改整体。
我们在dao层接口写一个public int update(User user);
然后在mapper.xml写上
<update id="update" parameterType="com.qcby.entity.User">
update user
<set>
<if test="username !=null and username!=''">
username = #{username} ,
</if>
<if test="address != null and address != ''">
address = #{address}
</if>
</set>
where id = #{id}
</update>
这就是set标签的用法,也是非常简单。
我们可以只输入username或是address,来修改一项。四、set标签
@Test
public void update(){
User user = new User();
user.setId(5);
user.setSex("女");
mapper.update(user);
session.commit();
}
五、choose when 和otherwise标签
功能:
这个标签相当于是我们java当中的if.....elseif.....else
choose标签是这个标签组合当中的父标签和标签都在标签内部。
when标签就相当于是我们的 if 和 elseif
otherwise标签相当于是我们的 else
<select id="selectUserByChoose" resultType="com.qcby.entity.User"
parameterType="com.qcby.entity.User">
select * from user
<where>
<choose>
<when test="id !='' and id != null">
id=#{id}
</when>
<when test="username !='' and username != null">
and username=#{username}
</when>
<otherwise>
and sex=#{sex}
</otherwise>
</choose>
</where>
</select>
这就是用法 可以进行多重选择条件
六、格式化标签trim
trim标记是一个格式化的标记,可以完成set或者是where标记的功能
子标签:
prefix:前缀
prefixoverride:去掉sql语句前面的关键字,例如:and或者是or
suffix:后缀
suffixoverride:去掉sql语句后面的关键字
代码使用案例:
1改写where
<select id="selectUserByUsernameAndSex" parameterType="com.qcby.entity.User"
resultType="com.qcby.entity.User">
select * from user
<trim prefix="where" prefixOverrides="and | or">
<if test="username != null">
and username=#{username}
</if>
<if test="sex != null">
and sex=#{sex}
</if>
</trim>
</select>
2改写set
<update id="update" parameterType="com.qcby.entity.User">
update user
<trim prefix="set" suffixOverrides=",">
<if test="username != null and username != ''">
username = #{username},
</if>
<if test="sex != null and sex != ''">
sex = #{sex},
</if>
</trim>
where id = #{id}
</update>
七、foreach标签
功能:处理以数组形式出现的数据
子标签:
<!-- collection:当前要循环的数组或者集合 -->
<!-- item: 我们指定要循环的数组的每一个元素 -->
<!-- separator:每一个元素应该用什么来做分割 -->
<!-- open:当前循环是以什么开始 -->
<!-- close:当前循环是以什么结束 -->
open和close会加上以什么开始的元素。
批量删除
批量删除的sql语句:delete from user where id in (1,2,3,4,5);
<delete id="deleteMoreByArray">
delete from user where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
接口:int deleteMoreByArray(@Param("ids") Integer[] ids);
测试:
@Test
public void deleteMoreByArray(){
int result = mapper.deleteMoreByArray(new Integer[]{1,2,3});
session.commit();
System.out.println(result);
}
批量添加
<insert id="insertMoreByList" >
insert into user(id,username,birthday,sex,address) values
<foreach collection="users" item="user" separator=",">
(null,#{user.username},#{user.birthday},#{user.sex},#{user.address})
</foreach>
</insert>
接口int insertMoreByList(@Param("users") List<User> users);
@Test
public void insertMoreByList(){
User user1 = new User("a1","男","北京");
User user2 = new User("a2","男","上海");
User user3 = new User("a3","男","广州");
List<User> users = Arrays.asList(user1,user2,user3);
int result = mapper.insertMoreByList(users);
session.commit();
System.out.println(result);
}