首页 > 其他分享 >Mybatis 常用传参方式

Mybatis 常用传参方式

时间:2023-05-03 16:23:07浏览次数:28  
标签:传参 node 常用 List env appId Mybatis id select

虽然了解Mybatis 的底层原理,技术架构,各种对接搭建才是高大上,但是对于业务实现的高频率散点的汇总,更加有实用价值,规避常见问题,一步到位精准快速开发,可以节省生命。

单个简单参数

public List<AgentVo> getAgentList(String appId);
  
<select id="getAgentList" resultType="com.wht.demo.dao.vo.AgentVo">  
	select 
		t.node_id as nodeId,
		t.host_name as hostName,
		t.address_ip as addressIp
	from 
	t_node_agent t 
	where t.delete_flag =1 
	and t.app_id= #{appId}  
</select> 

1、单个参数传递可以直接用变量名,可以支持多种类型。
2、select 中的parameterType="String" 可以省略

单个实体对象参数

Java是面向对象开发,我们很多数据传递的是一个VO,例如搜索面板。

public List<AgentVo> getAgentList(AgentVo agentVo);
  
<select id="getAgentList" resultType="com.wht.demo.dao.vo.AgentVo">  
	select 
		t.node_id as nodeId,
		t.host_name as hostName,
		t.address_ip as addressIp
	from 
	t_node_agent t 
	where t.delete_flag =1 
	and t.app_id= #{appId} 
	and t.env = #{env}
</select> 

1、单个对象作为参数,可以用对应的属性直接接受。

单个List传参

这个是真实项目非常常见的传参方式

public List<AgentVo> getAgentList(List<String> appIdList);
  
<select id="getAgentList" resultType="com.wht.demo.dao.vo.AgentVo">  
	select 
		t.node_id as nodeId,
		t.host_name as hostName,
		t.address_ip as addressIp
	from 
	t_node_agent t 
	where t.delete_flag =1 
	and t.app_id in
	<foreach item="item" index="index" collection="list" open="(" separator="," close=")"> 
    #{item} 
  </foreach>
</select> 

多个基础数据类型参数

为什么不适用VO对象直接传参,有些时候对象包含几十个字段,我们只是做简单的查询,直接用基础数据数据类型传参更加干净利落。

public List<AgentVo> getAgentList(String appId,String env);
  
<select id="getAgentList" resultType="com.wht.demo.dao.vo.AgentVo">  
	select 
		t.node_id as nodeId,
		t.host_name as hostName,
		t.address_ip as addressIp
	from 
	t_node_agent t 
	where t.delete_flag =1 
	t.app_id= #{0}  
	t.env = #{1}
</select> 

1、一般三个参数以内,这么传参
2、直接按照参数顺序接收参数。
3、为了提高可读性,建议都采用参数名接收,IDEA等开发工具自动带出,不影响开发速度。

public List<AgentVo> getAgentList(@Param("appId") String appId,@Param("env") String env);
  
<select id="getAgentList" resultType="com.wht.demo.dao.vo.AgentVo">  
	select 
		t.node_id as nodeId,
		t.host_name as hostName,
		t.address_ip as addressIp
	from 
	t_node_agent t 
	where t.delete_flag =1 
	t.app_id= #{appId}  
	t.env = #{env}
</select> 

@Param 是一个让人见文知义的好注解,单个参数就没必要了。
所以更加推荐下面这种方式。

基础数据类型加List

有时候我们需要不只是单一的参数传递方式

public List<AgentVo> getAgentList(@Param("env") String env, @Param("appIdList") List<String> appIdList);
  
<select id="getAgentList" resultType="com.wht.demo.dao.vo.AgentVo">  
	select 
		t.node_id as nodeId,
		t.host_name as hostName,
		t.address_ip as addressIp
	from 
	t_node_agent t 
	where t.delete_flag =1 
	and t.env =#{env}
	and t.app_id in
	<foreach item="item" index="index" collection="appIdList" open="(" separator="," close=")"> 
    #{item} 
  </foreach>
</select> 

Map传参数

service层

