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>
相关推荐
笨小孩@GF 知行合一6 小时前
易语言-高级应用
数据库·编程·易语言·中文编程
KaiwuDB6 小时前
KaiwuDB 运维实战04:DRBD + KaiwuDB——物联网场景下的低成本数据库高可用方案
运维·数据库·物联网·时序数据库·kaiwudb·aiot·多模数据库
Zhou1411367 小时前
SpringBoot_03_Web开发
前端·spring boot·python
夜雪一千8 小时前
MySQL外键彻底入门:是什么、怎么用、什么时候不该用
数据库·mysql
qq_334466868 小时前
删除SSMS的历史登录记录
数据库·sql
CarIise8 小时前
MyBatis动态SQL:从条件拼接到底层原理实战
mybatis
zl_dfq8 小时前
Redis 之 【常见面试题总结】(从数据类型到高可用、缓存一致性全解析)
数据库·redis
龙亘川8 小时前
铭记英烈守初心,数字强基促服务:亘川智城退役军人服务系统的实践与价值
java·数据库·人工智能
Zhou1411369 小时前
SpringBoot_02_自动配置原理
java·spring boot·后端