今天开始讲解mybatis的第二天内容

 一,拼接sql

在mapper.xml中,会多次使用到同一条sql片段,这时为了简便书写,将其定义出来

<sql id="base_sql">

 goods_name,

good_no,

goods_price,

goods_desc

</sql>
<select id="queryGoodsByGoodsName" resultMape="GoodsMap">

select <include refid="base_sql" /> from goods g where g.goods_name='${goodsName}'

</select>

二,动态SQL

MyBatis 的一个强大的特性之一通常是它的动态 SQL 能力。提供了OGNL表达式动态生成SQL的功能。动态SQL有:

1、if

2、choose, when, otherwise

3、where, set

4、foreach

下面解释其用法:

if:

<if test="goodsNo!= null and goodsNo!= '' ">
and end_time <![CDATA[ <= ]]> #{goodNo}
</if>

2、choose, when, otherwise

<select id="queryUserLikeNameOrAge" resultType="User">
SELECT * FROM tb_user WHERE sex = 1
<choose>
  <when test="name != null and name !=''">
AND name like '%${name}%'
</when>
<when test="age !=null and age != ''">
AND age > #{age}
</when>
<otherwise>
AND created is not null
</otherwise>
</choose>
</select>

相当于java中 if  elseif 逻辑;

<select id="queryUserByIds" resultType="User">
    SELECT * FROM tb_user WHERE id IN
       <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
       </foreach>
</select>
String orderId = request.getParameter("orderId");
String tcms = request.getParameter("tcms");
if (tcms != null && !"".equals(tcms)) {
EcTcMessage ecTcMessage = new EcTcMessage();
Integer userId = Integer.parseInt(request.getSession().getAttribute("userId").toString());
ecTcMessage.setEditUserId(userId);
ecTcMessage.setIds(tcms.split(","));
ecTcMessage.setCallBackUserId(userId.toString());
ecTcMessage.setCallBackTime(DateUtils.getCurrentDateString());
ecTcMessage.setIsAdvisory("Y");
// ecTcMessage.setOrderId(orderId);
orderService.updateTcMessageIsBuy(ecTcMessage);
}
return "ok";

//ibatis  批量更新

传递参数是一个Model对象

