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 小时前
Maven多模块开发:高效构建复杂项目
java·开发语言·spring boot·maven
手握风云-2 小时前
JavaEE 进阶第十九期:MyBatis-Plus,让 CRUD 飞起来
java·java-ee·mybatis
UIUV2 小时前
构建Git AI提交助手:从零到全栈实现的学习笔记
前端·后端·typescript
小灵不想卷2 小时前
LangChain4j 与 SpringBoot 整合
java·后端·langchain4j
undefinedType2 小时前
rails知识扫盲
数据库·后端·敏捷开发
小灵吖2 小时前
LangChain4j 的 Low 和 High level API
后端
砍材农夫2 小时前
强应用-弱引用-虚引用-软引用
后端
毅炼2 小时前
Java 基础常见问题总结(5)
java·后端
前路不黑暗@2 小时前
Java项目:Java脚手架项目的通用组件的封装(七)
java·开发语言·spring boot·后端·学习·spring cloud·maven