根据 username 和 sex 来查询数据。如果username为空,那么将只根据sex来查询;反之只根据username来查询。
mapper层
java复制代码
<select id="selectUser" 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>
<if test="birthday!=null">
and birthday=#{birthday}
</if>
<if test="address!=null and address!=''">
and address=#{address}
</if>
<if test="id!=null">
and id=#{id}
</if>
</where>
</select>
@Test
public void selectUser(){
User user = new User();
user.setUsername("熊大");
user.setSex("男");
List<User> users = mapper.selectUser(user);
for (User u:users){
System.out.println(u.toString());
}
}
结果如下
2、修改
<set>---<if> 标签 ------ 用来组装update语句
mapper层
java复制代码
<update id="updateUser" parameterType="com.qcby.entity.User">
update user
<set>
<if test="username!=null and username!=''">
username=#{username},
</if>
<if test="sex!=null and sex!=''">
sex=#{sex},
</if>
<if test="birthday!=null">
birthday=#{birthday},
</if>
<if test="address!=null and address!=''">
address=#{address},
</if>
</set>
where id=#{id}
</update>
注意where id语句放在<set>标签外,并且<if>标签里的SQL语句使用逗号隔开
Dao接口
java复制代码
int updateUser(User user);
Test类
不要忘记 session.commit();
java复制代码
@Test
public void updateUser(){
User user = new User();
user.setId(1);
user.setBirthday(new Date());
user.setAddress("大连");
mapper.updateUser(user);
session.commit();
}
<select id="ChooseUser" parameterType="com.qcby.entity.User" resultType="com.qcby.entity.User">
select * from user
<where>
<choose>
<when test="username!=null and username!=''">
username = #{username}
</when>
<when test="sex!=null and sex!=''">
sex = #{sex}
</when>
<otherwise>
id = #{id}
</otherwise>
</choose>
</where>
</select>
Dao接口
java复制代码
List<User> ChooseUser(User user);
Test类
java复制代码
@Test
public void chooseUser(){
User user = new User();
user.setId(1);
//user.setUsername("张三");
//user.setSex("男");
List<User> users = mapper.ChooseUser(user);
for (User u:users){
System.out.println(u.toString());
}
}
结果如下:
4、<trim>标签
trim标记是一个格式化的标记,可以完成set或者是where标记的功能
(1)用 trim 改写上面select的 if+where 语句
prefix:前缀
prefixoverride:去掉第一个and或者是or
mapper层
java复制代码
<select id="TrimSelectUser" 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 = #{username}
</if>
<if test="sex!=null and sex!=''">
and sex = #{sex}
</if>
<if test="birthday !=null">
and birthday = #{birthday}
</if>
<if test="address !=null and address!=''">
and address = #{address}
</if>
<if test="id !=null">
and id = #{id}
</if>
</trim>
</select>
(2)用 trim 改写上面update的 if+set 语句
suffix:后缀
suffixOverrides:去掉后缀
java复制代码
<update id="TrimUpdateUser" 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>
<if test="birthday!=null">
birthday = #{birthday} ,
</if>
<if test="address !=null and address !=''">
address = #{address} ,
</if>
</trim>
where id = #{id}
</update>
int insertMoreUser(@Param("users") List<User> users);
Test类
java复制代码
@Test
public void insertMoreUser(){
User user1 = new User("aa",new Date(),"男","北京");
User user2 = new User("bb",new Date(),"男","北京");
User user3 = new User("cc",new Date(),"男","北京");
List<User> users = Arrays.asList(user1,user2,user3);
mapper.insertMoreUser(users);
session.commit();
}
结果如下:
6.批量删除
<foreach>标签的应用
collection:当前要循环的数组或者集合
item: 我们指定要循环的数组的每一个元素
separator:每一个元素应该用什么来做分割
open:当前循环是以什么开始
close:当前循环是以什么结束
mapper层
java复制代码
<delete id="deleteMore" >
delete from user where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
Dao接口
java复制代码
int deleteMore(@Param("ids") Integer[] ids);
Test类
java复制代码
@Test
public void deleteMore(){
Integer[] ids = new Integer[]{6,7,8};
mapper.deleteMore(ids);
session.commit();
}