Mybatis模糊查询和动态sql语句的用法

2025-05-29 0 47

mybatis 模糊查询和动态sql语句

模糊查询

对数据库最常用的操作就是查询了,但是如何使用mybatis进行模糊查询呢?下面先看一个简单的模糊查询

?

1

2

3

4

5

6

7
<select id="select01" resultmap="basicresultmap">

select

*

from

oa_employee

where emp_name like #{asd}

</select>

这是一条伪模糊查询, 因为没有实现真正的模糊 “%”。参数为字符串,所以#{}中内容不被限制。但是应该如何插入 % 字符呢。 我们首先想到的是传递字符串参数时将%插入到字符串中 “张%”,但是这种方法操作略微繁琐了一些。 下面提供了使用sql方法的策略

?

1

2

3

4

5

6

7
<select id="select01" resultmap="basicresultmap">

select

*

from

oa_employee

where emp_name like concat( #{asd} ,'%')

</select>

另外一种不推荐的写法给大家

?

1

2

3

4

5

6

7
<select id="select01" resultmap="basicresultmap">

select

*

from

oa_employee

where emp_name like '${emp_name}%'

</select>

#{} 是采用预编译的写法,也就是jdbc中的perparestatement,这种写法可以防止sql注入,但${}这种写法是不采用预编译,其中的参数写成类中的属性或者map的key值或者为接口中注解的参数名。

mybatis 提供了bind 标签。下面举个例子

?

1

2

3

4

5

6

7

8
<select id="select01" resultmap="basicresultmap">

<bind name="emp_name" value="'%'+ _parameter.getemp_name() +'%'"/>

select

*

from

oa_employee

where emp_name like #{emp_name}

</select>

他是在#{}表达式自动填入value值,值得注意的是“_parameter.getemp_name()” 调用的方法是对象中作为查询参数的属性的get方法

多条件查询

多种条件查询的要点是判断查询条件是否为空,拼接sql语句。在mybatis中提供了if标签和where 标签。 下面来介绍两种标签的用法。

if标签

?

1

2

3

4

5

6

7

8

9

10

11

12

13
<select id="select01" resultmap="basicresultmap">

select

*

from

oa_employee

where 1=1

<if test="emp_name != null and emp_name != ''">

and emp_name = #{emp_name }

</if>

<if test="emp_sex != null and emp_sex != ''">

and sex = #{emp_sex}

</if>

</select>

mybatis 中的if标签有些类似于el表达式的使用,test中可以直接写入类中的属性或者key值。

where标签

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
<select id="select01" resultmap="basicresultmap">

select

*

from

oa_employee

<where>

<if test="emp_name != null and emp_name != ''">

and emp_name = #{emp_name }

</if>

<if test="emp_sex != null and emp_sex != ''">

and sex = #{emp_sex}

</if>

</where>

</select>

这里的where标签 替换了前一段代码的 where 1=1 。 mybatis中的where 标签会判断标签内是否有内容, 如果有内容就自动生成where 并把 where 后面的第一个and +一个空格,or+一个空格 去掉。

choose , when 和 otherwise 标签

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19
<select id="select01" resultmap="basicresultmap">

select

*

from

oa_employee

<where>

<choose>

<when test="emp_name != null and emp_name != ''">

and emp_name = #{emp_name }

</when>

<when test="emp_sex != null and emp_sex != ''">

and sex = #{emp_sex}

</when>

<otherwise>

emp_id = 50

</otherwise>

</choose>

</where>

</select>

当所有条件不满足时,执行otherwise标签的内容。

trim标签

?

1

2

3

4

5

6

7

8

9

10

11

12

13
<select id="select01" resultmap="basicresultmap">

select

*

from

oa_employee

<trim prefix="where" prefixoverrides="and |or ">

<if test="emp_name != null and emp_name != ''">

and emp_name = #{emp_name }

</if>

<if test="emp_sex != null and emp_sex != ''">

and sex = #{emp_sex}

</if>

</trim>

trim标签的属性及其含义

  • – prefix : 标签之间有内容在最前面加入
  • – prefixoverrides: 检查内容的最前面是否匹配,匹配就删除
  • – suffix: 标签之间有内容在最后面加入
  • – suffixoverrides:检查内容的最后面是否匹配,匹配就删除

set标签

set标签常用于update操作,并且会自动抹掉无关的,

?

1

2

3

4

5

6

7

8

9

10

11

12

13
<update id="update01" >

update

oa_employee

<set>

<if test="emp_name != null and emp_name != ''">

emp_name = #{emp_name}

</if>

<if test="emp_sex != null and emp_sex != ''">

,sex = #{emp_sex}

</if>

</set>

where emp_id = 50

</update>

foreach标签

foreach 用于处理数组或者list集合,下面是一个批量添加的例子

?

1

2

3

4

5

6

7

8

9
<insert id="insert01">

insert into

oa_employee

( emp_name, sex, fk_dept_id)

values

<foreach collection="list" item="employee" separator=",">

(#{employee.emp_name},#{employee.emp_sex},#{employee.fk_dept_id})

</foreach>

</insert>

其中 如果参数为数组 则collection只能为“array” 参数为list集合则collection只能为 “list” item类似jstl 中的var的作用, 指代容器中的每一个对象。separator=”,”的含义是每条数据以 , 分割。 未注明的属性有 open 和 close 他们的含义是在遍历开始和结束时分别添加其内容。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/LLY19960418/article/details/71124247

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Mybatis模糊查询和动态sql语句的用法 https://www.kuaiidc.com/109398.html

相关文章

发表评论
暂无评论