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 注解
相关推荐
菜鸟谢7 小时前
Rust 枚举 (enum) 完整核心知识点
后端
晓杰在写后端8 小时前
从0到1实现Balatro游戏后端(9):Blind奖励结算与金币系统实现
后端·游戏开发
Patrick_Wilson8 小时前
幂等到底是什么?从前端视角讲透 SQL、HTTP 与 POST 接口的幂等设计
前端·后端·架构
凌览8 小时前
一人公司别再上 Jenkins,真不值
前端·后端
菜鸟谢8 小时前
Rust 元组与数组内存管理笔记
后端
oil欧哟8 小时前
Codex 最佳实践(超级长文):先搞懂 AI,再用好 AI
前端·人工智能·后端
AskHarries8 小时前
把一个外部系统接成 MCP 工具
后端·程序员
释然小师弟8 小时前
Android开发十年:反思与回顾
android·后端·嵌入式
用户3169353811838 小时前
如何从零编写一个 Spring Boot Starter
spring boot
雪隐8 小时前
个人电脑玩AI-04让5060 Ti给你打工——本地FLUX.2 Klein 的 AI 图片生成
人工智能·后端