当前位置: 首页 > news >正文

Mybatis 动态SQL(set)

我们先用XML的方式实现 : 把 id 为 13 的那一行的 username 改为 ip

创建一个接口 UserInfo2Mapper ,然后在接口中声明该方法

package com.example.mybatisdemo.mapper;
import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;
import java.util.List;@Mapper
public interface UserInfo2Mapper {Integer updateByCondition(UserInfo userInfo);
}

然后在resources 中创建 Userinfo2XMLMapper.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.example.mybatisdemo.mapper.UserInfo2Mapper"><update id="updateByCondition">update userinfoset<trim suffixOverrides=",">//删掉最后的逗号<if test="username!=null">username = #{username},</if><if test="age!=null">age = #{age},</if><if test="gender!=null">gender = #{gender}</if></trim>where id = 13</update>
</mapper>

然后我们回到接口 UserInfo2Mapper,右键,Generate,test,勾选 updateByCondition,ok

补充代码,

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;import static org.junit.jupiter.api.Assertions.*;@Slf4j
@SpringBootTest
class UserInfo2MapperTest {@Autowiredprivate UserInfo2Mapper userInfo2Mapper;@Testvoid updateByCondition() {UserInfo userInfo = new UserInfo();userInfo.setUsername("ip");//userInfo.setAge(23);//userInfo.setGender(0);userInfo2Mapper.updateByCondition(userInfo);}
}

运行成功

 打开数据库看,没毛病

Userinfo2XMLMapper.xml 里面的 trim 标签也可以化简,如下图,其他的跟上面一样,也是可以正常运行的

<?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.example.mybatisdemo.mapper.UserInfo2Mapper"><update id="updateByCondition">update userinfo<set><if test="username!=null">username = #{username},</if><if test="age!=null">age = #{age},</if><if test="gender!=null">gender = #{gender}</if></set>where id = 12</update>
</mapper>

http://www.lryc.cn/news/285836.html

相关文章:

  • Ubuntu18.04在线镜像仓库配置
  • 多数据源配置H2 Mysql
  • 【ASP.NET Core 基础知识】--路由和请求处理--路由概念(一)
  • 【Unity】RayMarching体积云理论学习
  • 物联网与智慧城市的无界未来:如何打破传统束缚,开启智能生活新篇章
  • nodejs下载安装
  • 从零学Java - Lambda表达式
  • RV1103与FPGA通过MIPI CSI-2实现视频传输,实现网络推流
  • 力扣62. 不同路径
  • 使用Element-Plus 加载style
  • Kafka常见指令及监控程序介绍
  • Docker 仓库管理
  • LeetCode-410.分割数组的最大值
  • Redis和RediSearch的安装及使用
  • 面向对象进阶--接口2
  • 提升认知,推荐15个面向开发者的中文播客
  • 数据分析-Pandas如何整合多张数据表
  • 配置redis挂载
  • C++ 实现游戏(例如MC)键位显示
  • 力扣hot100 合并两个有序链表 递归 双指针
  • 10个常用python自动化脚本
  • C++中函数的默认参数(缺省参数)
  • 在线扒站网PHP源码-在线扒站工具网站源码
  • vue+elementUI el-select 中 没有加clearable出现一个或者多个×清除图标问题
  • 【Python从入门到进阶】47、Scrapy Shell的了解与应用
  • 【ARM 嵌入式 编译系列 2.2 -- GCC 编译参数学习 assembler-with-cpp 使用介绍】
  • 深入理解java对象的内存布局
  • MetaGPT中提到的SOP
  • 第15届蓝桥杯嵌入式省赛准备第三天总结笔记(使用STM32cubeMX创建hal库工程+串口接收发送)
  • centos安装redis,但是启动redis-server /home/redis/conf/redis7000.conf卡住,怎么解决