<statement> or DELIMITER expected, got 'id' mybatis版本1.3.0 今天莫名其妙的出来了这个问题

<statement> or DELIMITER expected, got 'id' mybatis版本1.3.0 今天莫名其妙的出来了这个问题,第1张

这个算是idea的一个bug,其实语句如果没有问题是可以正常使用的,如果不想看到这个红色提示可以在setting中去掉对它的检测。settings->Language Injections->xml:iBatis3:sql|select|insert|update|delete|statement中xmltag去掉“sql|”后保存即可

看看springmvc和mybatis的中文api,自己试着做一点CRUD的小例子,一点一点的做,从简单到复杂,慢慢就会上手了。这两个框架的整合还是蛮简单的,只要你的配置文件弄对了就行了。

  在工作中,我们常常需要在不改变源码的条件下对sql进行一些修改。

  在这过程中用到的技术原理就是mybatis的拦截器(对于mybatis的内置对象笔者也还知之甚少,但这个不耽误咱使用mybatis的拦截器)。

package cominterceptorsmybatis;

import lombokexternslf4jSlf4j;

import orgapachecommonslang3StringUtils;

import orgapacheibatisexecutorstatementStatementHandler;

import orgapacheibatismappingBoundSql;

import orgapacheibatismappingMappedStatement;

import orgapacheibatismappingSqlCommandType;

import orgapacheibatismappingSqlSource;

import orgapacheibatisplugin;

import orgapacheibatisreflectionDefaultReflectorFactory;

import orgapacheibatisreflectionMetaObject;

import orgapacheibatisreflectionSystemMetaObject;

import orgapacheibatisreflectionfactoryDefaultObjectFactory;

import orgapacheibatisreflectionwrapperDefaultObjectWrapperFactory;

import orgapacheibatissessionResultHandler;

import orgapacheibatissessionRowBounds;

import orgspringframeworkstereotypeComponent;

import javalangreflectField;

import javalangreflectMethod;

import javasqlConnection;

import javautilProperties;

@Component

@Intercepts({

@Signature(

type = StatementHandlerclass, method ="prepare", args = {Connectionclass, Integerclass})

})

@Slf4j

public class MybatisSqlInterceptorimplements Interceptor {

@Override

    public Object intercept(Invocation invocation)throws Throwable {

StatementHandler statementHandler = (StatementHandler) invocationgetTarget();

MetaObject metaObject = MetaObjectforObject(statementHandler, SystemMetaObjectDEFAULT_OBJECT_FACTORY, SystemMetaObjectDEFAULT_OBJECT_WRAPPER_FACTORY,new DefaultReflectorFactory());

//先拦截到RoutingStatementHandler,里面有个StatementHandler类型的delegate变量,其实现类是BaseStatementHandler,然后就到BaseStatementHandler的成员变量mappedStatement

        MappedStatement mappedStatement = (MappedStatement) metaObjectgetValue("delegatemappedStatement");

//id为执行的mapper方法的全路径名

        String id = mappedStatementgetId();

//sql语句类型 select、delete、insert、update

        String sqlCommandType = mappedStatementgetSqlCommandType()toString();

BoundSql boundSql = statementHandlergetBoundSql();

//获取到原始sql语句

        String sql = boundSqlgetSql();

String mSql = sql;

//TODO 修改位置

        //注解逻辑判断  添加注解了才拦截

        Class classType = ClassforName(mappedStatementgetId()substring(0, mappedStatementgetId()lastIndexOf("")));

String mName = mappedStatementgetId()substring(mappedStatementgetId()lastIndexOf("") +1, mappedStatementgetId()length());

for (Method method : classTypegetDeclaredMethods()) {

if (methodisAnnotationPresent(InterceptAnnotationclass) && mNameequals(methodgetName())) {

InterceptAnnotation interceptorAnnotation = methodgetAnnotation(InterceptAnnotationclass);

if (interceptorAnnotationflag()) {

//重新设置mapper接口的参数或者对sql进行改造

                    boundSqlsetAdditionalParameter("","");

}

}

}

//通过反射修改sql语句

        Field field = boundSqlgetClass()getDeclaredField("sql");

fieldsetAccessible(true);

fieldset(boundSql, mSql);

return invocationproceed();

}

@Override

    public Object plugin(Object target) {

if (targetinstanceof StatementHandler) {

return Pluginwrap(target,this);

}else {

return target;

}

}

@Override

    public void setProperties(Properties properties) {

}

}

package cominterceptorsmybatis;

import javalangannotationElementType;

import javalangannotationRetention;

import javalangannotationRetentionPolicy;

import javalangannotationTarget;

@Target({ElementTypeMETHOD, ElementTypePARAMETER})

@Retention(RetentionPolicyRUNTIME)

public @interface InterceptAnnotation {

boolean flag()default true;

}

(对于mybatis拦截器的使用还需要了解mybatis更多的内置方法,本篇文章也只是mybatis拦截器的一点皮毛)

以上就是关于<statement> or DELIMITER expected, got 'id' mybatis版本1.3.0 今天莫名其妙的出来了这个问题全部的内容,包括:<statement> or DELIMITER expected, got 'id' mybatis版本1.3.0 今天莫名其妙的出来了这个问题、mybatis CRUD 出现问题 不知道该怎么弄、mybatis拦截处理等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9320096.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存