MyBatis 的一个强大的特性之一通常是它的动态 SQL 能力;
MyBatis 使用一种强大的动态 SQL 语言来改进这种情形,这种语言可以被用在任意映射的 SQL 语句中。动态 SQL 元素和使用 JSTL 或其它相似的基于 XML 的文本处理器相似。MyBatis 采用功能强大的基于 OGNL 的表达式来消除其他元素。
假设数据库中有表person和card;其中person表的字段有pid,pname,page,psex,cid;
1.条件查询
if
使用基本的if判断条件,解决动态where条件。具体实例:
根据姓名和年龄进行查询:
<select id="selectPerson" parameterType="person" resultType="person">
select * from person where 1=1
<if test="pname!=null and pname!=''"> and
pname like '%${pname}%' </if>
<if test="page!=0"> <![CDATA[and page<=#{page}
]]>
</if>
<if test="psex!=null and psex!=''"> and psex=#{psex} </if>
</select>
此处在where条件后加1=1的恒等条件很好的解决了在接下来的if判断中各个条件前面的and添加问题,实现了将后面所有的if判断中的and条件一致化,统一都加and。select * from person where 1=1;
在动态 SQL 中所做的最通用的事情是包含部分 where 字句的条件。
<select id="selectPerson" parameterType="person" resultType="person">
select * from person
<where>
<if test="pname!=null and pname!=''"> and pname
like '%${pname}%' </if>
<if test="page!=0"> <![CDATA[and page<=#{page} ]]>
</if>
<if test="psex!=null and psex!=''"> and psex=#{psex} </if>
</where>
</select>
此处where标签:会自动去除第一个and关键字,如果所有的条件不存在了,则where关键字也消失
sql片段:解决一段sql代码的共享问题
<sql id="selectperson">
<if test="pname!=null and pname!=''">
and pname like '%${pname}%'
</if>
<if test="page!=0">
<![CDATA[and page<=#{page} ]]>
</if>
<if test="psex!=null and psex!=''">
and psex=#{psex}
</if>
</sql>
<select id="selectPerson" parameterType="person" resultType="person">
select * from person
<where>
pid=6
<include refid="selectperson" />
</where>
</select>
导入片段:<include refid="selectperson"/>
2.集合查询
foreach:解决集合查询问题
第一种方法:用封装类属性的方式:具体实例
根据年龄在某个集合中进行查询:
(1)mapper.xml映射文件:
<select id="selectPersonByAges" parameterType="personPo"
resultType="person">
select * from person where page in
<foreach collection="ages" item="item" open="(" close=")"
separator=",">
#{item}
</foreach>
</select>
1-collection:指定要遍历的集合
2-item:定义个临时变量接受集合中的值
3-open:以什么符号开始
4-close:以什么结尾
5-separator:每一个值之间的分隔符
(2)mapper接口定义的方法:
public List<Person> selectPersonByAges(PersonPo p);
PersonPo类:
import java.util.List;
public class PersonPo extends Person {
private List<Integer> ages;
public List<Integer> getAges() {
return ages;
}
public void setAges(List<Integer> ages) {
this.ages = ages;
}
}
(3)使用junit测试结果:
@Test
public void testSelectPersonByAges(){
PersonPo p=new PersonPo();
List<Integer> ages=new ArrayList<Integer>();
//ages.add(1);
ages.add(50);
ages.add(45);
p.setAges(ages);
List<Person> l=pm.selectPersonByAges(p);
for (Person person : l) {
System.out.println(person);
}
}
第二种方法:以list集合作为参数方式:
(1)mapper.xml映射文件:
<select id="selectPersonByAgeList" parameterType="java.util.List"
resultType="person">
select * from person where page in
<foreach collection="list" item="item" open="(" close=")"
separator=",">
#{item}
</foreach>
1-parameterType:java.util.List
2-collection:固定值:list
(2)mapper接口定义方法:
public List<Person> selectPersonByAgeList(List<Integer> ages);
(3)使用junit测试结果:
@Test
public void testSelectPersonByAgeList(){
List<Integer> ages=new ArrayList<Integer>();
//ages.add(1);
ages.add(50);
ages.add(45);
List<Person> l=pm.selectPersonByAgeList(ages);
System.out.println(l.size());
for (Person person : l) {
System.out.println(person);
}
}
第三种方法:以数组作为参数方式:
(1)mapper.xml映射文件
</select>
<select id="selectPersonByAgeArray" parameterType="Object[]"
resultType="person">
select * from person where page in
<foreach collection="array" item="item" open="(" close=")"
separator=",">
#{item}
</foreach>
</select>
1-parameterType:Object[]
2-collection:array
(2)接口定义方法:
public List<Person> selectPersonByAgeArray(int[] ages);//集合查询的数组方式
(3)使用junit测试结果:
@Test
public void testselectPersonByAgeArray(){
int[] ages=new int[2];
//ages.add(1);
ages[0]=50;
ages[1]=45;
List<Person> l=pm.selectPersonByAgeArray(ages);
System.out.println(l.size());
for (Person person : l) {
System.out.println(person);
}
}