<update id="update" parameterClass="ecTcMessage">
update ec_tc_message set
IS_ADVISORY =
'Y',CALLBACK_TIME=to_char(sysdate,'YYYY-MM-DD
HH24:mi:ss'),CALLBACK_USERID=#callBackUserId#,EDIT_USER_ID=#editUserId#,EDIT_TIME=sysdate
where TC_MESSAGE_ID in
<iterate property="ids" conjunction="," open="(" close=")">
#ids[]#
</iterate>
</update>

//批量更新

 <update id="update" parameterClass="PharmacistComment">
update PHARMACIST_COMMENT
<dynamic prepend="set"> <isNotEmpty prepend="," property="goodsNo">
<![CDATA[ GOODS_NO=#goodsNo# ]]>
</isNotEmpty> <isNotEmpty prepend="," property="isDelete">
<![CDATA[ IS_DELETE=#isDelete# ]]>
</isNotEmpty> <dynamic>
where PHARMACIST_COMMENT_ID=#pharmacistCommentId#
</update>

S

tring ids = null;
if (request.getParameter("ids") != null) {
ids = request.getParameter("ids").trim(); String[] idArr = ids.split(",");
for (int i = 0; i < idArr.length; i++) {
PharmacistComment bean = new PharmacistComment();
bean.setPharmacistCommentId(Long.parseLong(idArr[i]));
bean.setIsDelete("Y");
pharmacistCommentService.update(bean); }
}

//注意:

这两个功能都是根据id批量更新,不同的是参数封装的不一样,

第一个id在java里是一个数组,是在sql里面进行遍历,

第二个是在java代码里遍历后就直接先设置为'Y'了,

但是两个最后的功能都是一样的

mybati之day02的更多相关文章

  1. 学习day02

    day021.结构标记 ***** 做布局 1.<header>元素 <header></header> ==> <div id="heade ...

  2. 《Professional JavaScript for Web Developers》day02

    <Professional JavaScript for Web Developers>day02 1.在HTML中使用JavaScript 1.1 <script>元素 HT ...

  3. 《javascript经典入门》-day02

    <javascript经典入门>-day02 1.使用函数 1.1基本语法 function sayHello() { aler('Hello'); //...其他语句... } #关于函 ...

  4. tedu训练营day02

    1.Linux命令 1.关机.重启 关机 :init 0 重启 :init 6 2.rm 1.rm -rf 文件/目录 r :递归删除文件夹内的子文件夹 f :强制删除,force 2.练习 1.在用 ...

  5. Python基础-day02

    写在前面 上课第二天,打卡: 大人不华,君子务实. 一.进制相关 - 进制基础 数据存储在磁盘上或者内存中,都是以0.1形式存在的:即是以 二进制 的形式存在: 为了存储和展示,人们陆续扩展了数据的表 ...

  6. c#day02

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace testmys ...

  7. day02 基本数据类型与运算符

    day02 1.基本数据类型 2.算术运算符 +,-,*,/,%,++,-- 3.赋值运算符 =,+=,-=,*=,/=,%= 4.关系运算符 +=,-=,*=,/=,%=  结果是boolean类型 ...

  8. python开发学习-day02(元组、字符串、列表、字典深入)

    s12-20160109-day02 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

  9. 2017-2018-1 JAVA实验站 冲刺 day02

    2017-2018-1 JAVA实验站 冲刺 day02 各个成员今日完成的任务 小组成员 今日工作 完成进度 张韵琪 写博客.进行工作总结 100% 齐力锋 找背景音乐.开始界面图片.按钮图片.按钮 ...

随机推荐

  1. 002_系统表查询(sysdatabases等)

    002_系统表查询(sysdatabases等) --1.获取所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name --2.获取所有表 ...

  2. Knockoutjs官网翻译系列(二) Observable 数组

    承接前文,前文书说道了KO框架中如何使用observable的视图模型属性来与UI元素进行绑定并自动进行双向更新的事儿.observable属性除了服务基础数据类型之外,还定义了专门为服务数组类型的o ...

  3. jsp页面间的传值

    很多的时候我们只是把我们需要的数据,查询出来,然后用request.setAttribute("" ,"" )方法保存这个数据集合.再在我们能跳转到的下一个js ...

  4. ASP.NET页面传值与跳转

    asp.net页面传值的五种方法:QueryString,Session,Application,Request.Cookies,Server.Transfer 其中Cookie和Server.Tra ...

  5. 微软SpeechRecognitionEngine

    API官网手册:http://msdn.microsoft.com/zh-cn/library/System.Speech.Recognition.SpeechRecognitionEngine(v= ...

  6. win64安装及配置apache+php+mysql

    Windows是64位Win7,所以Apache.PHP和Mysql都选用的64位版本(最好是统一64位). Apache:httpd-2.4.7-win64.zip PHP:PHP-5.4.6-Wi ...

  7. ubuntu14.04使用reaver跑pin码

    今天刚说过没找到支持ubuntu14.04用reaver跑pin的旧版库文件这就有摸索到方法了... 另外安装系统ubuntu14.04以及一系列破解工具比如aircrack,minidwep等都不在 ...

  8. LED驅動芯片對LED壽命的影響

    5050年,領導作為一種新型節能光源在世界和中國有非常高的熱情和偉大的問題,不得不贏得市場占有率從室外到室內照明應用,中國也如雨后春筍般涌現在大型和小型LED照明企業.鑒于LED照明的主要原因是其促進 ...

  9. intrins.h 里面的函数都有什么,功能是什么?

    是c51中的intrins.h库 _crol_  字符循环左移 _cror_ 字符循环右移 _irol_   整数循环左移 _iror_  整数循环右移 _lrol_   长整数循环左移 _lror_ ...

  10. Qt全局热键(windows篇)(使用RegisterHotKey和句柄进行注册)

    转载:http://www.cuteqt.com/blog/?p=2088 Qt对于系统底层,一直没有很好的支持,例如串口并口通信,还有我们经常都会用到的全局热键,等等.既然Qt可能出于某种原因,不对 ...