mybatis动态sql详情

mac2022-06-30  42

mybatis动态拼装sql详情

MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。

MyBatis中用于实现动态SQL的元素主要有:

if

choose(when,otherwise)

trim

where

set

foreach

bind

<if>元素

在mybatis中if是最常用的判断语句,用来进行一些简单的判断,然后进行动态sql的拼接。

<select id="dynamicIfTest" parameterType="Blog" resultType="Blog"> select * from t_blog where 1 = 1 <if test="title != null"> and title = #{title} </if> <if test="content != null"> and content = #{content} </if> <if test="owner != null"> and owner = #{owner} </if> </select>

<choose>(<when>,<otherwise>)元素

这个类似于switch多分支语句。

<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog"> select * from t_blog where 1 = 1 <choose> <when test="title != null"> and title = #{title} </when> <when test="content != null"> and content = #{content} </when> <otherwise> and owner = "owner1" </otherwise> </choose> </select>

<where>,<trim>元素

where语句的作用主要是简化SQL语句中where中的条件判断的。

<select id="dynamicWhereTest" parameterType="Blog" resultType="Blog"> select * from t_blog <where> <if test="title != null"> and title = #{title} </if> <if test="content != null"> and content = #{content} </if> <if test="owner != null"> and owner = #{owner} </if> </where> </select>

where元素的作用是会在写入where元素的地方输出一个where,另外一个好处是你不需要考虑where元素里面的条件输出是什么样子的,MyBatis会智能的帮你处理,如果所有的条件都不满足那么MyBatis就会查出所有的记录,如果输出后是and 开头的,MyBatis会把第一个and忽略,当然如果是or开头的,MyBatis也会把它忽略;此外,在where元素中你不需要考虑空格的问题,MyBatis会智能的帮你加上。像上述例子中,如果title=null, 而content != null,那么输出的整个语句会是select * from t_blog where content = #{content},而不是select * from t_blog where and content = #{content},因为MyBatis会智能的把首个and 或 or 给忽略。

<trim>元素

trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;正因为trim有这样的功能,所以我们也可以非常简单的利用trim来代替where元素的功能,示例代码如下:

<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog"> select * from t_blog <trim prefix="where" prefixOverrides="and |or"> <if test="title != null"> title = #{title} </if> <if test="content != null"> and content = #{content} </if> <if test="owner != null"> or owner = #{owner} </if> </trim> </select> 使用trim元素不会出现语法错误

<set>元素

set元素主要是用在更新操作的时候,主要是在包含的语句前输出一个set,然后如果包含的语句是以逗号结束的话将会把该逗号忽略。有了set元素我们就可以动态的更新那些修改了的字段。也就是说set可以用来进行更新操作,因为每次更新并不是把所有的字段都更新,而是更新一部分。下面是一段示例代码:

<update id="dynamicSetTest" parameterType="Blog"> update t_blog <set> <if test="title != null"> title = #{title}, </if> <if test="content != null"> content = #{content}, </if> <if test="owner != null"> owner = #{owner} </if> </set> where id = #{id} </update>

需要注意的是:set元素中的内容不能都为空,如果都为空会出现语法错误。所以在进行更新的时候必须确保字段不能都为空。

<foreach>元素

<foreach>元素用于进行迭代遍历操作,因为有时候查询的数据不止一条。这是mybatis用于数组和集合的遍历方式。这个元素通常在构建in条件语句时使用。

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。

<select id="dynamicForeachTest" resultType="Blog"> select * from t_blog where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>

这个是单参数的时候,为list类型,它的mapper 代码文件:

public List<Blog> dynamicForeachTest(List<Integer> ids);

单参数array数组的类型时:

<select id="dynamicForeach2Test" resultType="Blog"> select * from t_blog where id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>

对应的mapper 代码文件:

public List<Blog> dynamicForeach2Test(int[] ids);

自己把参数封装成Map的类型

<select id="dynamicForeach3Test" resultType="Blog"> select * from t_blog where title like "%"#{title}"%" and id in <foreach collection="ids" index="index" item="item" open="(" separator="," close=")"> #{item} ---ids为map中的键,而item就是键对应的值 </foreach> </select>

对应的mapper代码文件:

public List<Blog> dynamicForeach3Test(Map<String, Object> params);

测试:

@Test public void dynamicForeach3Test() { SqlSession session = Util.getSqlSessionFactory().openSession(); BlogMapper blogMapper = session.getMapper(BlogMapper.class); final List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(2); ids.add(3); ids.add(6); ids.add(7); ids.add(9); Map<String, Object> params = new HashMap<String, Object>(); params.put("ids", ids); params.put("title", "中国"); List<Blog> blogs = blogMapper.dynamicForeach3Test(params); for (Blog blog : blogs) System.out.println(blog); session.close(); }

<bind>元素

在进行模糊查询时,使用${}进行字符串拼接是无法防止sql注入的,但是如果使用concat()函数进行连接,但是只对mysql有效而Oracle需要使用||,而bind元素可以跨越数据库使用。

它的功能是在当前OGNL上下文中创建一个变量并绑定一个值。

<select id="fuzzyQuery" resultType="Blog" parameterType="java.lang.String"> <!-- bind标签用于创建新的变量 _parameter为传入的参数--> <bind name="titleLike" value="'%'+_parameter+'%'"/> select * from t_blog where title like #{titleLike} </select>

转载于:https://www.cnblogs.com/jasonboren/p/11394721.html

最新回复(0)