Mybatis 入参类型方式全面详解

作者:Mzoro 时间:2023-10-16 20:03:40 

Mybatis 入参方式

单个基本类型或 String 参数

在 mapper 文件中随便写

<select id="" resultMap="resultMap">
select * from USER_INFO t where t.name = #{sdfa,jdbcType=VARCHAR}
</select>
List<Student> get(String name);

单个 Map 或者自定义类型的

  • 如是 Map , 那么参数各是 Map 的 key

  • 如果是自定义类型的,参数是属性名,更确切的说是 get 方法,例如:getName (), 那么 mapper 文件中就要写 #{name,jdbcType=VARCHAR}

如果是单个的 Collection

参数名字就是 collection

<select id="" resultMap="resultMap">
select * from USER_INFO t where t.name in
   <foreach collection="conllection" item="i" ......>
   </foreach>
</select>
List<Student> get(List<String> names);

如果是多个参数

可以使用 @Param ("parametername")

<select id="" resultMap="resultMap">
select * from USER_INFO t where t.name in
   <foreach collection="param" item="i" ......>
   </foreach>
   and age = #{age,jdbcType=NUMERIC}
</select>
List<Student> get(@Param("param") List<String> names,@Param("age") int age);

如果不想使用 @Param,而是想直接使用接口方法参数的变量名作为 mapper 的参数名,需要增加 编译参数 -parameters, 并启用 useActualParamName 选项(默认开启)来编译项目这里以 maven 为例

普通工程

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <target>1.8</target>
                <source>1.8</source>
                <parameters>true</parameters>
            </configuration>
        </plugin>
    </plugins>
</build>

springboot:

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-parameters</jvmArguments><!--  增加这个参数 -->
</configuration>
</plugin>

如上设置好之后 ,就可以直接用接口方法参数名作为 mapper 参数了

接口文件中:

List<ComBusinessSwitch> getSwitchByCode(String code, String orgId, String stationId);

mapper 文件中

<select id="getSwitchByCode" resultMap="BaseResultMap">
select * from SWITCH T where code = #{code,jdbcType=VARCHAR} and orgid = #{orgId,jdbcType=VARCHAR} and stationid = #{stationId,jdbcType=VARCHAR}
</select>

来源:https://my.oschina.net/Mzoro/blog/3147716

标签:Mybatis,入参,类型
0
投稿

猜你喜欢

  • C#多线程之任务的用法详解

    2023-08-27 10:51:18
  • Android入门之使用OKHttp组件访问网络资源

    2023-08-28 10:57:03
  • Spring高阶用法之自定义业务对象组件化

    2022-02-18 07:48:55
  • Mybatis工具类JdbcTypeInterceptor运行时自动添加jdbcType属性

    2023-08-24 03:49:59
  • 简单介绍三层架构工作原理

    2022-10-01 20:28:25
  • JAVA读取文件夹大小的几种方法实例

    2021-05-24 21:01:53
  • Java递归基础与递归的宏观语意实例分析

    2021-06-24 09:33:10
  • Spring5新功能@Nullable注解及函数式注册对象

    2021-10-27 05:49:44
  • Android自定义ViewPager实例

    2023-03-11 10:24:50
  • Spring Cloud Ribbon的踩坑记录与原理详析

    2023-02-06 04:06:55
  • C#实现的算24点游戏算法实例分析

    2021-12-01 04:13:21
  • SpringBoot中利用AOP和拦截器实现自定义注解

    2022-09-14 00:26:53
  • SSH框架网上商城项目第10战之搭建商品类基本模块

    2023-11-12 14:00:29
  • Android自定义View圆形进度条控件(三)

    2021-11-13 10:17:51
  • 通过Java修改游戏存档的实现思路

    2023-07-30 20:10:55
  • BufferedInputStream(缓冲输入流)详解_动力节点Java学院整理

    2022-01-08 07:02:41
  • JavaWeb Hibernate使用全面介绍

    2021-09-13 11:26:07
  • RxJava2.x实现定时器的实例代码

    2023-08-06 17:41:01
  • Spring Cloud Stream消息驱动组件使用方法介绍

    2022-05-30 04:30:52
  • C#强制类型转换小结

    2023-11-01 06:29:53
  • asp之家 软件编程 m.aspxhome.com