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% 齐力锋 找背景音乐.开始界面图片.按钮图片.按钮 ...
随机推荐
- 系统管理中 bash shell 脚本常用方法总结
在日常系统管理工作中,需要编写脚本来完成特定的功能,编写shell脚本是一个基本功了!在编写的过程中,掌握一些常用的技巧和语法就可以完成大部分功能了,也就是2/8原则 1. 单引号和双引号的区别 单引 ...
- JavaScript 超类与子类 继承
//超类和子类 继承的实现 function R(w, h) { var date = new Date(); this.width = w; this.height = h; this.create ...
- Xaml 页面布局学习
对于一开始设计xaml界面的初学者,总是习惯性的拖拽控件进行布局,这样也许方便.简单.快捷,但偶尔会出现一些小错误, 当需要将控件进行很细微的挪动时也比较吃力. 这里,我个人建议用一些代码将xaml界 ...
- python记录
1. 序列的分片操作:需要提供两个索引作为边界,第1个索引的元素包含在分片内,第2个索引的元素不包含在分片内. 为了能让分片部分能够包含列表的最后一个元素,必需提供最后一个元素的下一个元素所对应的索引 ...
- Secure CRT 如何连接虚拟机里面的CentOS系统 当主机使用有线网的时候 作者原创 欢迎转载
1.虚拟机的网卡配置如下图所示: 2.在CentOS 5.8的命令行界面:输入如下指令 然后准备修改里面的网关地址和自己的IP地址 3.同时查看自己的IP地址和网关 4.在第二步里面修改,网关地址应该 ...
- vs2012远程调试
不知道大家有没有遇到过这种情况,刚开发完的程序,明明在本机能够好好的运行,可是部署到服务器过分发给用户时,总是出现莫名其妙的错误. 一时半会又看不出问题来,怎么办呢?难道只能在服务器或是客户电脑上装一 ...
- cxf所用的lib
cxf_lib
- 【学习笔记】【Foundation】数组
数组:可重复,有序 不可变数组 创建数组 //array开头的方法是类方法,init开头的是实例方法 //NSArray* arr=[[NSArray alloc]initWithObjects:@& ...
- You don't have permission to access /phpmyadmin/main.php on this server.
wamp 安装后,打开首页.出现问题,信息如下: “You don't have permission to access /phpmyadmin/main.php on this server.” ...
- Ftp协议Socket实现
原来用WebRequest来传输文件,被人鄙视了.就弄个Socket版的,支持Active,Passive模式. 带事件日志,有时间的人可以拿去做C#版的flashfxp. public class ...