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

Springboot Mybatis条件查询

        在Springboot中通过Mybatis实现条件查询

package com.wzb.ConditionSelectExercise20240923;import com.wzb.Pojo20240923.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.time.LocalDate;
import java.util.List;@Mapper
public interface ConditionEmpMapper {// 条件查询,在实际开发中,常常需要根据不同的条件查询员工的信息// 姓名:要求实现模糊匹配// 性别:要求实现精确匹配// 入职时间:要求进行范围查询// 根据最后修改时间进行排序// SQL语句/* select id, username, password, name, gender, image, job, entrydate, dept_id,create_time, update_timefrom empwhere name like '%张%'and gender = 1and entrydate between '2010-01-01' and '2020-01-01'order by update_time desc;*/// 接口方法实现// 1.
//    @Select("select * from emp " +
//            "where name like '%${name}%' " +   // 注意,使用通配符就不能直接使用#{}进行占位了
//            "and gender = #{gender} " +
//            "and entrydate between #{begin} and #{end} " +
//            "order by update_time desc")
//    public List<Emp> selectEmp(String name, short gender, LocalDate begin, LocalDate end);// 虽然上述方法看似能够实现业务逻辑,但用了${}占位符,会导致SQL注入的问题// 2.使用concat字符串拼接函数解决SQL注入风险@Select("select * from emp " +"where name like concat('%', #{name}, '%') " +   // 使用concat函数实现拼接,可以使用#{}"and gender = #{gender} " +"and entrydate between #{begin} and #{end} " +"order by update_time desc")public List<Emp> selectEmp(String name, short gender, LocalDate begin, LocalDate end);// 特别注意:保证接口中的@Select中的SQL语句的占位符参数名必须和方法中的形参名字相同,不然会出现找不到参数的错误// 在Springboot 1.X的版本,还需要用@Param注解来指定SQL语句中的参数名// 但是在Springboot 2.X以上的版本,只需要保证SQL中的参数和方法中的参数名相同即可// 因为Springboot父工程对compiler编译插件进行了默认的参数parameter配置,在编译时,会生成字节码文件中保留原方法// 形参的名字,所以说需要保证SQL语句中参数名字和方法形参名相同,才能保证#{}可以正确获得方法形参的值}

        Pojo类

package com.wzb.Pojo20240923;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.time.LocalDate;
import java.time.LocalDateTime;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {private Integer id;private String username;private String password;private String name;private Short gender;private String image;private Short job;private LocalDate entrydate;     //LocalDate类型对应数据表中的date类型private Integer deptId;private LocalDateTime createTime;//LocalDateTime类型对应数据表中的datetime类型private LocalDateTime updateTime;
}

 


http://www.mrgr.cn/news/34692.html

相关文章:

  • 基于 Amazon Bedrock +lambda函数调用大模型构建你的智能网页助手
  • 【已解决】用JAVA代码实现递归算法-从自然数中取3个数进行组合之递归算法-用递归算法找出 n(n>=3) 个自然数中取 3 个数的组合。
  • 匈牙利算法详解与实现
  • 如何使用GLib的单向链表GSList
  • 【leetcode】环形链表、最长公共前缀
  • 注册建造师执业工程规模标准(市政公用工程)
  • 计算机毕业设计Hadoop+PySpark深圳共享单车预测系统 PyHive 共享单车数据分析可视化大屏 共享单车爬虫 共享单车数据仓库 机器学习 深度学习
  • Linux 压缩制定目录下指定类型的多个文件
  • YOLO V10简单使用
  • 0-1开发自己的obsidian plugin DAY 1
  • C++的哲学思想
  • iOS 顶级神器,巨魔录音机更新2.1正式版
  • 一看就会!PS2024下载安装教程详解
  • 在 Java 中,你如何实现不可变对象?不可变对象有哪些好处?
  • 【Godot4.3】三角形类
  • JS的链判断符有几种写法,有哪些用法?
  • # 深度学习笔记(9)huggingface 构建数据集
  • kubernetes网络(三)之bird的路由反射器的使用
  • 大数据新视界 --大数据大厂之 Reactjs 在大数据应用开发中的优势与实践
  • npm、yarn、pnpm 最新国内镜像源设置和常见问题解决