ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

MyBatis传递获取参数

MyBatis传递获取参数 MyBatis 参数传递 获取参数完整总结MyBatis 参数本质把 Java 方法入参传入 xml/mapper通过#{}/${}取值底层使用 OGNL 表达式解析。一、#{} 与 ${} 核心区别必记#{param}预编译占位符?防止 SQL 注入绝大多数场景使用${param}直接字符串拼接无预编译有注入风险只用于动态表名、排序字段-- 安全 where name #{name} -- 危险直接拼接 order by ${sortColumn}二、各种传参方式方式 1单个简单参数基本类型 / 包装类 String Integer Longmapper 接口User selectById(Long id);xml直接写参数名即可名字可以任意MyBatis 单参数不校验名字select idselectById resultTypeUser select * from t_user where id #{id} /select注意单个参数#{任意名字}都可以拿到值不匹配也不会报错。方式 2多个普通参数多个入参① 使用 Param企业最常用推荐接口用Param(xxx)指定参数名ListUser selectUser(Param(name) String name, Param(age) Integer age);xml 直接使用注解定义的名字select idselectUser resultTypeUser select * from t_user where if testname ! nulland name like concat(%,#{name},%)/if if testage ! nulland age #{age}/if /where /select② 不使用 Param使用 arg0,arg1 /param1,param2不推荐可读性差where name #{arg0} and age #{arg1} where name #{param1} and age #{param2}生产禁止参数顺序改动直接崩。方式 3POJO 实体对象传参最常用接口int updateUser(User user);User 属性id,name,age xml 直接写属性名update idupdateUser update t_user set if testname!nullname#{name},/if if testage!nullage#{age},/if /set where id#{id} /update#{name}等价于取入参对象的user.getName()OGNL 访问对象属性。方式 4Map 传参接口ListUser selectByMap(MapString,Object params);java 调用MapString,Object map new HashMap(); map.put(name,张三); map.put(age,20); mapper.selectByMap(map);xml 直接取 map 的 keyselect idselectByMap resultTypeUser select * from t_user where if testname ! nulland name#{name}/if if testage ! nulland age#{age}/if /where /select方式 5集合 / 数组参数foreach 场景List 集合ListUser selectByIds(ListLong ids);xml collection 写listforeach collectionlist itemid open( close) separator, #{id} /foreach数组ListUser selectByIds(Long[] ids);xml collection 写arrayforeach collectionarray itemid open( close) separator, #{id} /foreach如果集合放在 POJO 对象属性中class Query{ private ListLong idList; } ListUser select(Query query);collection 写属性名foreach collectionidList itemid如果加了Param(idList)collection 写注解名字。方式 6多参数其中一个是集合ListUser select(Param(status) Integer status,Param(idList) ListLong idList);xmlwhere status #{status} and id in foreach collectionidList itemid open( close) separator, #{id} /foreach三、#{} 内部属性#{name,javaTypeString,jdbcTypeVARCHAR}jdbcType数据库字段类型null 值的时候建议指定部分数据库 (Oracle) 传 null 会报错name #{name,jdbcTypeVARCHAR} age #{age,jdbcTypeINTEGER}javaTypejava 类型一般不需要写mode存储过程用typeHandler自定义类型处理器四、OGNL 表达式if test 里面的语法if test 这里是OGNL表达式不是sql !-- 判断非null并且非空字符串 -- if testname ! null and name !/if !-- 判断数字 -- if testage ! null and age 18/if !-- 对象属性嵌套 -- if testuserVO.deptId ! null/if !-- 调用方法 -- if testname.length()0/if⚠️坑test 里面不要写#{}${}直接写属性名五、返回参数 / 结果映射 resultType /resultMap1.resultType简单映射别名 / 全类名自动把列名和实体属性映射下划线转驼峰需要开启配置select idxxx resultTypecom.xxx.entity.User2.resultMap自定义映射复杂字段、关联查询、字段名不一致手动配置resultMap iduserMap typeUser id columnuid propertyid/ result columnuser_name propertyname/ /resultMap select idxxx resultMapuserMapapplication.yml 开启下划线转驼峰mybatis: configuration: map-underscore-to-camel-case: true六、高频踩坑总结1. 多个参数不加 Param直接写属性名报错找不到参数多个参数必须加Param或者封装对象 /map。2.test中写#{name}OGNL 解析异常test 里面直接写属性不要占位符。3. Oracle 传入 null报错需要指定 jdbcType4. foreach collection 写错不加 Paramlist 集合→list数组→array加 Paramcollection 写注解名字在对象里面写对象属性名5.${}直接拼接注意 SQL 注入禁止直接接收前端传入的表名。6. 单参数xml 参数名可以随便写但是加上 Param 之后必须和注解名字一致。对比 Spring Data JPA 参数MyBatis手动控制参数#{} 预编译灵活JPA?1 ?2或者:name命名参数。
返回列表