Mybatis使用注意

1.Mapper层注解

在Mapper/Dao层需要使用注解@Mapper

请输入图片描述

或者在Mapper/Dao层使用注解@Repository然后在Application(启动文件)中加入注解@MapperScan("com.yenx.demo.Mapper")

请输入图片描述请输入图片描述

2.Mapper映射文件格式

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.yenx.demo.Mapper.UserMapper">
    <select id="getByName" resultType="com.yenx.demo.Entity.User">
            select * from users where username = #{username}
    </select>
</mapper>

3.Mapper映射文件位置配置

需要在application.properties/application.yml中配置Mapper映射文件的位置

Application.properties格式

mybatis.mapper-locations=classpath:Mapper/*.xml

Application.yml格式

mybatis:
  mapper-locations:
    classpath: Mapper/*.xml
最后修改:2025 年 01 月 24 日
如果觉得我的文章对你有用,请随意赞赏