mybatis动态SQL多条件查询1 - if 标签
Mybatis框架的动态SQL技术是一种根据特定条件动态拼接SQL语句的过程。它的存在是为了解决拼接SQL语句字符串的痛点问题。创建新的Mapper接口和新的mapper映射文件。
目录
1.创建DynamicSqlMapper接口
2.创建DynamicSqlMapper映射文件
3.测试类
4.测试结果
5. 引伸 : 其中一样传入信息不符合
5.1 and后的参数不提供
5.1.1运行效果
5.2 where后的参数不提供
5.2.1 运行效果
5.2.2 解决方法
6. 总结
1.创建DynamicSqlMapper接口
package com.mybatis.mapper;import com.mybatis.pojo.Emp;import java.util.List;/*** @author 大力pig* @date 2022/05/07*/
public interface DynamicSqlMapper {/*** 多条件查询员工信息*/List<Emp> getEmpByCondition(Emp emp);
}
2.创建DynamicSqlMapper映射文件
if test标签后不需要写原来的${}或者#{}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.mapper.DynamicSqlMapper"><!-- List<Emp> getEmpByCondition(Emp emp);--><select id="getEmpByCondition" resultType="Emp">select * from t_emp where<if test="empName != null and empName != ''">emp_name = #{empName}</if><if test="age != null and age != ''">and age = #{age}</if><if test="sex != null and sex != ''">and sex = #{sex}</if><if test="email != null and email != ''">and email = #{email}</if></select>
</mapper>
3.测试类
@Testpublic void testGetEmpByCondition(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();DynamicSqlMapper mapper = sqlSession.getMapper(DynamicSqlMapper.class);List<Emp> emps = mapper.getEmpByCondition(new Emp(null, "张三", 32, "男", "123@qq.com"));System.out.println(emps);}
4.测试结果
11:08:49:276 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource 434 - Created connection 1234250905.
11:08:49:292 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==> Preparing: select * from t_emp where emp_name = ? and age = ? and sex = ? and email = ?
11:08:49:338 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==> Parameters: 张三(String), 32(Integer), 男(String), 123@qq.com(String)
11:08:49:390 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - <== Total: 1
[Emp{eid=1, empName='null', age=32, sex='男', email='123@qq.com', dept=null}]
5. 引伸 : 其中一样传入信息不符合
5.1 and后的参数不提供
and后的参数是只, 如上图mapper映射文件中, age, sex, email
我们将age和email都设置成null, 在运行一次,看看效果。
List<Emp> emps = mapper.getEmpByCondition(new Emp(null, "张三", null, "男", null));
5.1.1运行效果
11:22:57:465 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource 434 - Created connection 769530879.
11:22:57:496 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==> Preparing: select * from t_emp where emp_name = ? and sex = ?
11:22:57:622 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==> Parameters: 张三(String), 男(String)
11:22:57:763 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - <== Total: 1
[Emp{eid=1, empName='null', age=32, sex='男', email='123@qq.com', dept=null}]
总结: and后的参数不提供, 不会影响sql运行,而且我们从LOG中可以看出,and后设置成null的参数,就不会参与sql运行。
5.2 where后的参数不提供
List<Emp> emps = mapper.getEmpByCondition(new Emp(null, null, 32, "男", "123@qq.com"));
5.2.1 运行效果
运行错误,我们可以从log里看出, where后跟的是and,那么sql语法肯定是错误的。
1:25:12:513 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource 434 - Created connection 769530879.
11:25:12:513 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==> Preparing: select * from t_emp where and age = ? and sex = ? and email = ?
11:25:12:670 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==> Parameters: 32(Integer), 男(String), 123@qq.com(String)org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'and age = 32and sex = '男'' at line 4
5.2.2 解决方法
在mapper映射文件中where语句后添加恒成立的条件 1=1, 映射文件如下。
<select id="getEmpByCondition" resultType="Emp">select * from t_emp where 1=1<if test="empName != null and empName != ''">emp_name = #{empName}</if><if test="age != null and age != ''">and age = #{age}</if><if test="sex != null and sex != ''">and sex = #{sex}</if><if test="email != null and email != ''">and email = #{email}</if></select>
运行结果: 成功运行且不报错。我们可以从LOG看出,恒成立条件1=1之后拼接and就不会报错。
11:32:01:649 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource 434 - Created connection 769530879.
11:32:01:649 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==> Preparing: select * from t_emp where 1=1 and age = ? and sex = ? and email = ?
11:32:01:727 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - ==> Parameters: 32(Integer), 男(String), 123@qq.com(String)
11:32:01:806 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 137 - <== Total: 1
[Emp{eid=1, empName='null', age=32, sex='男', email='123@qq.com', dept=null}]
6. 总结
恒成立1=1的作用:
1. where后跟着的参数是null的时候方便拼接sql
2.当参数都是null时候,sql也可以成功运行。
动态SQL中:
if 标签根据标签中test属性所对应的表达式决定标签中的内容是否需要拼接到sql中去
mybatis动态SQL多条件查询1 - if 标签相关推荐
- Mybatis 动态sql where 条件搜索以及In函数的使用
where 标签,里面有成立的条件,才进行where查询,自动添加或者过滤and, 逗号等 <select id="findUserByEntity" parameterTy ...
- c3p0 参数 模糊查询_mybatis之动态sql,模糊查询,结果集处理,mybatis分页及特殊字符处理...
目标及项目目录结构 目标 1.mybatis动态sql 2.模糊查询 3.查询返回结果集的处理 4.分页查询 5.特殊字符处理 项目的目录结构 1.mybatis动态sql If.trim.forea ...
- 一个mybatis动态 SQL查询的完整小案例。包含多表联合查询。
多表联合查询 一个根据机场查询航线的例子.有两张表,机场表包含机场信息:机场id.机场名字.机场城市. 航班包含航线信息:航班id.飞机编号.飞行时间.票价.起飞机场id.降落机场id. 需要查询的结 ...
- MyBatis动态SQL(认真看看, 以后写SQL就爽多了)
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 来源:cnblogs.com/homejim/p/9909657. ...
- 9.mybatis动态SQL标签的用法
mybatis动态SQL标签的用法 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦 ...
- 认真看看, 以后写 SQL 就爽多了:MyBatis 动态 SQL:
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 阿进的写字台 来源 | cnblogs.com ...
- MyBatis动态SQL,写SQL更爽
点击上方 好好学java ,选择 星标 公众号重磅资讯,干货,第一时间送达 今日推荐:分享一套基于SpringBoot和Vue的企业级中后台开源项目,这个项目有点哇塞!个人原创100W +访问量博客: ...
- 认真看看, 以后写 SQL 就爽多了:MyBatis 动态 SQL
点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达今日推荐:2020年7月程序员工资统计,平均14357元,又跌了,扎心个人原创100W+访问量博客:点击前往,查看更多 整理 ...
- MyBatis——动态SQL语句——if标签和where标签复合使用
功能需求 根据性别和名字查询用户 官方文档 MyBatis--动态 SQL SQL语句 SELECT id, username, birthday, sex, address FROM `user` ...
最新文章
- jieba分词的使用
- package.json中dependencies 与devDependencies 的区别
- 多线程:线程之间的协作(join、wait、notify、notifyAll、await、signal、signalAll)
- python自动化测试框架pytest.pdf_Python 自动化测试框架 unittest 和 pytest 对比
- 01)自学JavaScript
- 程序员工资倒挂,IT 行业薪资潜规则如何规避?
- python槽怎么用_【Python成长之路】从零学GUI -- 多窗口跳转(信号与槽函数用法)...
- 二叉树的先序,中序,后序,层次的递归及非递归遍历
- annotation-driven里面的argument-resolvers参数??
- 阿里云专家带你揭秘云计算数据底座——对象存储
- HTTP 1 1与HTTP 1 0的比较
- html 强制占据一行,html – 如何强制内联div保持在同一行?
- 3dmax如何拆分模型_3dmax怎么拆分模型画贴图
- html调用js函数取随机返回数值并自动显示在html页面
- npm修改全局安装默认路径(C:\Users\Administrator\AppData\Roaming\npm\node_modules)
- 常见的知识图谱(Wikidata、YAGO、ConceptNet、DBpedia)
- 被小伙伴要出来的文章[0]:英语学习
- setTimeout原理
- 节省你学习时间的百余个谷歌小技巧
- 02Linux常用的基本命令