Mybatis 常用传参方式
虽然了解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>
分类:
mybatis
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码