1.map接口传递参数
接口参数为Map,按密钥值映射。[en]The API parameter is Map, which is mapped by key value.
public List<Role> findRolesByMap(Map<String,Object> parameterMap);
<select id="findRolesByMap" parameterType="map" resultType="role">
select name,email,phone from t_role where username=#{username} and password=#{password}
select>
通过map的键名去匹配值
2.使用注解传递多个参数
public List<Role> findRolesByAnno(@Param("username")String username,@Param("password")String password);
<select id="findRolesByAnno" resultType="role">
select name,email,phone from t_role where username=#{username} and password=#{password}
select>
使用注解方式无需写parameterType属性,MyBatis会自动匹配
3.通过JavaBean传递
class Bean {
String password;
String username;
}
public List<Role> findRolesByBean(Bean bean);
<select id="findRolesByBean" parameterType="Bean全类名" resultType="role">
select name,email,phone from t_role where username=#{username} and password=#{password}
select>
bean类需要写get和set方法,parameterType需要写全类名
4.混合使用
class Bean {
String password;
String username;
}
class Bean2 {
String sex;
String phone;
}
public List<Role> findRolesByMix(@Params("bean1")Bean1 bean1,@Params("bean2")Bean2 bean2);
<select id="findRolesByMix" resultType="role">
select name,email,phone from t_role where username=#{bean1.usernmae} and password=#{bean1.password} and sex=#{bean2.sex}
select>
这是混合了注解与bean传参的方式,用到了注解传参,也无需写parametersType属性
总结:
1.使用map传参导致了业务可读性的丧失,因为必须要阅读其键值才知道其作用,扩展可维护性差,最好不要使用该方式。
2.使用@Param参数,在参数<=5个的时候推荐使用,参数较多时建议使用javabean的方式,因为更加直观 3.对于使用混合参数的,要明确参数的合理性 < code></=5个的时候推荐使用,参数较多时建议使用javabean的方式,因为更加直观>
Original: https://blog.csdn.net/weixin_45056780/article/details/110249795
Author: 世代农民
Title: MyBatis传递多个参数的4种方式
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/6103/
转载文章受原作者版权保护。转载请注明原作者出处!