<!--动态if标签查询--><selectid="findByNameAndAddressIf"resultType="com.intellif.mozping.entity.People">
select *
from tb_people p
<where><iftest="name != null and name != ''">
and p.name = #{name}
</if><iftest="address != address and address != ''">
and p.address = #{address}
</if></where></select>
<!--第一种:--><selectid="findByNameAndAddressIf"resultType="com.intellif.mozping.entity.People">
select *
from tb_people p
where
<iftest="name != null and name != ''">
and p.name = #{name}
</if><iftest="address != address and address != ''">
and p.address = #{address}
</if></select><!--条件都为null时,打印出来的预编译语句是:select * from tb_people p where ,语法会报错--><!--第二种:--><selectid="findByNameAndAddressIf"resultType="com.intellif.mozping.entity.People">
select *
from tb_people p
<where><iftest="name != null and name != ''">
and p.name = #{name}
</if><iftest="address != address and address != ''">
and p.address = #{address}
</if></where></select><!--条件都为null时,打印出来的预编译语句是:select * from tb_people p -->
<!--trim标签,--><selectid="findByNameAndAddressTrim"resultType="com.intellif.mozping.entity.People">
select *
from tb_people p
<trimprefix="where"prefixOverrides="AND|OR"><iftest="name != null and name != ''">
and p.name = #{name}
</if><iftest="address != address and address != ''">
and p.address = #{address}
</if></trim></select>
代码
@TestpublicvoidqueryTrim(){
SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSessionFactoryInstaceByConfig(CONFIG_FILE_PATH).openSession();
PeopleMapper peopleMapper = sqlSession.getMapper(PeopleMapper.class);
List<People> byNameAndAddressIf = peopleMapper.findByNameAndAddressTrim("Parker","tianjing");for(People p : byNameAndAddressIf){
System.out.println(p);}}
打印:
15:41:58.869[main] DEBUG c.i.m.d.P.findByNameAndAddressTrim -==> Preparing: select * from tb_people p where p.name =?15:41:58.889[main] DEBUG c.i.m.d.P.findByNameAndAddressTrim -==> Parameters:Parker(String)15:41:58.906[main] DEBUG c.i.m.d.P.findByNameAndAddressTrim -<== Total:1People(id=2, name=Parker, age=20, address=tianjing, edu=Bachelor)
注意:
prefix="where" 会自动给我们加上where前缀
prefixOverrides="AND|OR"的作用是去除第一个And或者OR,为什么呢,在这个测试中传了2个条件,因此没有这个的话,是where and name ="Parker" and address ="tianjing" ,sql语法是错误的。
假如这里连续多个条件,那么每一个条件都有可能不传,不传的那个会被忽略,因此其实并不知道哪一个if里面的and是第一个and,所以无法确定哪一个里面的and不写,所以只能都写了,让框架去去除
第一个and。
在set赋值更新语句中也是一样,需要将最后面的逗号给去掉,原理和前缀类似
<trimsuffixOverrides=","><iftest="username!=null">
name = #{username},
</if><iftest="age != 0">
age = #{age},
</if></trim>
六、foreach
用于遍历数组或者集合,比如一个集合中包含很多主键id,要查询这些id对应的数据。
属性
说明
collection
collection属性的值有三个分别是list、array、map三种
open
前缀
close
后缀
separator
分隔符,表示迭代时每个元素之间以什么分隔
item
表示在迭代过程中每一个元素的别名
index
用一个变量名表示当前循环的索引位置
6.1 查询
映射配置:
<!--forEach--><selectid="findByIdForEach"resultType="com.intellif.mozping.entity.People">
select *
from tb_people where id in
<foreachcollection="ids"open="("close=")"separator=","item="id">
#{id}
</foreach></select>
测试
//Java接口:
List<People>findByIdForEach(@Param("ids")List<Integer> ids);//测试:@TestpublicvoidqueryForEach(){
SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSessionFactoryInstaceByConfig(CONFIG_FILE_PATH).openSession();
PeopleMapper peopleMapper = sqlSession.getMapper(PeopleMapper.class);
ArrayList<Integer> ids =newArrayList<>();for(int i =1; i <10; i++){
ids.add(i);}
List<People> peopleList = peopleMapper.findByIdForEach(ids);for(People p : peopleList){
System.out.println(p);}}
6.2 插入
插入操作也类似,比如插入一个多个对象,插入一个集合
<insertid="insertPeopleList">
insert into t_people(name,age) values
<foreachcollection="users"item="user"separator=",">
(#{user.name},#{user.age})
</foreach></insert>
七、bind
bind 元素可以从OGNL表达式中创建一个变量并将其绑定到上下文。说起来比较抽象,看示例。
映射文件
<!--forEach--><selectid="findByBind"resultType="com.intellif.mozping.entity.People"><!-- 声明了一个参数queryId,在后面就可以使用了 --><bindname="queryId"value="1"/>
select * from tb_people where id = ${queryId}
</select>
<!--sql--><sqlid="baseSql">
name, age
</sql><selectid="findBySql"resultType="com.intellif.mozping.entity.People">
select
<includerefid="baseSql"/>
from tb_people
</select>