Mybatis动态SQL索引标签
IF标签
如果表(Product)的字段比较多的话,为了应付各个字段的查询,那么就需要写多条sql语句,这样就变得难以维护。
这个时候,就可以使用Mybatis 动态SQL里的if标签。
当mapper中的parametType为基本类型(如int,string等)时,是怎样使用的
最简单的使用方法:
<select id="list" parameterType="string" resultMap="ClassroomResultMap">
select id, name
from bc
where name = #{name}
</select>
这里的参数#{}中写什么变量名都可以,mybatis会自动给赋值。而当使用if语句时,比如
<select id="list" parameterType="string" resultMap="ClassroomResultMap">
select id, name
from bc
<where>
<if test="name != null and <span style="font-family: Arial, Helvetica, sans-serif;">name</span> != ''">
name like CONCAT('%','${name}','%')
</if>
</where>
</select>
会报错
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘name’ in ‘class java.lang.String’
原因:
mybatis自动调用OGNL寻找String的name属性
解决办法:
1、使用_parameter
<select id="list" parameterType="string" resultMap="ClassroomResultMap">
select id, name
from bc
<where>
<if test="_parameter != null and _parameter != ''">
name like CONCAT('%','${name}','%')
</if>
</where>
</select>
2、使用mybatis默认的对象名:value
<select id="list" parameterType="string" resultMap="ClassroomResultMap">
select id, name
from bc
<where>
<if test="value != null and value != ''">
name like CONCAT('%','${value}','%')
</if>
</where>
</select>
where标签
问题描述:
如果由多条条件判断,当某一条件缺失时,执行会报错。而使用where
<where>标签会进行自动判断
如果任何条件都不成立,那么就在sql语句里就不会出现where关键字
如果有任何条件成立,会自动去掉多出来的 and 或者 or。
例:只需某一条件有值即可。
set 标签
当 update 语句中没有使用 if 标签时,如果有一个参数为 null,都会导致错误。
当在 update 语句中使用if标签时,如果前面的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置 SET 关键字,并剔除追加到条件末尾的任何不相关的逗号。使用 if+set 标签修改后,如果某项为 null 则不进行更新,而是保持数据库原值。如下示例:
trim标签
trim 用来定制想要的功能,比如where标签就可以用
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
来替换
set标签就可以用
<trim prefix="SET" suffixOverrides=",">
...
</trim>
来替换
运行set标签中的代码,其效果是一样的。
choose 标签
Mybatis里面没有else标签,但是可以使用when otherwise标签来达到这样的效果。
<select id="listProduct" resultType="Product">
SELECT * FROM product_
<where>
<choose>
<when test="name != null">
and name like concat('%',#{name},'%')
</when>
<when test="price !=null and price != 0">
and price > #{price}
</when>
<otherwise>
and id >1
</otherwise>
</choose>
</where>
</select>
其作用是: 提供了任何条件,就进行条件查询,否则就使用id>1这个条件。
foreach 标签
foreach标签通常用于in 这样的语法里,如例,如图查询出id等于1,3,5的数据出来。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.how2java.pojo">
<select id="listProduct" resultType="Product">
SELECT * FROM product_
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
</mapper>
bind标签
bind取代模糊查询的好处是提高了代码的可移植性。
在进行模糊查询时,如果是MySQL数据库,常常用到的是一个concat,它用’%'和参数相连。然而在Oracle数据库则没有,Oracle数据库用连接符号”||“。
当我们有了bind元素,就不必使用数据库语言,而是使用MyBatis的动态SQL即可完成。所以无论是MySQL还是Oracle都可以使用这样的语句,从而提高了代码的可移植性。
//使用MySQL数据库
select * from product_ where name like concat('%',#{0},'%')
//使用Oracle数据库
select * from product_ where name like '%'||#{0}||'%'
//使用bind元素
<select id="listProduct" resultType="Product">
<bind name="likename" value="'%' + name + '%'" />
select * from product_ where name like #{likename}
</select>