• SpringBoot中 Mybatis 的xml映射文件配置


    目录

    1.依赖

    2.示例代码

    2.1不带resultMap标签示例

    2.1带resultMap标签示例

    3.resultMap标签不加的情况说明


    1.依赖

    在pom.xml文件中引入依赖

    1. <dependency>
    2. <groupId>org.mybatis.spring.boot</groupId>
    3. <artifactId>mybatis-spring-boot-starter-test</artifactId>
    4. <version>3.0.3</version>
    5. <scope>test</scope>
    6. </dependency>

    2.示例代码

    提示:在resource下创建文件mapper,用于存放xml配置文件

    2.1不带resultMap标签示例

    dao接口:

    1. package com.example.springbootdemo.Mapper;
    2. import com.example.springbootdemo.pojo.Student;
    3. import org.apache.ibatis.annotations.Insert;
    4. import org.apache.ibatis.annotations.Mapper;
    5. import org.apache.ibatis.annotations.Select;
    6. import java.time.LocalDate;
    7. import java.util.List;
    8. @Mapper
    9. public interface StudentMapper{
    10. @Insert("insert into td_student(username, password, name, gender, age,create_time, update_time) "+
    11. "values(#{username},#{password},#{name},#{gender},#{age},#{createTime},#{updateTime})")
    12. void insert(Student student);
    13. void delete(List ids);
    14. void update(Student student);
    15. List list(String name, Short gender, LocalDate begin, LocalDate end);
    16. @Select("select * from td_student where id = #{id}")
    17. Student getById(Integer id);
    18. @Select("select * from td_student where username=#{username}")
    19. Student getByUserName(String username);
    20. }

    对应的xml映射文件:

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.example.springbootdemo.Mapper.StudentMapper">
    6. <update id="update">
    7. update td_student
    8. <set>
    9. <if test="username != null and username != ''">
    10. username = #{username},
    11. </if>
    12. <if test="password != null and password != ''">
    13. password = #{password},
    14. </if>
    15. <if test="gender != null">
    16. gender = #{gender},
    17. </if>
    18. <if test="age != null">
    19. age = #{age},
    20. </if>
    21. <if test="name != null and name != ''">
    22. name = #{name},
    23. </if>
    24. <if test="updateTime != null">
    25. update_time = #{updateTime},
    26. </if>
    27. </set>
    28. where id = #{id}
    29. </update>
    30. <delete id="delete">
    31. delete from td_student where id in
    32. <foreach collection="ids" item="id" separator="," open="(" close=")">
    33. #{id}
    34. </foreach>
    35. </delete>
    36. <select id="list" resultType="com.example.springbootdemo.pojo.Student">
    37. select * from td_student
    38. <where>
    39. <if test="name != null and name != ''">
    40. name like concat('%',#{name},'%')
    41. </if>
    42. <if test="gender != null">
    43. and gender = #{gender}
    44. </if>
    45. <if test="begin != null and end != null">
    46. and entrydate = between #{begin} and #{end}
    47. </if>
    48. </where>
    49. order by update_time desc
    50. </select>
    51. </mapper>

    当然还有这种加resultMap标签的情况:

    2.1带resultMap标签示例

    1. "1.0" encoding="UTF-8" ?>
    2. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.example.dao.UserDao">
    5. <resultMap id="UserResultMap" type="com.example.model.User">
    6. <id property="id" column="id" />
    7. <result property="name" column="name" />
    8. <result property="email" column="email" />
    9. resultMap>
    10. <insert id="insertUser" parameterType="com.example.model.User">
    11. INSERT INTO user (name, email) VALUES (#{name}, #{email})
    12. insert>
    13. <select id="getUserById" parameterType="java.lang.Integer" resultType="com.example.model.User">
    14. SELECT * FROM user WHERE id = #{id}
    15. select>
    16. <select id="getAllUsers" resultMap="UserResultMap">
    17. SELECT * FROM user
    18. select>
    19. <update id="updateUser" parameterType="com.example.model.User">
    20. UPDATE user SET name = #{name}, email = #{email} WHERE id = #{id}
    21. update>
    22. <delete id="deleteUser" parameterType="java.lang.Integer">
    23. DELETE FROM user WHERE id = #{id}
    24. delete>
    25. mapper>

    3.resultMap标签不加的情况说明

    这取决于你的具体需求。 在 MyBatis 中主要用于处理复杂的映射情况,比如联合查询、嵌套结果等。对于简单的 CRUD 操作,如果不涉及复杂的映射关系,通常不需要显式定义 

    对于你的 User 实体类,如果它的属性名和数据库表中的列名完全一致,并且你不做特殊的映射处理(比如将数据库中的某个列映射到 Java 对象的另一个属性),那么你可以直接使用 resultType 来指定返回结果的类型,而不需要定义 

    resultType 告诉 MyBatis 返回的结果应该被映射到 com.example.model.User 类型的对象上。MyBatis 会根据 SQL 查询返回的列名自动匹配 User 类中的属性名,并将相应的值赋给属性。

    然而,如果你需要处理更复杂的场景,比如列名和属性名不匹配,或者需要映射到嵌套的对象,那么你就需要定义  来进行详细的映射配置。

  • 相关阅读:
    Java培训:现实世界中的原生Java
    c基础知识-数组(详解)
    我的云服务器初始化配置
    EIGRP & OSPF对比
    防火墙 (五十四)
    Spring事务管理和事务相关配置以及事务传播行为
    B. Square Filling
    学生python编程----飞机训练
    C# 自定义事件
    leetcode刷题笔记——单调栈
  • 原文地址:https://blog.csdn.net/qq_69183322/article/details/136331361