当前位置:   article > 正文

SpringBoot中 Mybatis 的xml映射文件配置

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<Integer> ids);
  14. void update(Student student);
  15. List<Student> 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. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE 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. <!-- 定义User的结果映射 -->
  6. <resultMap id="UserResultMap" type="com.example.model.User">
  7. <id property="id" column="id" />
  8. <result property="name" column="name" />
  9. <result property="email" column="email" />
  10. </resultMap>
  11. <!-- 插入用户 -->
  12. <insert id="insertUser" parameterType="com.example.model.User">
  13. INSERT INTO user (name, email) VALUES (#{name}, #{email})
  14. </insert>
  15. <!-- 根据ID查询用户 -->
  16. <select id="getUserById" parameterType="java.lang.Integer" resultType="com.example.model.User">
  17. SELECT * FROM user WHERE id = #{id}
  18. </select>
  19. <!-- 查询所有用户 -->
  20. <select id="getAllUsers" resultMap="UserResultMap">
  21. SELECT * FROM user
  22. </select>
  23. <!-- 更新用户 -->
  24. <update id="updateUser" parameterType="com.example.model.User">
  25. UPDATE user SET name = #{name}, email = #{email} WHERE id = #{id}
  26. </update>
  27. <!-- 删除用户 -->
  28. <delete id="deleteUser" parameterType="java.lang.Integer">
  29. DELETE FROM user WHERE id = #{id}
  30. </delete>
  31. </mapper>

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

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

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/181586
推荐阅读
相关标签
  

闽ICP备14008679号