mybatis-210721-01—动态sql-if判断&where查询条件.md

  • if_判断

    • EmployeeMapper.java
    • EmployeeMapper.xml(笔记在这儿)
    • MybatisTest.javawhere
  • where查询条件
    • EmployeeMapper.xml(笔记在这儿)(where)(1=1)
    • EmployeeMapper.xml(笔记在这儿)(where标签)

if_判断

EmployeeMapper.java

package com.bgy.mybatis.dao;
import java.util.List;
import com.bgy.mybatis.bean.Employee;public interface EmployeeMapper {public List<Employee> getEmpsByConditionIf(Employee employee);
}

EmployeeMapper.xml(笔记在这儿)

<?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.bgy.mybatis.dao.EmployeeMapper"><!--ifchoose(when,otherwise)trim(where,set)foreach--><!--查询员工要求:携带了那个字段,查询条件就带上这个字段的值--><select id="getEmpsByConditionIf" resultType="com.bgy.mybatis.bean.Employee">select * fromtbl_employeewhere<!--test:判断表达式(OGNL)从参数中取值遇见特殊符号需要转义比如: " " ==(转义)==> &quot;&quot;--><if test="id!=null and id!=''">id=#{id}</if><if test="lastName!=null and lastName!=&quot;&quot;">and last_name like #{lastName}</if><if test="email!=null and email.trim()!=&quot;&quot;">and email like #{email}</if><!-- ognl会自动进行字符串和数字的转换判断   "0"==0 --><if test="gender==0 or gender==1">and gender=#{gender}</if></select>
</mapper>

MybatisTest.java

package com.bgy.mybatis.test;import java.io.IOException;
import java.io.InputStream;
import java.util.List;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;import com.bgy.mybatis.bean.Department;
import com.bgy.mybatis.bean.Employee;
import com.bgy.mybatis.dao.DepartmentMapper;
import com.bgy.mybatis.dao.EmployeeMapper;class MybatisTest {public SqlSessionFactory getSqlSessionFactory() throws IOException {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);return new SqlSessionFactoryBuilder().build(inputStream);}/*** 测试sql——if判断* @throws IOException*/@Testpublic void test() throws IOException{SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession = sqlSessionFactory.openSession();try {EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);Employee employee = new Employee(1,"","","1");List<Employee> emps = mapper.getEmpsByConditionIf(employee);System.out.println(emps);} finally {sqlSession.close();}}
}


where查询条件

EmployeeMapper.xml(笔记在这儿)(where)(1=1)

<?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.bgy.mybatis.dao.EmployeeMapper"><!--ifchoose(when,otherwise)trim(where,set)foreach--><!--查询员工要求:携带了那个字段,查询条件就带上这个字段的值--><select id="getEmpsByConditionIf" resultType="com.bgy.mybatis.bean.Employee">select * fromtbl_employeewhere 1=1<!--查询的时候如果某些条件没带sql,拼装可能有问题解决:1、给where后面加上 1=1 ,以后的条件都是    and xxx2、mybatis使用where标签来将所有的查询条件包括在内。--><if test="id!=null and id!=''">and id=#{id}</if><if test="lastName!=null and lastName!=&quot;&quot;">and last_name like #{lastName}</if><if test="email!=null and email.trim()!=&quot;&quot;">and email like #{email}</if><!-- ognl会自动进行字符串和数字的转换判断  "0"==0 --><if test="gender==0 or gender==1">and gender=#{gender}</if></select>
</mapper>

EmployeeMapper.xml(笔记在这儿)(where标签)

<?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.bgy.mybatis.dao.EmployeeMapper"><!--ifchoose(when,otherwise)trim(where,set)foreach--><!--查询员工要求:携带了那个字段,查询条件就带上这个字段的值--><select id="getEmpsByConditionIf" resultType="com.bgy.mybatis.bean.Employee">select * fromtbl_employee<!--查询的时候如果某些条件没带sql,拼装可能有问题解决:1、给where后面加上 1=1 ,以后的条件都是    and xxx2、mybatis使用where标签来将所有的查询条件包括在内。mybatis就会将where标签中拼装的sql,将多出来的and或or去掉。where只能去掉第一个多出来的and或or。--><where><if test="id!=null and id!=''">id=#{id}</if><if test="lastName!=null and lastName!=&quot;&quot;">and last_name like #{lastName}</if><if test="email!=null and email.trim()!=&quot;&quot;">and email like #{email}</if><!-- ognl会自动进行字符串和数字的转换判断  "0"==0 --><if test="gender==0 or gender==1">and gender=#{gender}</if></where></select>
</mapper>

