今天开始讲解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. Redhat Enterprise 5.4下安装配置Oracle 11g R2详细过程

    1.Linux环境配置准备 环境:Linux:Redhat Enterprise 5.4,DB:Oracle 11g R2 X64,Oracle安装到/home/oralce_11目录下. 配置过程如 ...

  2. 武汉科技大学ACM :1001: A+B for Input-Output Practice (I)

    Problem Description Your task is to Calculate a + b. Too easy?! Of course! I specially designed the ...

  3. python的hashlib模块

    # -*- coding: utf-8 -*- """python 的MD5 sha1 模块""" import hashlib #md5的 ...

  4. LR:Code - 60990,Code - 10343 问题解决

    Code - 60990 Error: Two Way Communication Error: Function two_way_comm_post_message /two_way_comm_po ...

  5. 关于Cococs中的CCActionEase(中)

    相比之前的速度正弦变化动作(这个东西叫什么更好一些?渐变动画?)与速度指数级变化动作,CCEaseIn/CCEaseOut/CCEaseInOut更具灵活性.你可以设置运动的速率,甚至是在运动的过程中 ...

  6. Update主循环、状态机的实现

    从写一段程序,到写一个app,写一个游戏,到底其中有什么不同呢?一段程序的执行时间很短,一个应用的执行时间很长,仅此而已. 游戏中存在一个帧的概念.   这个概念大家都知道,类比的话,它就是电影胶卷的 ...

  7. 基础 ADO.NET 访问MYSQL 与 MSSQL 数据库例子

    虽然实际开发时都是用 Entity 了,但是基础还是要掌握和复习的 ^^ //set connection string, server,database,username,password MySq ...

  8. Windows打印体系结构之Print Spooler概念与架构

    Windows打印体系结构之Print Spooler概念与架构Windows 思杰之路(陶菘) · 2016-09-06 22:07 房子好不好,对我而言始终都是肉体的栖居.对于灵魂,我从来不知道该 ...

  9. android http协议post请求方式

    方式一:HttpPost(import org.apache.http.client.methods.HttpPost 代码如下: private Button button1,button2,but ...

  10. JavaScript方法undefined/null原因探究及闭包简单实现

    昨天一个刚写前端不久的同学发消息问这个问题(如下图): HTML代码为(省略部分代码): <head> <script src="test.js">< ...