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

Mybatis——动态sql

if标签

用于判断条件是否成立。使用test属性进行条件判断,如果条件为true,则拼接sql。

<where>标签用于识别语句是否需要连接词and,识别sql语句。

package com.t0.maybatisc.mapper;import com.t0.maybatisc.pojo.Emp;
import org.apache.ibatis.annotations.*;import java.time.LocalDate;
import java.util.List;@Mapper
public interface EmpMapper {
//    条件查询public List<Emp> list( String name,  Short gender,  LocalDate begin,  LocalDate end);}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.t0.maybatisc.mapper.EmpMapper"><select id="list" resultType="com.t0.maybatisc.pojo.Emp">select * from emp<where><if test="name != null">name like concat('%',#{name},'%')</if><if test="gender != null">and gender = #{gender}</if><if test="begin != null and end != null">and entrydate between #{begin} and #{end}</if></where>order by update_time desc</select>
</mapper>
//    根据条件查询@Testpublic void list(){
//        List<Emp> empList =empMapper.list("张",(short) 1,LocalDate.of(2010,01,01),LocalDate.of(2020,01,01));List<Emp> empList =empMapper.list(null,(short)1,null,null);System.out.println(empList);}
}

用于select、upgrade。

foreach标签

package com.t0.maybatisc.mapper;import com.t0.maybatisc.pojo.Emp;
import org.apache.ibatis.annotations.*;import java.time.LocalDate;
import java.util.List;@Mapper
public interface EmpMapper {
//    多条删除public  void deleteByids(List<Integer> ids);}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.t0.maybatisc.mapper.EmpMapper"><select id="deleteByids" resultType="com.t0.maybatisc.pojo.Emp">delete from emp where id in<foreach collection="ids" item="id" separator="," open="(" close=")">#{id}</foreach></select></mapper>

collection="ids" item="id" separator="," open="(" close=")"

    @Testpublic void TestdeleteByids(){List<Integer> ids = Arrays.asList(19,18,16);empMapper.deleteByids(ids);}

sql和include

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.t0.maybatisc.mapper.EmpMapper"><sql id="commonS">select id,username,password,name,gender,image,job,entrydate,dept_id,create_time,update_timefrom emp</sql><select id="list" resultType="com.t0.maybatisc.pojo.Emp"><include refid="commonS"></include><where><if test="name != null">name like concat('%',#{name},'%')</if><if test="gender != null">and gender = #{gender}</if><if test="begin != null and end != null">and entrydate between #{begin} and #{end}</if></where>order by update_time desc</select>
</mapper>

 

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

相关文章:

  • 可视化大屏开发系列——页面布局
  • Python statistics 模块
  • wireshark常见使用表达式
  • 用Java获取键盘输入数的个十百位数
  • 第10章 启动过程组 (制定项目章程)
  • html侧导航栏客服栏
  • Clonable接口和拷贝
  • 关于小蛋の编程和小蛋编程为同一作者的说明
  • 大数据平台之Spark
  • How to use ModelSim
  • 【专业英语 复习】第8章 Communications and Networks
  • 运行vue3项目相关报错
  • 2024年6月计算机视觉论文推荐:扩散模型、视觉语言模型、视频生成等
  • Centos Stream9 和Centos Stream10的下载网址
  • chrome 录制器及性能分析工具的使用
  • 如何打造稳定、好用的 Android LayoutInspector?
  • C++ Thead互斥量死锁,mutex如何防止死锁---C++11多线程快速学习
  • Ubuntu 之Glade图形化设计器
  • 152. 乘积最大子数组
  • proactor模式
  • Charles抓包工具
  • RabbitMQ如何保证消息可靠
  • 学习笔记——路由网络基础——路由的高级特性
  • 网络编程之XDP、TC和IO_URING以及DPDK
  • 晶谷高温烧结导电浆料用低熔点玻璃粉 晶谷耐高温导电漆导电油墨高温玻璃粉
  • 【Mysql】DQL操作单表、创建数据库、排序、聚合函数、分组、limit关键字
  • Excel 常用技巧(四)
  • 【Linux 基础】文件与目录管理
  • C++系列-String(一)
  • 服务器硬件的基础知识