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 注解
相关推荐
FreeCultureBoy10 小时前
用 jenv 管理 Java 环境:从安装 JDK 到多版本切换
后端
IT_陈寒10 小时前
Vite的热更新突然失效,原来是因为这个配置
前端·人工智能·后端
考虑考虑10 小时前
SQL语句中的order by可能造成时间重复
数据库·后端·mysql
Pkmer10 小时前
古法编程: 代理模式
后端·设计模式
文心快码BaiduComate11 小时前
Comate搭载Kimi K2.6,长程13h!
前端·后端·程序员
Pkmer11 小时前
古法编程: 责任链模式
后端·设计模式
KevinSheeran11 小时前
Rust高级代码题 - 手写一个 LRU Cache
后端
Java女侠_9年实战12 小时前
JVM调优“瞎调”——没分析GC日志,乱改堆内存参数导致OOM
后端
做个文艺程序员12 小时前
流式输出(SSE)在 Spring Boot 中的实现【OpenClAW + Spring Boot 系列 第3篇】
java·spring boot·后端
你有医保你先上12 小时前
Elasticsearch Go 客户端
后端·elasticsearch·go