Springboot整合mybatis配置文件

1.创建 MyBatis 的配置文件(sqlMapConfig.xml)
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
" http://mybatis.org/dtd/mybatis-3-config.dtd">
注意:Spring Boot 下 大部分配置已经被 starter 接管,这个文件更多是占位 + 扩展用。
2.创建PersonMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.qcby.mapper.PersonMapper"> <!-- 查询全部 --> <select id="getPersons" resultType="Person"> select * from tx_person </select> <!-- 根据 id 查询 --> <select id="getPersonById" parameterType="int" resultType="Person"> select * from tx_person where pid = #{id} </select> <!-- 插入 --> <insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="pid"> insert into tx_person(pid, pname, addr, gender, birth) values (#{pid}, #{pname}, #{addr}, #{gender}, #{birth}) </insert> <!-- 删除 --> <delete id="delete" parameterType="int"> delete from tx_person where pid = #{id} </delete> </mapper>
3.创建mapper接口
List<Person> getPersons();
4.yml加
mybatis: config-location: classpath:mybatis/sqlMapConfig.xml mapper-locations: classpath:mybatis/mapper/*.xml type-aliases-package: com.qcby.model
这三行分别干什么:

  • config-location:MyBatis 全局配置文件
  • mapper-locations:XML 映射文件位置
  • type-aliases-package:实体类包(Person → Person)
    5,启动原来的测试类
    打印的东西一样
    配置文件方式改回纯注解方式
    1.删掉 yml 里的 mapper-locations.这样XML 就不会参与
    2.Mapper 接口上写 SQL 注解
相关推荐
逝水无殇2 小时前
C# 异常处理详解
开发语言·后端·c#
考虑考虑4 小时前
Sentinel安装
java·后端·微服务
IT_陈寒4 小时前
SpringBoot自动配置失灵?你可能忘了这个关键注解
前端·人工智能·后端
碎碎念_4924 小时前
SpringBoot + Vue 前后端分离从 0 到 1 完整环境配置流程
vue.js·spring boot·后端
逝水无殇4 小时前
C# 文件的输入与输出详解
开发语言·数据库·后端·c#
YIAN5 小时前
从手写 Agent 工具到 MCP 协议:一文搞懂大模型跨进程跨语言工具调用
后端·agent·mcp
小兔崽子去哪了5 小时前
Docker 删除镜像后磁盘空间没有释放?
后端·docker·容器
高明珠6 小时前
麒麟 V10 SP1 排查实录:ttyS5 每 10 秒莫名收到一批报文,元凶是 eGTouchD
后端
卷无止境6 小时前
Python FFI 技术深度解析:ctypes、cffi 与 pybind11 的性能差异与实践挑战
后端·python
ServBay6 小时前
AI Gateway 是什么?为什么每个平台都在做
后端·aigc·ai编程