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 注解
相关推荐
IT_陈寒1 天前
别再死记硬背Python语法了!这5个思维模式让你代码量减半
前端·人工智能·后端
老赵全栈实战1 天前
【每日一技MyBatis trim标签核心用法
java·mybatis·orm
xyy1231 天前
C# 读取 appsettings.json 配置指南
后端
code_YuJun1 天前
Spring ioc 完全注解
后端
kevinzeng1 天前
反射的初步理解
后端·面试
下次一定x1 天前
深度解析 Kratos 客户端服务发现与负载均衡:从 Dial 入口到 gRPC 全链路落地(上篇)
后端·go
kevinzeng1 天前
Spring 核心知识点:EnvironmentAware 接口详解
后端
xyy1231 天前
C# / ASP.NET Core 依赖注入 (DI) 核心知识点
后端
yuhaiqiang1 天前
为什么我建议你不要只问一个AI?🤫偷偷学会“群发”,答案准到离谱!
人工智能·后端·ai编程
双向331 天前
AR 眼镜拯救社恐:我用 Kotlin 写了个拜年提词器
后端