SpringBoot整合Mybatis传参的⼏种⽅式(多参数传递)在SpringBoot整合Mybatis中,传递多个参数的⽅式和Spring整合Mybatis略微有点不同,下⾯主要总结三种常⽤的⽅式
⼀、顺序传参法
Mapper层:
传⼊需要的参数
public interface GoodsMapper {
public Goods selectBy(String name,int num);
}
*使⽤这种⽅式,在SpringBoot中,参数占位符⽤#{arg0},#{arg1},#{arg…}
总结:这种⽅法不建议使⽤,sql层表达不直观,且⼀旦顺序调整容易出错。
⼆、@Param注解传参法
Mapper层:
public interface GoodsMapper {
public Goods selectBy(@Param("name")String name,@Param("num")int num);
}
*#{}⾥⾯的名称对应的是注解@Param括号⾥⾯修饰的名称。
<select id="selectBy" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from goods
where good_name=#{name} and good_num=#{num}
</select>
总结:这种⽅法在参数不多的情况还是⽐较直观的,推荐使⽤。
三、使⽤Map封装参数
Mapper层:
将要传⼊的多个参数放到Map集合
public interface GoodsMapper {
public Goods selectBy(Map map);
}
*#{}⾥⾯的名称对应的是Map⾥⾯的key名称
<select id="selectBy" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from goods
where good_name=#{name} and good_num=#{num}
</select>param name
总结:这种⽅法适合传递多个参数,且参数易变能灵活传递的情况。
Service层:
带上要传⼊的参数
public interface GoodsService {
public Goods selectBy(String name,int num);
}
Service接⼝实现层:
封装Map集合:
@Override
public Goods selectBy(String name,int num) {
Map<String, String> map = new HashMap<String, String>();
map.put("name",name);
map.put("num",num+"");
return  GoodsMapper.selectBy(map);
}
Controller层:
@RequestMapping("/selectBy.do")
@ResponseBody
public Goods selectBy(String name,int num) {
return  goodsService.selectBy(name,num);
}
测试:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。