mybatis动态标签——where、if
2024-10-21 04:16:20
mapper接口
public interface EmpMapper { List<Emp> getEmp(Emp emp);
}
mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.dynamicSQL.mapper.EmpMapper"> <!--
if标签:通过test属性中的表达式判断标签中的内容是否有效(是否会拼接到sql中)
where标签:1.若where标签中有条件成立,会自动生成where关键字
2.会自动将where标签中内容前多余的and去掉,但是其中内容后多余的and无法去掉
3.若where标签中没有任何一个条件成立,则where也不会自动生成
-->
<select id="getEmp" resultType="emp">
select * from t_emp
<where>
<if test="empName != null and empName != ''">
and emp_name = #{empName}
</if>
<if test="age != null and age != ''">
and age = #{age}
</if>
<if test="gender != null and gender != ''">
and gender = #{gender}
</if>
</where>
</select>
</mapper>
测试代码
@Test
public void test1(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp emp = new Emp(null,"李四",null,"男");
List<Emp> list = mapper.getEmp(emp); list.forEach(System.out::println);
}
最新文章
- 使用MapReduce实现join操作
- my_mosaic
- redis 学习笔记(5)-Spring与Jedis的集成
- WINCE+6410 拨号上网
- SharePoint2013 SharePoint-Hosted 模式 分页方法
- source和.命令的区别
- eclipse import的项目报autowired cannot be resolved to a type的错误
- phpstorm配置Xdebug进行调试PHP教程
- UINavigationController和UITabBarController合用
- Delphi总结使用TStrings的一些技巧
- 使用tortoisegit工具git地址中带号码密码的拉取,以及使用这种方式后中途重置密码报git remote: HTTP Basic: Access denied 错误解决办法
- java8_api_字符串处理
- php过滤&;nbsp;字符
- tkinter之grid布局管理器详解
- json对象转数组
- PythonStudy——字符编码 Character Encoding
- Django REST framework 源码剖析
- 多线程 Runnable 实现线程 内部类
- BZOJ 2434 阿狸的打字机(fail树)
- ubuntu 安装php 报错解决
热门文章
- 第三篇:前端基础之JavaScript
- 带你读AI论文丨针对文字识别的多模态半监督方法
- python 学生管理系统 文件版 增删改查
- eclipse启动一个Springboot项目
- 在 C# 9 中使用 foreach 扩展
- [剑指Offer]3.数组中重复的数字
- 交叉编译esp8089
- excel文件 实现自动处理数据的功能
- 微机原理与系统设计笔记2 | 8086CPU结构与功能
- angular11报错Can&#39;t bind to &#39;ngForOf&#39; since it isn&#39;t a known property of &#39;tr&#39;. 三种排查办法以及解决方案