if标签:
if标签通常是根据条件做判断,一般作为where子句的一部分
if表达式判断是否传递参数在其后面有一个test属性,该属性必填,为true或false,
为true时,会拼接该if中SQL片段,为false,则不拼接SQL子句
该test的判断是通过OGNL表达式判断
<select id="select1" parameterType="mybatis.pojo.student" resultType="mybatis.pojo.student">
select * from student
where 1=1
<if test="SID!=null and SID!=0">
and SID=#{SID}
</if>
<if test="Sname!=null">
and Sname=#{Sname}
</if>
</select>
注意:if标签无法去除后面的and,所以需要加上1=1,以保证SQL语句的正确性。
执行过程:
①只传递一个参数:
student student1=new student();
student1.setSID(1);
List<student> list=studentMapper1.select1(student1);
②传递两个参数:
student student1=new student();
student1.setSname("赵");
student1.setSID(1);
List<student> list=studentMapper1.select1(student1);
③不传递参数:
student student1=new student();
List<student> list=studentMapper1.select1(student1);
where标签:
where标签作用:如果该标签包含的元素有结果返回,就插入where
注意:与if标签不同的是插入where后会将where后面的and和or自动去除,而if不能需要拼接1=1;
<!--where标签使用-->
<select id="select1" parameterType="mybatis.pojo.student" resultType="mybatis.pojo.student">
select *
from student
<where>
<if test="SID!=null and SID!=0">
and SID=#{SID}
</if>
<if test="Sname!=null">
and Sname=#{Sname}
</if>
</where>
</select>
在where标签使用时一般和if标签一起使用,如果条件一个都不满足,则不拼接where条件,如果有一个或者是多个条件成立。
①传递两个参数:
②传递一个参数:
③不传递参数:
set标签:
如果标签包含的元素有返回值,就插入一个set
注意:如果set后面的SQL中以逗号结尾的,将最后的逗号去掉
接口:
public int update(@Param("id") Integer id,@Param("name") String name);
xml文件:
<update id="update">
update student
<set><if test="name!=null ">
Sname=#{name}
</if>
</set>
where SID =#{id};
</update>
trim标签:
去掉SQL中多余的and关键字、逗号,或者给SQL拼接上where或者set关键字
<update id="update">
update student
<trim prefix="set" suffixOverrides=",">
<if test="name!=null ">
Sname=#{name},
</if>
</trim>
where SID =#{id};
</update>
foreach标签:
用途:批量操作
属性:
collection属性:必填
当参数为数组时,collection属性是array
当参数是list时,collection属性是list
当参数是map时,collection属性属性是map
item属性:变量名,值是从集合中迭代遍历的每一个值
open属性:循环开始的字符串
close属性:循环结束的字符串
separator属性:每次循环的分隔符
index属性:索引的属性名,当集合是数组时可以通过index来获取数据,当迭代是map时,index表示的是key
测试Java代码代码:
public void selectstudentAll(){
SqlSession sqlSession = sqlSessionFactory.openSession();
studentmapper studentMapper1 =sqlSession.getMapper(studentmapper.class);
List<Integer> arraylist=new ArrayList<>();
arraylist.add(1);
arraylist.add(2);
arraylist.add(3);
arraylist.add(4);
List<student> list=studentMapper1.selectstudentaAll(arraylist);
Iterator<student> iterator = list.iterator( );
while (iterator.hasNext()){
System.out.println(iterator.next() );
}
}
xml文件:
<select id="selectstudentaAll" resultType="mybatis.pojo.student">
select * from student where SID in
<foreach collection="list" open="(" close=")" item="SID" separator=",">
#{SID}
</foreach>
</select>
接口文件:
List<student> selectstudentaAll(List list);
执行过程:
执行结果:
模糊匹配:
方法一:将通配符以参数(%,-)形式传入
Java测试代码:
public void selectlike(){
SqlSession sqlSession = sqlSessionFactory.openSession();
studentmapper studentMapper1 =sqlSession.getMapper(studentmapper.class);
student s1=studentMapper1.selectlike("%赵%");
System.out.println(s1);
}
xml文件:
<select id="selectlike" parameterType="String" resultType="mybatis.pojo.student">
select * from student
where Sname like #{name}
</select>
接口文件:
public student selectlike(@Param("name") String name);
执行过程:
运行结果:
方法二:使用MySQL提供的concat函数
concat(x,y)将x与y拼接在一起
java测试代码:
public void selectlike(){
SqlSession sqlSession = sqlSessionFactory.openSession();
studentmapper studentMapper1 =sqlSession.getMapper(studentmapper.class);
student s1=studentMapper1.selectlike("赵");
System.out.println(s1);
}
xml文件:
<select id="selectlike" parameterType="String" resultType="mybatis.pojo.student">
select * from student
where Sname like concat('%',concat(#{name},'%'))
</select>
接口文件:
public student selectlike(@Param("name") String name);
执行过程:
执行结果:
方法三:bind标签
注意:bind表达式中value属性处理中借助OGNL表达式,对应的参数需要具有getter方法
java测试代码:
public void selectlike(){
SqlSession sqlSession = sqlSessionFactory.openSession();
studentmapper studentMapper1 =sqlSession.getMapper(studentmapper.class);
student s1=studentMapper1.selectlike("赵");
System.out.println(s1);
}
xml文件:
<select id="selectlike" resultType="mybatis.pojo.student">
select * from student
<bind name="namesql" value="'%'+name+'%'"/>
where Sname like #{namesql}
</select>
接口文件:
public student selectlike(@Param("name") String name);
执行过程:
执行结果: