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_idcreate_timeupdate_time(下划线命名)
  • 实体类属性:deptIdcreateTimeupdateTime(驼峰命名)
  • 结果:默认情况下,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>
相关推荐
程序猿阿伟12 小时前
《一套完整方法论:搞定图形应用的Docker镜像优化》
数据库·docker·容器
二等饼干~za89866812 小时前
geo优化源码开发搭建技术分享
大数据·网络·数据库·人工智能·音视频
数据库小学妹13 小时前
HTAP混合负载架构:如何用一个数据库同时搞定交易和分析
数据库·经验分享·架构·dba
wuxinyan12313 小时前
工业级大模型学习之路029:解决双智能体调用数据库报错问题
数据库·人工智能·python·学习·智能体
Elastic 中国社区官方博客13 小时前
Elastic 线下 Meetup 将于 2026 年 7 月 26 号下午在深圳举行
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
YL2004042613 小时前
【Redis实战篇】秒杀实现方案(以优惠券秒杀为例)
数据库·redis
智研数智工坊13 小时前
SpringBoot4.0.6 + Security7.x + JWT 最新完整实战|无状态权限认证、统一异常处理、可直接落地
java·spring boot·spring security·jwt·权限认证
DIY源码阁13 小时前
JavaSwing宿舍管理系统 - MySQL版
java·数据库·mysql·eclipse
cfm_291413 小时前
MySQL8.0 InnoDB Cluster
数据库·mysql