6.动态sql - if
2024-10-19 23:35:42
满足条件的数据
mapper.xml 满足if条件就执行,不满足就不加
<select id="selectStateByTitle" parameterType="string" resultMap="blogResultMap">
select * from blog
where state = "1"
<if test="value != null and value != ''">
and title like '%${value}%'
</if>
</select>
接口:mapper
List<Blog> selectStateByTitle(String title);
测试 test 可以写满足的条件也可以不写
@Test
public void testSelectStateByTitle(){
SqlSession session = MybatisUtil.getSqlSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.class);
List<Blog> blogList = blogMapper.selectStateByTitle(null);
session.close();
System.out.println("<<<<<------ 查询------>>>>>:"+blogList);
}
实际应用在代码中
<select id="findList" resultType="Periodical"> SELECT <include refid="periodicalColumns"/> FROM periodical a <include refid="periodicalJoins"/> <where> a.del_flag = #{DEL_FLAG_NORMAL} <if test="type != null and type != ''"> AND type = #{type} </if> <if test="name != null and name != ''"> AND a.name LIKE <if test="dbName == 'oracle'">'%'||#{name}||'%'</if> <if test="dbName == 'mssql'">'%'+#{name}+'%'</if> <if test="dbName == 'mysql'">concat('%',#{name},'%')</if> </if> </where> <choose> <when test="page !=null and page.orderBy != null and page.orderBy != ''"> ORDER BY ${page.orderBy} </when> <otherwise> ORDER BY a.update_date DESC </otherwise> </choose> </select>
最新文章
- 【先定一个小目标】Windows下Redis的安装使用
- [WPF系列] window自定义
- java变量的加载顺序
- 解决 HttpClient 模拟 http 的get 请求后 ,出现 403 错误
- Lucas
- u盘在电脑读不出来,但别的可以读,别的u盘在我电脑又可以识别怎么回事?
- 反转(开关问题) POJ 3276
- MsSql省市联动表
- Java访问kafka的时候java.nio.channels.ClosedChannelException解决办法
- Unique Binary Search Trees II 解答
- docker exec 运行命令
- hdu1260(dp)
- robot framework -重点记录
- [Swift]LeetCode373. 查找和最小的K对数字 | Find K Pairs with Smallest Sums
- android的listview以及画线--to thi tha
- aop技术理解使用
- 用360清理了一下电脑后发现Eclipse软件无法打开
- YARN集群的mapreduce测试(四)
- h.264并行熵解码
- 『转』MySQL存储过程语法例子
热门文章
- .Net 使用 GregorianCalendar 获取任意一天为一年的第几周
- ConfigureAwait(false)
- xcode编译静态库选择cpu架构
- java连接字符串操作,可用来向sql传值
- hadoop再次集群搭建(4)-Cloudera Manager Installation
- 关于handler的再次讨论
- [codevs1159]最大全0子矩阵(悬线法)
- NSSelectorFromString 使用示例
- Log4Net 调试时输出sql到 视图->;输出的sql语句
- hadoop运行故障问题解决1——datanode节点启动后自动关闭