欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > Mybatis XML基本使用

Mybatis XML基本使用

2025/3/13 22:38:39 来源:https://blog.csdn.net/swg321321/article/details/146195615  浏览:    关键词:Mybatis XML基本使用

Mybatis

文章目录

  • Mybatis
  • 前言
  • if 判断
    • 字符串判断
    • if 整数判断
    • 使用示例代码
  • case when 使用替代if else
    • 示例代码


前言

在XML 判断中 test 的 表达式 最好写在 ' ' 里面。
test 的 表达式 采用" " 写,当字符串是单个字符会报NumberFormatException异常

在XML中判断时候不能使用 < 需要用 &lt; 替代。

if 判断

if判断表达式里面最好是单引号作为表达式的区间

字符串判断

如果字符串是单个字符采用下列写法会报”NumberFormatException"

<if test = "sex != null and sex != '' and sex == 'M'">

需要修改一下如下方法才不会报错

<if test = 'sex != null and sex != "" and sex == "M"'>

if 整数判断

<if test = 'age == 10'>

使用示例代码

  select *from  <if test = 'condition &lt;= 1' >table_1</if><if test = 'condition > 1'>table_2</if>

结果
传入参数 condition = 1

select *
from  
table_1

传入参数 condition = 2

select *
from  
table_2

case when 使用替代if else

<choose><when test = 'condition == 1'>条件一执行如下SQL</when><when test = 'condition == 2'>条件二执行如下SQL</when><otherwise>上面条件不满足执行当前SQL</otherwise>
</choose>

示例代码

select * 
from order
where 1 = 1
<choose><when test = 'condition == 1'>and price > 10   /**价格超过10元*/</when><when test = 'condition == 2'>and type = 1  /**订单类型为 数码*/</when><otherwise>and name like '%科技%'</otherwise>
</choose>

结果
传入参数 condition = 1

select * 
from order
where 1 = 1 and  price > 10

传入参数 condition = 2

select * 
from order
where 1 = 1 and type = 1

传入参数 condition = 10

select * 
from order
where 1 = 1 and name like '%科技%'

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词