Mybatis 入参类型方式全面详解
更新时间:2023年06月02日 09:28:01 作者:Mzoro
这篇文章主要为大家介绍了Mybatis入参的类型方式全面示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
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>以上就是Mybatis 入参类型方式全面详解的详细内容,更多关于Mybatis 入参方式的资料请关注脚本之家其它相关文章!
相关文章
springboot中RestTemplate配置HttpClient连接池详解
这篇文章主要介绍了springboot中RestTemplate配置HttpClient连接池详解,这些Http连接工具,使用起来都比较复杂,如果项目中使用的是Spring框架,可以使用Spring自带的RestTemplate来进行Http连接请求,需要的朋友可以参考下2023-11-11
基于idea 的 Java中的get/set方法之优雅的写法
这篇文章主要介绍了基于idea 的 Java中的get/set方法之优雅的写法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2019-01-01


最新评论