Mybatis动态sql语句
来源:互联网 发布:网络客服招聘条件 编辑:程序博客网 时间:2024/06/08 04:27
传统的使用JDBC的方法,相信大家在组合复杂的的SQL语句的时候,需要去拼接,稍不注意哪怕少了个空格,都会导致错误。Mybatis的动态SQL功能正是为了解决这种问题, 其通过 if, choose, when, otherwise, trim, where, set, foreach,sql,include,bind标签,可组合成非常灵活的SQL语句,从而提高开发人员的效率。动态 SQL是MyBatis强大特性之一。极大的简化我们拼装SQL的操作。
MyBatis 采用功能强大的基于 OGNL 的表达式来简化操作。
1.if功能
简单的条件判断,利用if语句我们可以实现某些简单的条件选择。判断为拼接、否则忽略。
public List<Student> getStudent(Student student);<!-- 自定义条件查询 --><select id="getStudent" resultMap="StudentMap"> select * from student where 1=1 <if test="id != null"> and t_id=#{id} </if> <if test="studentName != null and studentName != '' "> and t_name like #{studentName} </if> <if test="age != null"> and t_age=#{age} </if></select> @Test public void TestGetSchool() { try { StudentMapper mapper=session.getMapper(StudentMapper.class); System.out.println(mapper); Student stu=new Student(); //stu.setId(2); //stu.setStudentName("wa"); stu.setAge(15); List<Student> students=mapper.getStudent(stu); for(Student student:students) { System.out.println(student.getId() + " " + student.getStudentName() + " "+student.getAge()+" "+student.getEnterDate()+student.getSid()); } } finally { session.close(); } }2.where 功能 where标签语句的作用主要是简化SQL语句中where中的条件判断,where元素的作用是会在写入where元素的地方输出一个where,另外一个好处是你不需要考虑where元素里面的条件输出是什么样子的,MyBatis会智能的帮你处理,如果所有的条件都不满足那么MyBatis就会查出所有的记录,如果输出后是and开头的,MyBatis会把第一个and忽略,当然如果是or开头的,MyBatis也会把它忽略;此外,在where元素中你不需要考虑空格的问题,MyBatis会智能的帮你加上。
public List getStudent1(Student student);
select * from student
and t_id=#{id}
and t_name like #{studentName}
and t_age=#{age}
3.choose 功能
choose元素的作用就相当于JAVA中的switch语句,基本上跟JSTL中的choose的作用和用法是一样的,通常都是与when和otherwise搭配的。when元素表示当when中的条件满足的时候就输出其中的内容,跟JAVA中的switch效果差不多的是按照条件的顺序,当when中有条件满足的时候,就会跳出choose,即所有的when和otherwise条件中,只有一个会输出,当所有的我很条件都不满足的时候就输出otherwise中的内容。
<!-- 自定义条件查询 使用choose--><select id="getStudent2" resultMap="StudentMap"> select * from student where 1=1 <choose> <when test="id != null"> and t_id=#{id} </when> <when test="studentName != null and studentName != ''"> and t_name like #{studentName} </when> <when test="age != null"> and t_age=#{age} </when> <otherwise> and 2=2 </otherwise> </choose></select><!-- 自定义条件查询 使用choose where--><select id="getStudent3" resultMap="StudentMap"> select * from student <where> <choose> <when test="id != null"> and t_id=#{id} </when> <when test="studentName != null and studentName != ''"> and t_name like #{studentName} </when> <when test="age != null"> and t_age=#{age} </when> <otherwise><!-- and 2=2 --> </otherwise> </choose> </where></select>
4.set 功能
set元素主要是用在更新操作的时候,它的主要功能和where元素其实是差不多的,主要是在包含的语句前输出一个set,然后如果包含的语句是以逗号结束的话将会把该逗号忽略,如果set包含的内容为空的话则会出错。有了set元素我们就可以动态的更新那些修改了的字段。
public int updateStudent(Student student); <!-- 修改学生 使用set标签--> <update id="updateStudent"> update student <set> <if test="studentName != null and studentName != ''"> t_name = #{studentName}, </if> <if test="age != null"> t_age=#{age}, </if><if test="sid != null"> t_sid=#{sid}, </if> </set> where t_id = #{id} </update>
5.trim 功能
trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是 prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和 suffixOverrides;正因为trim有这样的功能,所以我们也可以非常简单的利用trim来代替where元素的功能
<!-- 自定义条件查询 使用trim --> <select id="getStudent4" resultMap="StudentMap"> select * from student <trim prefix="where" prefixOverrides="and | or"> <if test="id != null"> and t_id=#{id} </if> <if test="studentName != null and studentName != ''"> and t_name like #{studentName} </if> <if test="age != null"> and t_age=#{age} </if> </trim> </select><!-- 自定义条件查询 使用trim --> <select id="getStudent5" resultMap="StudentMap"> select * from student <trim prefix="where" suffixOverrides="and | or"> <if test="id != null"> t_id=#{id} and </if> <if test="studentName != null and studentName != ''"> t_name like #{studentName} and </if> <if test="age != null"> t_age=#{age} and </if> </trim> </select>代替where元素的功能
6.foreach 功能
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
如果传入的是单参数且参数类型是一个List的时候,collection属性值为list。
如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array。
如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key。
{item}就能取出变量的值也就是当前遍历出的元素。
1)查询
//参数是list,多参数查询 //public List<Student> getStudent6(List<Integer> list); public List<Student> getStudent6(@Param("list")List<Integer> list);<!-- 自定义条件查询 使用foreach --> <select id="getStudent6" resultMap="StudentMap"> select * from student where t_id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
2)批量添加
Insert into student (t_name,t_age,t_enterdate,t_sid) values('p1',23,'2009-2-23',2),('p2',23,'2009-2-23',2),('p3',23,'2009-2-23',2)//参数是list,批量添加 //public int addStudentbatch(List<Student> list); public int addStudentbatch(@Param("list")List<Student> list); <!-- 批量保存 使用foreach--> <!--MySQL下批量保存:可以foreach遍历 mysql支持values(),(),()语法--> <insert id="addStudentbatch"> insert into student(t_name,t_age,t_enterdate,t_sid) values <foreach collection="list" item="student" separator=","> (#{student.studentName},#{student.age},#{student.enterDate},#{student.sid}) </foreach> </insert>
7.sql,include功能
抽取可重用的sql片段。方便后面引用:
1)sql抽取:经常将要查询的列名,或者插入用的列名抽取出来方便引用。
2)include来引用已经抽取的sql。
<!-- sql标签,include标签 --> <sql id="studentfield"> (t_name,t_age,t_enterdate,t_sid) </sql> <insert id="addStudentbatch1"> insert into student <include refid="studentfield"> </include> values <foreach collection="list" item="student" separator=","> (#{student.studentName},#{student.age},#{student.enterDate},#{student.sid}) </foreach> </insert>
8.bind 功能
bind标签可以将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值。
<!-- 自定义条件查询 使用bind标签 --> <select id="getStudent7" resultMap="StudentMap"> <bind name="stuname" value=" '%'+studentName+'%' "/> select * from student where 1=1 <if test="id != null"> and t_id=#{id} </if> <if test="studentName != null and studentName != ''"> and t_name like #{stuname} </if> <if test="age != null"> and t_age=#{age} </if> </select>
- mybatis动态SQL语句
- mybatis动态SQL语句
- mybatis动态SQL语句
- MyBatis 动态SQL语句
- mybatis 动态SQL语句
- mybatis 动态sql语句
- mybatis动态SQL语句
- Mybatis 动态SQL语句
- mybatis动态sql语句
- MyBatis动态SQL语句
- Mybatis动态sql语句
- mybatis动态SQL语句
- mybatis 动态SQL语句
- mybatis动态SQL语句
- MyBatis动态Sql语句
- mybatis动态SQL语句
- mybatis动态SQL语句
- mybatis动态SQL语句
- Android 简单EventBus登录界面与传值(粘性事件)
- 【二】Springboot+Mybatis+Redis实现用户信息查询缓存
- 微信小程序底部选项卡/导航栏实现方法
- 21 容器deque&queue&stack深度探索(学自Boolean)
- ssm(struts2、spring、ibatis)框架使用注意事项
- Mybatis动态sql语句
- Windows10下使用Anaconda 配置tensorflow深度学习开发环境
- MVP泛型请求数据
- python内部装饰器@classmethod, @staticmethod, @property
- 随笔00--START NEW LIFE
- Executor模块详解
- 贪心算法——最优装载问题
- 单例模式:饿汉和懒汉式
- 日常