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 注解
相关推荐
alwaysrun7 小时前
Rust之异步框架Tokio
后端·编程语言
Csvn7 小时前
日志系统
后端·python
CodeSheep7 小时前
中国编程第一人,一人抵一城!
前端·后端·程序员
一只IT攻城狮7 小时前
️ Spring Boot 文件上传,防御恶意文件攻击
java·spring boot·web安全
Randyliu7 小时前
20260511-Pydantic和SQLalchemy
后端·python
倒流时光三十年7 小时前
第6篇 Consumer 精讲(上):Offset 提交与幂等消费
spring boot·kafka
smallYoung7 小时前
【学习笔记】中间件-RabbitMQ
后端
Java成神之路-7 小时前
解析 MyBatis 中 #{} 与 ${}区别及 SQL 注入防范(附 Like/In/Order by 安全写法)
sql·安全·mybatis
三千星7 小时前
Java开发者转型AI工程化Week 3:从LangChain4j到AI Agent
后端·langchain
AI人工智能+电脑小能手7 小时前
【大白话说Java面试题 第45题】【JVM篇】第5题:JVM中,对象何时会进入老年代?
java·开发语言·jvm·后端·面试