Mybatis中的动态sql怎么利用OGNL表达式进行处理

发布时间:2020-11-24 15:03:39 作者:Leah
来源:亿速云 阅读:221

Mybatis中的动态sql怎么利用OGNL表达式进行处理?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

常用的Mybatis动态sql标签有6种:

      1. if 语句 (简单的条件判断)

      2. choose (when,otherwize) ,相当于Java 语言中的 switch ,与 jstl 中的choose 很类似.

      3. trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)

      4. where (主要是用来简化sql语句中where条件判断的,能智能的处理 and or ,不必担心多余导致语法错误)

      5. set (主要用于更新时)

      6. foreach (在实现 mybatis in 语句查询时特别有用)

(1) if

模糊查询

<select id="select1" resultType="BaseresultMap"> 
 SELECT * FROM User WHERE Age = ‘18' 
 <if test="name != null"> 
 AND name like #{name} 
 </if> 
</select> 

年龄18且可以模糊搜索姓名

(2)choose,when,otherwize

当Job参数有传入时,就找出对应工作的人,否则就找出Job为none的人,而不是所有人

<select id="select2" resultType="BaseresultMap"> 
 SELECT * FROM User WHERE Age = ‘18' 
 
 <choose> 
 <when test="Job != null"> 
 AND Job =#{Job} 
 </when> 
 <otherwise> 
 AND Job="none" 
 
 </otherwise> 
 </choose> 
</select> 

(3)foreach

 <select id="select5" resultType="BaseresultBase"> 
 select * from User where id in 
 <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> 
  #{item} 
 </foreach> 
 </select> 
 
public List<User> select5(List<Integer> ids); 

(4) where set trim

where,set

为什么要用where,因为单纯的写where可能会导致 where And ... 和 where .....情况的发生,Set也是一样的

当然 trim 标签是万能的

<select id="select3" resultType="BaseresultMap"> 
 SELECT * FROM User 
<where> 
 
 <if test="Age != null"> 
 Age = #{Age} 
 </if> 
 <if test="Job != null"> 
 AND Job like #{Job} 
 </if> 
 
<where> 
</select> 
 
<update id="update1"> 
 update User 
 <set> 
 <if test="username != null">username=#{username},</if> 
 <if test="password != null">password=#{password},</if> 
 <if test="Age != null">Age =#{Age}</if> 
 </set> 
 where id=#{id} 
</update> 
<pre code_snippet_id="2048504" snippet_file_name="blog_20161214_2_7439616" class="prettyprint lang-xml" name="code"><pre code_snippet_id="2048504" snippet_file_name="blog_20161214_2_7439616" name="code" class="html"><pre code_snippet_id="2048504" snippet_file_name="blog_20161214_2_7439616"></pre> 
<pre></pre> 
<pre></pre> 
<p></p> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
<pre code_snippet_id="2048504" snippet_file_name="blog_20161214_3_3393435" name="code" class="html"></pre><pre code_snippet_id="2048504" snippet_file_name="blog_20161214_3_3393435"></pre> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
 
</pre><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" rel="external nofollow" ><img src="https://cache.yisu.com/upload/information/20200623/121/113452.png"></a></div></pre> 

看完上述内容,你们掌握Mybatis中的动态sql怎么利用OGNL表达式进行处理的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. MyBatis动态sql
  2. 在Struts2中怎么利用ognl对数组进行遍历

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis ognl表达式 动态sql

上一篇:使用Navicat如何将文化导入或导出数据库

下一篇:在Android中使用TextView实现一个跑马灯效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》