springboot--06

一、根据 ID 查询

1. 基础 SQL 与接口

sql

复制代码
-- SQL语句
select * from emp where id = 19;

java

运行

复制代码
// Mapper接口方法
@Select("select * from emp where id = #{id}")
public Emp getById(Integer id);

2. 问题:字段与属性名不匹配

  • 数据库字段:dept_id、create_time、update_time(下划线命名)
  • 实体类属性:deptId、createTime、updateTime(驼峰命名)
  • 结果:默认情况下,MyBatis 无法自动封装这些不匹配的字段值。

二、数据封装的三种解决方案

方案 1:SQL 中给字段起别名

在查询语句中,将下划线字段起别名,使其与实体类属性名一致:

java

运行

复制代码
@Select("select id, username, password, name, gender, image, job, entrydate, " +
        "dept_id deptId, create_time createTime, update_time updateTime from emp where id = #{id}")
public Emp getById(Integer id);

✅ 优点:实现简单,无需额外配置❌ 缺点:每次查询都要手动写别名,代码冗余


方案 2:使用@Results和@Result注解手动映射

java

运行

复制代码
@Results({
    @Result(column = "dept_id", property = "deptId"),
    @Result(column = "create_time", property = "createTime"),
    @Result(column = "update_time", property = "updateTime")
})
@Select("select * from emp where id = #{id}")
public Emp getById(Integer id);

✅ 优点:一次配置,注解可复用,SQL 语句更简洁❌ 缺点:需要额外编写注解配置


方案 3:开启驼峰命名自动映射(推荐)

在application.properties中添加配置:

properties

复制代码
# 开启下划线到驼峰的自动映射:a_column → aColumn
mybatis.configuration.map-underscore-to-camel-case=true

✅ 优点:一劳永逸,无需修改 SQL 或添加注解,MyBatis 自动完成映射❌ 缺点:需要全局配置,适合项目中统一使用驼峰命名的场景


三、条件查询

1. 错误示例(存在 SQL 注入)

java

运行

复制代码
// 不推荐:使用${}拼接字符串,存在SQL注入风险
@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> list(String name, Short gender, LocalDate begin, LocalDate end);

2. 正确示例(使用预编译,安全高效)

java

运行

复制代码
// 推荐:使用concat函数拼接模糊查询,结合#{}参数传递
@Select("select * from emp where name like concat('%', #{name}, '%') and gender = #{gender} " +
        "and entrydate between #{begin} and #{end} order by update_time desc")
public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
  • 说明:concat('%', #{name}, '%') 可以安全实现模糊查询,同时利用预编译防止 SQL 注入。

四、XML 映射文件规范

当 SQL 语句较复杂时,推荐使用 XML 映射文件管理 SQL:

  1. 同包同名 :XML 文件名与 Mapper 接口名一致,且放在与 Mapper 接口相同的包下(如com.itheima.mapper.EmpMapper对应resources/com/itheima/mapper/EmpMapper.xml)
  2. namespace 绑定 :XML 的namespace属性值为 Mapper 接口的全限定名
  3. 方法绑定 :XML 中<select>标签的id属性值与 Mapper 接口方法名一致,resultType为返回结果的实体类全限定名

示例 XML:

xml

复制代码
<mapper namespace="com.itheima.mapper.EmpMapper">
    <select id="list" resultType="com.itheima.pojo.Emp">
        select * from emp where name like concat('%', #{name}, '%') and gender = #{gender}
        and entrydate between #{begin} and #{end} order by update_time desc
    </select>
</mapper>
相关推荐
IvorySQL9 分钟前
PostgreSQL 日报|恢复期 COPY TO 未日志表修复(10 月 21 日)
数据库·postgresql·区块链
码士集团小青32 分钟前
SpringBoot 集成缓存性能之王 Caffeine
spring boot·后端·缓存
贝猫说python1 小时前
大模型比赛2.3智能体开发(工具调用智能问答助手)
数据库·oracle
许彰午3 小时前
28-ORA-01110数据文件读取错误
数据库·oracle
凯凯姜3 小时前
MySQL 联合索引设计与索引失效
数据库·mysql
猫咪很可爱3 小时前
Oracle TEMP 表空间不足(ORA-01652)问题分析与优化实践
数据库·oracle
专业程序开发源4 小时前
springboot欧缇蔓月子中心管理系统82697-计算机课程设计、毕业设计
java·spring boot·后端·python·spring·django·课程设计
九零HTTP5 小时前
10月21号-CSDN配菜-IP池的5个坑
服务器·数据库·tcp/ip
莫逸风5 小时前
【毕设源码二期01】校园失物招领系统:从发布线索到核验交接
spring boot·毕业设计·课程设计·react
Data-Miner5 小时前
需要做财务数据分析,有什么好用的工具?从准备到出报告的实操指南
大数据·数据库·数据分析