源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Mybatis利用OGNL表达式处理动态sql的方法教程

  • 时间:2020-02-24 19:34 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Mybatis利用OGNL表达式处理动态sql的方法教程
本文介绍的是关于Mybatis中用OGNL表达式处理动态sql的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍: [b]常用的Mybatis动态sql标签有6种:[/b]       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 语句查询时特别有用) [b](1) if[/b] 模糊查询
<select id="select1" resultType="BaseresultMap"> 
 SELECT * FROM User WHERE Age = ‘18' 
 <if test="name != null"> 
 AND name like #{name} 
 </if> 
</select> 
年龄18且可以模糊搜索姓名 [b](2)choose,when,otherwize[/b] 当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> 
[b](3)foreach[/b]
 <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); 
[b](4) where set trim[/b] 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="http://static.blog.csdn.net/images/save_snippets.png"></a></div></pre> 
[b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如有疑问大家可以留言交流,谢谢大家对编程素材网的支持。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部