SpringBoot中 Mybatis 的xml映射文件配置

目录

1.依赖

2.示例代码

2.1不带resultMap标签示例

2.1带resultMap标签示例

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


1.依赖

在pom.xml文件中引入依赖

      <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>3.0.3</version>
            <scope>test</scope>
        </dependency>

2.示例代码

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

2.1不带resultMap标签示例

dao接口:

package com.example.springbootdemo.Mapper;

import com.example.springbootdemo.pojo.Student;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface StudentMapper{

    @Insert("insert into td_student(username, password, name, gender, age,create_time, update_time) "+
    "values(#{username},#{password},#{name},#{gender},#{age},#{createTime},#{updateTime})")
    void insert(Student student);

    void delete(List<Integer> ids);

    void update(Student student);

    List<Student> list(String name, Short gender, LocalDate begin, LocalDate end);

    @Select("select * from td_student where  id = #{id}")
    Student getById(Integer id);

    @Select("select * from td_student where username=#{username}")
    Student getByUserName(String username);
}

对应的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.example.springbootdemo.Mapper.StudentMapper">
    <update id="update">
        update td_student
        <set>
            <if test="username != null and username != ''">
                username = #{username},
            </if>
            <if test="password != null and password != ''">
                password = #{password},
            </if>
            <if test="gender != null">
                gender = #{gender},
            </if>
            <if test="age != null">
                age = #{age},
            </if>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
        delete from td_student where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

    <select id="list" resultType="com.example.springbootdemo.pojo.Student">
            select * from td_student
            <where>
                <if test="name != null and name != ''">
                    name like concat('%',#{name},'%')
                </if>
                <if test="gender != null">
                    and gender = #{gender}
                </if>
                <if test="begin != null and end != null">
                    and entrydate = between #{begin} and #{end}
                </if>
            </where>
            order by update_time desc
    </select>

</mapper>

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

2.1带resultMap标签示例

<?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.example.dao.UserDao">  
  
    <!-- 定义User的结果映射 -->  
    <resultMap id="UserResultMap" type="com.example.model.User">  
        <id property="id" column="id" />  
        <result property="name" column="name" />  
        <result property="email" column="email" />  
    </resultMap>  
  
    <!-- 插入用户 -->  
    <insert id="insertUser" parameterType="com.example.model.User">  
        INSERT INTO user (name, email) VALUES (#{name}, #{email})  
    </insert>  
  
    <!-- 根据ID查询用户 -->  
    <select id="getUserById" parameterType="java.lang.Integer" resultType="com.example.model.User">  
        SELECT * FROM user WHERE id = #{id}  
    </select>  
  
    <!-- 查询所有用户 -->  
    <select id="getAllUsers" resultMap="UserResultMap">  
        SELECT * FROM user  
    </select>  
  
    <!-- 更新用户 -->  
    <update id="updateUser" parameterType="com.example.model.User">  
        UPDATE user SET name = #{name}, email = #{email} WHERE id = #{id}  
    </update>  
  
    <!-- 删除用户 -->  
    <delete id="deleteUser" parameterType="java.lang.Integer">  
        DELETE FROM user WHERE id = #{id}  
    </delete>  
  
</mapper>

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

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

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

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

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

  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值