05_mybatis动态sql
1.sql片段
1.sql片段****
mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。
2.需求
用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。
对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。
3.定义sql片段**
<!--定义sql片段-->
<!--id:标识sql片段-->
<!--经验:基于单表来定义sql片段,这样可以提高可重用性,一般不要包含where-->
<sql id="query_user_where">
<!--where 可以去掉条件中的第一个and-->
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!=''">
and user.sex=#{userCustom.sex}
</if>
<if test="userCustom.username!=null and userCustom.username!=''">
and user.username like '%${userCustom.username}%'
</if>
</if>
</sql>
4.引用sql片段
<select id="findUserList" parameterType="UserQueryVo" resultType="UserCustom">
select * from user
<where>
<!-- 引用sql片段,如果不在同一个mapper文件中,这时要加namespace-->
<include refid="query_user_where"/>
</where>
</select>
2.foreach
1.需求
在用户查询列表和查询总数的statement中增加多个id输入查询。
sql语句如下:
两种方法:
SELECT * FROM USER WHERE id=1 OR id=10 OR id=16
<!-- 法2-->
SELECT * FROM USER WHERE id IN(1,10,16)
2.在输入参数类型中添加List ids传入多个id**
/**
* Description: 查询时,将多个对象的属性集合在此类
* User: jiatp
* Date:2019/9/3 0003 下午 4:39
*/
public class UserQueryVo {
//用户的查询条件
private UserCustom userCustom;
//传入多个id
private List<Integer> ids;
//可以包装其它信息,商品,订单 等
public List<Integer> getIds() {
return ids;
}
public void setIds(List<Integer> ids) {
this.ids = ids;
}
public UserCustom getUserCustom() {
return userCustom;
}
public void setUserCustom(UserCustom userCustom) {
this.userCustom = userCustom;
}
}
3.修改mapper.xml
<!--定义sql片段-->
<!--id:标识sql片段-->
<!--经验:基于单表来定义sql片段,这样可以提高可重用性,一般不要包含where-->
<sql id="query_user_where">
<!--where 可以去掉条件中的第一个and-->
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!=''">
and user.sex=#{userCustom.sex}
</if>
<if test="userCustom.username!=null and userCustom.username!=''">
and user.username like '%${userCustom.username}%'
</if>
<if test="ids!=null">
<!--<foreach collection="ids" open="and (" close=")" item="user_id" separator="or">-->
<!--id=#{user_id}-->
<!--</foreach>-->
<foreach collection="ids" open="and id in(" close=")" item="user_id" separator=",">
#{user_id}
</foreach>
</if>
</if>
</sql>
4.测试代码
//综合查询,用户的所有信息 foreach
@Test
public void findUserListForEach(){
SqlSession sqlSession = sqlSessionFactory.openSession();
UserQueryVoMapper mapper = sqlSession.getMapper(UserQueryVoMapper.class);
//创建包装对象
UserQueryVo userQueryVo = new UserQueryVo();
UserCustom userCustom = new UserCustom();
//由于设置了动态sql
userCustom.setSex("1");
//userCustom.setUsername("李四");
userQueryVo.setUserCustom(userCustom);
//加入多个id进行查询
List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(10);
ids.add(22);
userQueryVo.setIds(ids);
//调用查询
List<UserCustom> userList = mapper.findUserList(userQueryVo);
for(UserCustom us:userList) {
System.out.println(us);
}
}
}
持续补充中…
05_mybatis动态sql的更多相关文章
- 值得注意的ibatis动态sql语法格式
一.Ibatis常用动态sql语法,简单粗暴用一例子 <select id="iBatisSelectList" parameterClass="java.util ...
- Mysql - 游标/动态sql/事务
游标这个在我目前的项目里面用的还不多, 但是其功能还是很强大的. 动态sql以前都没用过, 是跟着富士康(不是张全蛋的富土康哦)过来的同事学的. 还是挺好用的. 我的数据库方面, 跟他学了不少. 在此 ...
- MyBatis4:动态SQL
什么是动态SQL MyBatis的一个强大特性之一通常是它的动态SQL能力.如果你有使用JDBC或其他相似框架的经验,你就明白条件串联SQL字符串在一起是多么地痛苦,确保不能忘了空格或者在列表的最后的 ...
- 分享公司DAO层动态SQL的一些封装
主题 公司在DAO层使用的框架是Spring Data JPA,这个框架很好用,基本不需要自己写SQL或者HQL就能完成大部分事情,但是偶尔有一些复杂的查询还是需要自己手写原生的Native SQL或 ...
- MySQL存储过程动态SQL语句的生成
用Mysql存储过程来完成动态SQL语句,使用存储过程有很好的执行效率: 现在有要求如下:根据输入的年份.国家.节假日类型查询一个节假日,我们可以使用一般的SQL语句嵌入到Java代码中,但是执行效率 ...
- 【Java EE 学习 79 下】【动态SQL】【mybatis和spring的整合】
一.动态SQL 什么是动态SQL,就是在不同的条件下,sql语句不相同的意思,曾经在“酒店会员管理系统”中写过大量的多条件查询,那是在SSH的环境中,所以只能在代码中进行判断,以下是其中一个多条件查询 ...
- 自定义函数执行动态sql语句
--函数中不能调用动态SQL,使用用存储过程吧.如果还要对函数做其他操作,换成存储过程不方便,可以考虑把其他操作一起封装在存储过程里面.如: create proc [dbo].[FUN_YSCL ...
- mybatis入门基础(五)----动态SQL
一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态s ...
- mybatis 动态sql表达式相关应用
一.mybatis 表达式简介 对于mybatis3 ,提供了一种动态sql的方式.通过动态sql我们可以直接在mybatis 的xm映射文件中直接通过条件判断的方式进行查询添加的拼接.mybatis ...
随机推荐
- 【JZOJ3293】【BZOJ4416】【luoguP3989】阶乘字符串
description 给定一个由前n个小写字母组成的串S. 串S是阶乘字符串当且仅当前n个小写字母的全排列(共n!种)都作为S的子序列(可以不连续)出现. 由这个定义出发,可以得到一个简单的枚举法去 ...
- 单调栈(最大子矩形强化版)——牛客多校第八场A
求01矩阵里有多少个不同的1矩阵 首先预处理出pre[i][j]表示i上面连续的1个数,对每行的高度进行单调栈处理 栈里的元素维护两个值:pre[i][j]和向前延伸最多能维护的位置pos 然后算贡献 ...
- MDK(KEIL) 两步解决 中文乱码 及 中文光标 半个半个跳的问题
1. 如果已经用MDK(KEIL)的默认设置写了好多中文,那么先用notepad把文件一一打开然后转变编码格式为 utf-8 without ROM,如下: 2. 如果还没有开始编辑,或者已经用not ...
- POJ-1836-Alignment-双向LIS(最长上升子序列)(LIS+LSD)+dp
In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are al ...
- Python3 From Zero——{最初的意识:007~函数}
一.编写可接受任意数量参数的函数:*.** >>> def test(x, *args, y, **kwargs): ... pass ... >>> test(1 ...
- scp 传输下载
利用scp传输文件 1.从服务器下载文件 scp username@servername:/path/filename /tmp/local_destination 例如scp codinglog@1 ...
- 活动:月末送Java技术书福利|抽奖
本公众号运营了快一年了 原创干货超过200+ 收获了也快1W粉丝 这么多粉丝-- 送书活动怎能少? 虽然这次我们是有备而来 但是-- 所有书籍为作者自掏腰包 所以本次送书数量有限 不能满足到所有人 重 ...
- 02ubuntu下python环境安装
原文链接:https://blog.csdn.net/weixin_42549407/article/details/85198460 我安装的是python3.6.9 1.下载python的源码压缩 ...
- 在 Mac 上使用 `sed` 命令的几个坑
不可忽略的备份格式 sed -i 's/hello/world/g' hello.text 上面这行代码,可以在 linux 上运行,作用是将找到的 hello 替换为 world,并且直接保存修改到 ...
- http协议 头部字段 referrer
学习笔记,非原创,抄自:https://www.cnblogs.com/amyzhu/p/9716493.html:https://blog.csdn.net/java_zhangshuai/arti ...