Mybatis之常用动态Sql语句

devtools/2025/2/6 19:57:19/

数据库结构、实体类

public class Youth {private Integer id;private String username;private Date birthday;private Character sex;private String address;private Integer age;public Youth(Integer id, String username, Date birthday, Character sex, String address) {this.id = id;this.username = username;this.birthday = birthday;this.sex = sex;this.address = address;}public Youth() {}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public Character getSex() {return sex;}public void setSex(Character sex) {this.sex = sex;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Youth{" +"id=" + id +", username='" + username + '\'' +", birthday=" + birthday +", sex=" + sex +", address='" + address + '\'' +", age=" + age +'}';}
}

where-if

where-if多用于查询含有某个特征语句上  如果 where标签内的条件成立 它会自动去掉条件语句开头多余的 and 或者 or

它会通过判断是否符合test中的属性来进行选择性执行 会将满足此条件的内置语句进行嵌入到sql语句中进行执行

在接口文件中写入

List<Youth> getYouthBywhereif(Youth youth);

在映射文件中写入

<select id="getYouthBywhereif" resultType="youth">select * from youth<where><if test=" username != null and username !='' ">and username = #{username}</if><if test="birthday != null">and birthday = #{birthday}</if><if test="sex!=null and sex!=''"  >and sex= #{sex}</if><if test="address!=null and address!=''">and address = #{address}</if><if test="age!=null">and age = #{age}</if></where></select>

测试类中写入

@Testpublic void getYouthBywhereifTest(){Youth youth = new Youth();youth.setSex('男');youth.setAddress("北京");List<Youth> youths = youthMapper.getYouthBywhereif(youth);for(Youth y:youths){System.out.println(y);}}

执行结果为

set-if

set-if多用于选择性修改操作上 它会自动去掉条件语句结尾多余的 ,

在映射文件中写入

<update id="updateYouthBysetif" parameterType="youth">update youth<set><if test="username!=null and username!=''">username = #{username},</if><if test="birthday!=null">birthday =#{birthday},</if><if test="sex!=null and sex!=''">sex = #{sex},</if><if test="address!=null and address!=''">address=#{address},</if><if test="age!=null">age = #{age}</if></set><where>id=#{id}</where></update>

在测试类中写入

@Testpublic void updateYouthBysetifTest(){Youth youth = new Youth(4,"张三", new Date(2019 - 1900,10 - 1,9),'男',"北京");int i = youthMapper.updateYouthBysetif(youth);if(i>0)System.out.println("修改成功");elseSystem.out.println("修改失败");}

执行结果为

特别须知

  1. 在test中非字符串类型 不可使用 属性 !=  '  '
  2. 建议将实体类的属性使用包装类进行修饰 因为非包装类不能使用 类似于age != null 的格式


http://www.ppmy.cn/devtools/156603.html

相关文章

深入核心:一步步手撕Tomcat搭建自己的Web服务器

介绍&#xff1a; servlet&#xff1a;处理 http 请求 tomcat&#xff1a;服务器 Servlet servlet 接口&#xff1a; 定义 Servlet 声明周期初始化&#xff1a;init服务&#xff1a;service销毁&#xff1a;destory 继承链&#xff1a; Tomcat Tomcat 和 servlet 原理&#x…

C++的 I/O 流

本文把复杂的基类和派生类的作用和关系捋出来&#xff0c;具体的接口请参考相关文档 C的 I/O 流相关的类&#xff0c;继承关系如下图所示 https://zh.cppreference.com/w/cpp/io I / O 的概念&#xff1a;内存和外设进行数据交互称为 I / O &#xff0c;例如&#xff1a;把数…

设计模式 - 行为模式_Template Method Pattern模板方法模式在数据处理中的应用

文章目录 概述1. 核心思想2. 结构3. 示例代码4. 优点5. 缺点6. 适用场景7. 案例&#xff1a;模板方法模式在数据处理中的应用案例背景UML搭建抽象基类 - 数据处理的 “总指挥”子类定制 - 适配不同供应商供应商 A 的数据处理器供应商 B 的数据处理器 在业务代码中整合运用 8. 总…

Tomcat启动流程与前端请求处理详解

Tomcat启动流程与前端请求处理详解 一、Tomcat启动流程 Tomcat是一个Servlet容器&#xff0c;它负责将Web应用程序中的Servlet与外部HTTP请求进行交互。Tomcat启动时会加载所有的Web应用和Servlet&#xff0c;并通过扫描和反射机制将其映射到适当的Servlet类上。下面是Tomcat启…

gitlab云服务器配置

目录 1、关闭防火墙 2、安装gitlab 3、修改配置 4、查看版本 GitLab终端常用命令 5、访问 1、关闭防火墙 firewall-cmd --state 检查防火墙状态 systemctl stop firewalld.service 停止防火墙 2、安装gitlab xftp中导入安装包 [rootgitlab ~]#mkdir -p /service/tool…

MySQL知识点总结(十七)

在从属服务器上执行RESET SLAVE命令时&#xff0c;会发生哪些操作&#xff1f; RESET SLAVE命令会断开从属服务器与主服务器的连接&#xff0c;以重置从属服务器&#xff0c;具体效果如下&#xff1a;清除 master.info和relay.log资料档案库删除所有中继日志启动新的中继日志文…

pytorch实现门控循环单元 (GRU)

人工智能例子汇总&#xff1a;AI常见的算法和例子-CSDN博客 特性GRULSTM计算效率更快&#xff0c;参数更少相对较慢&#xff0c;参数更多结构复杂度只有两个门&#xff08;更新门和重置门&#xff09;三个门&#xff08;输入门、遗忘门、输出门&#xff09;处理长时依赖一般适…

Java 大视界 -- 深度洞察 Java 大数据安全多方计算的前沿趋势与应用革新(52)

&#x1f496;&#x1f496;&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎你们来到 青云交的博客&#xff01;能与你们在此邂逅&#xff0c;我满心欢喜&#xff0c;深感无比荣幸。在这个瞬息万变的时代&#xff0c;我们每个人都在苦苦追寻一处能让心灵安然栖息的港湾。而 我的…