mybati之day02
今天开始讲解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的更多相关文章
- 学习day02
day021.结构标记 ***** 做布局 1.<header>元素 <header></header> ==> <div id="heade ...
- 《Professional JavaScript for Web Developers》day02
<Professional JavaScript for Web Developers>day02 1.在HTML中使用JavaScript 1.1 <script>元素 HT ...
- 《javascript经典入门》-day02
<javascript经典入门>-day02 1.使用函数 1.1基本语法 function sayHello() { aler('Hello'); //...其他语句... } #关于函 ...
- tedu训练营day02
1.Linux命令 1.关机.重启 关机 :init 0 重启 :init 6 2.rm 1.rm -rf 文件/目录 r :递归删除文件夹内的子文件夹 f :强制删除,force 2.练习 1.在用 ...
- Python基础-day02
写在前面 上课第二天,打卡: 大人不华,君子务实. 一.进制相关 - 进制基础 数据存储在磁盘上或者内存中,都是以0.1形式存在的:即是以 二进制 的形式存在: 为了存储和展示,人们陆续扩展了数据的表 ...
- c#day02
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace testmys ...
- day02 基本数据类型与运算符
day02 1.基本数据类型 2.算术运算符 +,-,*,/,%,++,-- 3.赋值运算符 =,+=,-=,*=,/=,%= 4.关系运算符 +=,-=,*=,/=,%= 结果是boolean类型 ...
- python开发学习-day02(元组、字符串、列表、字典深入)
s12-20160109-day02 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- 2017-2018-1 JAVA实验站 冲刺 day02
2017-2018-1 JAVA实验站 冲刺 day02 各个成员今日完成的任务 小组成员 今日工作 完成进度 张韵琪 写博客.进行工作总结 100% 齐力锋 找背景音乐.开始界面图片.按钮图片.按钮 ...
随机推荐
- JDK1.5新特性随手记
1.静态导入 import static 静态导入前写法: public class TestStatic { public static void main(String[] args) { Sys ...
- 字符串匹配算法之Sunday算法
字符串匹配查找算法中,最着名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore).两个算法在最坏情况下均具有线性的查找时间.但是在实用上,KMP算法并不比最简 ...
- C语言初学 给已知公式求圆周率
公式: 圆周率=1-1/3+1/5-1/7+......+1/(4n-3)-1/(4n-1) #include<stdio.h> #include<math.h> main() ...
- Swift—重写-备
========================= 重写实例属性 我们可以在子类中重写从父类继承来的属性,属性有实例属性和静态属性之分,他们在具体实现也是不同的. 实例属性的重写一方面可以重写gett ...
- 编译TWRP-recovery教程及源码地址
TWRP这个是一个老外的开源项目,全称Team-Win-Recovery-Project Source:https://github.com/TeamWin/Team-Win-Recovery-Pro ...
- 转:CFile.Open()的使用说明
在程中碰到这个一段代码: 讲的是CFILE类的文件操作,故参考MSDN系统学习一下(翻译了一下英文): CFile file;CFileException fe; //打开文件if(!file.Ope ...
- 【转】Android菜单详解——理解android中的Menu--不错
原文网址:http://www.cnblogs.com/qingblog/archive/2012/06/08/2541709.html 前言 今天看了pro android 3中menu这一章,对A ...
- P2P网贷投资须谨防圈钱人
摘要:P2P领域依然呈现投资热潮,甚至部分国有大行也有意涉足.这个行业到底怎么了?P2P平台上高收益的理财产品,到底能买不? 新年才刚刚过了10天,就有4家网贷平台被爆出支付危机,P2P一时被 ...
- Delphi Dcp 和BPL的解释
dcp = delphi compiled package,是 package 编译时跟 bpl 一起产生出来的,记录着 package 中公开的 class.procedure.function.v ...
- HDOJ 1197 Specialized Four-Digit Numbers
Problem Description Find and list all four-digit numbers in decimal notation that have the property ...