zoukankan      html  css  js  c++  java
  • mybatis多个参数不加@Param

    mybatis 3.4.1之后,允许不加 @Param指定参数名称,自动会以入参的名称作为param key

    useActualParamName
    允许使用方法签名中的名称作为语句参数名称。
    为了使用该特性,你的项目必须采用 Java 8 编译,并且加上 -parameters 选项。(新增于 3.4.1)
    默认:true

    案例

    不加 @Param 的多入参mapper

    public interface TestUserMapper {
        int update(Long guid, String appName);
    }
    

    入参为: update(110, "testAppName")

    • 编译时不加 -parameters 参数,那么mybatis的参数的格式是 {"arg0":110, "arg1":"testAppName"} , 在xml文件就不能用 #{guid} 来取值了
    • 加上 -parameters 编译后,参数格式为{"guid":110, "appName":"testAppName"} 在xml sql中就能正常使用参数名称

    原理:

    useActualParamName 默认是true,如果入参没加@Param指定参数key,则会利用java8的特性,从反射类Parameter#getName()获取这个参数的名称

    例如,可以maven编译插件上指定

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>8</source>
            <target>8</target>
            <compilerArgs>
                <arg>-verbose</arg>
                <arg>-Xlint:all</arg>
                **<arg>-parameters</arg>**
            </compilerArgs>
        </configuration>
    </plugin>
    

    结论

    不推荐偷懒,这种方式依赖java8的编译特性,比较隐晦,容易让其他开发混淆

    本文来自博客园,作者:mushishi,转载请注明原文链接:https://www.cnblogs.com/mushishi/p/14613773.html

  • 相关阅读:
    docker基本指令
    Process API
    Go的Context用法
    golang的可空类型和零值
    动态编程(DynamicObject、ExpandoObject)
    git clean
    SQL Server Join
    公用表表达式 Common Table Expression
    ubuntu安装mysql
    asp.net core-使用Nlog和log4net
  • 原文地址:https://www.cnblogs.com/mushishi/p/14613773.html
Copyright © 2011-2022 走看看