pojo.MappedStatement类示例代码

Java ORM框架:MappedStatement详解
MappedStatement是Java中MyBatis框架的核心组件之一,它用于封装SQL语句及其相关元数据。该类包含了id(唯一标识)、resultType(返回值类型)、paramterType(参数值类型)和sql(SQL语句)等属性,用于构建动态SQL并执行数据库操作。理解MappedStatement对于优化MyBatis的使用和提升数据库交互效率至关重要。
package com.lagou.pojo;

public class MappedStatement {

    //id标识
    private String id;
    //返回值类型
    private String resultType;
    //参数值类型
    private String paramterType;
    //sql语句
    private String sql;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getResultType() {
        return resultType;
    }

    public void setResultType(String resultType) {
        this.resultType = resultType;
    }

    public String getParamterType() {
        return paramterType;
    }

    public void setParamterType(String paramterType) {
        this.paramterType = paramterType;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }
}

mybatis拦截器代码如下 @Override public Object intercept(final Invocation invocation) throws Throwable { // 检查表名替换开关是否开启(处理null值情况) Boolean tableReplaceEnabled = configParam.getTableReplaceEnabled(); if (tableReplaceEnabled == null || !tableReplaceEnabled) { log.info("表名替换开关关闭:{}", tableReplaceEnabled); return invocation.proceed(); } log.info("表名替换开关打开:{}", tableReplaceEnabled); // 获取原始SQL Object[] args = invocation.getArgs(); MappedStatement mappedStatement = (MappedStatement) args[0]; Object parameter = args[1]; BoundSql boundSql = mappedStatement.getBoundSql(parameter); String originalSql = boundSql.getSql(); // 检查是否包含需要替换的表名 if (originalSql.contains(ORIGINAL_TABLE_NAME)) { // 执行表名替换 String replacedSql = originalSql.replace(ORIGINAL_TABLE_NAME, TARGET_TABLE_NAME); log.info("表名替换完成,原始SQL:{},替换后SQL:{}", originalSql, replacedSql); //重新生成一个BoundSql对象,确保参数映射正确传递 // 关键:使用原始的parameter对象,这样MyBatis会自动处理批量操作的参数绑定 BoundSql bs = new BoundSql(mappedStatement.getConfiguration(), replacedSql, boundSql.getParameterMappings(), parameter); //重新生成一个MappedStatement对象 MappedStatement newMs = copyMappedStatement(mappedStatement, new BoundSqlSqlSource(bs)); //赋回给实际执行方法所需的参数中 args[0] = newMs; } // 继续执行原始查询 return invocation.proceed(); }在遇到foreach标签的sql执行报错Parameter '__frch_item_0' not found. Available parameters are [request, param1]请分析问题原因并修复
11-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

enterpc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值