mybatis-210721-01---动态sql-if判断where查询条件相关推荐

  1. Mybatis映射文件动态SQL语句-01

    因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...

  2. 6.Mybatis中的动态Sql和Sql片段(Mybatis的一个核心)

    视频地址:http://edu.51cto.com/sd/be679 动态Sql是Mybatis的核心,就是对我们的sql语句进行灵活的操作,他可以通过表达式,对sql语句进行判断,然后对其进行灵活的 ...

  3. Mybatis 注解开发 + 动态SQL

    Hello 大家好我是橙子同学,今天分享注解Mybatis注解开发+动态sql 目录 每文一铺垫(今天有小插曲哦) 注解开发 添加 @Insert 删除 @Delete 查询 @Select 修改 @ ...

  4. Mybatis入门之动态sql

    Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...

  5. Java - MyBatis中的动态SQL是什么意思?

    分享一个大牛的人工智能教程.零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 对于一些复杂的查询,我们可能会指定多个查询条件,但是 ...

  6. mybatis注解开发动态sql

    mybatis注解开发动态sql 本篇来讲一下如何使用mybatis注解模式中的动态sql 先来讲一下什么是动态sql 在我们实际开发的时候可能会出现很多方法需要一条很相似的sql语句来进行增删改查, ...

  7. Mybatis中的动态SQL,一对一,一对多以及标签

    文章目录 动态SQL中的结果集映射 一对一 一对多 where标签 where..if...标签(作用和Java中的if一样,只要满足if条件的都可以拼接) where..choose..标签(作用和 ...

  8. MyBatis自定义自定义动态SQL解析方式

    引子 之前项目中一直使用的是JPA作为ORM框架,最近,新加了一个子服务,使用的是 MyBatis 作为ORM框架.既然用的是MyBatis,那就免不了 循环迭代参数.if 动态SQL查询等. 然而, ...

  9. Mybatis常用的动态SQL标签讲解

    先让我们看看mybatis常用动态SQL的标签有哪一些 如果mybatis和SSM整合环境不会搭建请看这篇 https://blog.csdn.net/weixin_44012722/article/ ...

最新文章

  1. Linux sed替换内容中有空格解决办法
  2. 170405、java版MD5工具类
  3. python3 打印_Python 3 进阶 —— print 打印和输出
  4. H.264视频编解码的代码移植和优化
  5. C语言转义字符的使用
  6. java黄历_黄历查询API免费接口,黄历查询API接口付费定制-进制数据
  7. 评分 - 2019寒假训练营第一次作业
  8. 帆软决策报表全页面刷新或局部组件定时刷新
  9. 从AR光学开始,了解AR眼镜
  10. OA软件详细功能模块列表
  11. JAVA对接圆通API
  12. mindoc升级总结
  13. 计算两条线或多边形的交点(LineString | MultiLineString | Polygon )
  14. java逻辑思维练习
  15. 如何将txt文件用shell拆分成多个?
  16. 已解决,软件V2报错 failed to read response header > websocket: close 1005 (no status)问题
  17. 计算机与软件工程-研究生复试-专业面试题型举例
  18. 如何在jsp里面写java代码
  19. Pr 入门教程如何个性化“时间轴”面板?
  20. Gazebo使用笔记(6) —— 摩擦力特性与碰撞位掩码

热门文章

  1. 设计灵感|APP注册登录页面的设计形式
  2. 技术沙龙|京东云端到端多媒体关键技术揭秘
  3. 结合C++,网页实现消息即时提醒(桌面右下角弹窗)
  4. [系统安全] 十六.PE文件逆向基础知识(PE解析、PE编辑工具和PE修改)
  5. java文本框添加单击事件_Java文本框上的ActionEvent事件
  6. 郭德纲的279个经典包袱
  7. 用python祝福父亲节_父亲节祝福语 父亲节最感恩温馨的问候语录
  8. python字符串下标从0开始还是1_python字符串下标与切片及使用方法
  9. HAProxy快速入门(一)——简介及原理
  10. Unity 调用IOS的StoreKit实现在游戏内部的对游戏进行星级评价和评论