关于Mybatis中MapperProxy的深入

本文主要探讨Mybatis中的MapperProxy,通过分析关键代码,理解MapperProxy在映射过程中的作用。首先介绍如何建立简单SSM项目并完成插入操作。接着,详细剖析MapperProxy类,包括其泛型、常量定义、sqlSession、mapperInterface和methodCache的作用。随后,通过源码分析,讲解MapperProxy的invoke方法执行流程,包括判断默认方法、缓存MapperMethod和执行MapperMethod.execute方法的过程,揭示MapperProxy如何连接Mapper接口和SQL执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了方便研读,先搭建一个简单的SSM项目(本文不做赘述),然后完成一套流程,下面已增加insert为例。

1. 先给出关键代码

public class PersonDto {

    private Long id;

    private String name;
   
   ......... 构造、get、set
}

为了方便演示,先建一个简单的实体类

@Mapper
public interface PersonMapper {

    int insertPerson(PersonDto personDto) throws Exception;
}
<?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.balls.mapper.PersonMapper">

    <insert id="insertPerson" parameterType="com.balls.dto.PersonDto">
        INSERT
        INTO PERSON(ID, NAME)
        VALUES(#{id}, #{name})
    </insert>

</mapper>

编写mapper接口和对应的xml

@RestController
@RequestMapping(value = "/personController")
public class PersonController {

    @Resource
    private PersonMapper personMapper;

    @RequestMapping(value = "/{id}/{name}/insert.do", method = RequestMethod.GET)
    public int gets(@PathVariable Long id, @PathVariable String name) throws Exception {
        int result = 0;
        result = personMapper.insertPerson(new PersonDto(id, "white"));
        return result;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值