Map<String, Object> params = new HashMap<String, Object>();
List<Integer> nodeIdList = new ArrayList<>();
nodeIdList.add(178325);
nodeIdList.add(178334);
nodeIdList.add(178486);
params.put("appId", "com.jd.hr.ploud");
params.put("nodeIdList", nodeIdList );
List<AgentVo> list=AgentVoDao.findAgents(params);

Dao层

 //根据userid和passengerType查询
 List<AgentVo> findAgents(Map<String, Object> params);

配置SQL

<select id="findAgents" resultMap="com.wht.demo.dao.vo.AgentVo" parameterType="java.util.Map">
 	select 
		t.node_id as nodeId,
		t.host_name as hostName,
		t.address_ip as addressIp
	from 
	t_node_agent t 
	where t.delete_flag =1  
	and node_id in
   <foreach item="item" index="index" collection="nodeIdList" open="(" separator="," close=")">  
   #{item}  
   </foreach>

   <if test="appId!= null" >
     and app_id= #{appId}
   </if>
  </select>

标签:传参,node,常用,List,env,appId,Mybatis,id,select
From: https://www.cnblogs.com/hcgk/p/17369199.html

相关文章

  • mybatis 动态sql语句拼接
    mybatis常用占位符的方式传递参数,比较安全,可以防止sql注入。有些时候,特殊逻辑需要java封装好一些特殊的查询条件,然后和mybatis一起封装成特殊sql进行执行。{param}:替换参数${sql}:拼接sql语句。遇到一些模板类的功能,例如用户选择查询条件,或者查询语句,就需要动态sql技术。这个......
  • mybatis格式化输出标签之trim标签
    trim属性主要有以下四个prefix:前缀覆盖并增加其内容suffix:后缀覆盖并增加其内容prefixOverrides:前缀判断的条件suffixOverrides:后缀判断的条件其实这个很少用,因为有对应的标签做处理。查询等同where标签功能<selectid="getAgentList"resultType="com.wht.demo.dao.vo.A......
  • mybatis格式化输出标签之where 标签
    复制copy是程序员提高效能的核心方式,但是由于有些知识陈旧了,年复一年的复制,而没有系统的知识学习,并不晓得还有更合理或者更优雅的写法。1=1这种东西很多项目很常见,但是应该被放进历史的垃圾桶的。<selectid="getAgentList"resultType="com.wht.demo.dao.vo.AgentVo"> s......
  • mybatis之OGNL表达式
    对象导航图语言(ObjectGraphNavigationLanguage),简称OGNL,是应用于Java中的一个开源的表达式语言(ExpressionLanguage)这里制作简单的了解,知道是什么。OGNL表达式OGNL常用表达式e1ore2e1ande2e1==e2,e1eqe2e1!=e2,e1neqe2e1<e2,e1lte2e1<=e2,e1l......
  • SQL Server(1)常用查询
    序言 结果集拼接左右横向拼接LEFTJOIN上线纵向拼接UNIONunion:得到两个查询结果的并集,并且自动去掉重复行。不会排序unionall:得到两个查询结果的并集,不会去掉重复行。也不会排序 YEARMONTH资料......
  • 常用Collection接口下集合,Map接口下集合
    Collection接口下集合Map接口下集合阻塞讲解 ......
  • 常用Collection接口下集合,Map接口下集合
    Collection接口下集合Map接口下集合阻塞讲解 ......
  • 常用Collection接口下集合,Map接口下集合
    Collection接口下集合  Map接口下集合 阻塞讲解 ......
  • 常用Collection接口下集合,Map接口下集合
    Collection接口下集合  Map接口下集合 阻塞讲解 ......
  • 第139篇:JS数组常用方法(map(),reduce(),foreach())
    好家伙,本篇为MDN文档数组方法的学习笔记Array.prototype.reduce()-JavaScript|MDN(mozilla.org)数组方法这块的知识缺了,补一下 1.map()方法map() 方法创建一个新数组,这个新数组由原数组中的每个元素都调用一次提供的函数后的返回值组成。constarray1=[1,4,9,......