Mybatis中为Mapper中传入多个值

1.通过顺序

 ​ <select id = "selectTest" resultMap = "SysResult">
     select * from user where name = #{0} and dept = #{1}
 </select>

在#{}中的数字代表了传递参数的顺序,一般不建议使用

2.通过@Param

 public User selectTest(@Param("UserName") String name,@Param("deptID") int deptId)
 <select id = "selectTest" resultMap = "UserResult">
     select * from user where name = #{userName} and dept = #{deptId}
 </select>

其中,#{}中的字符串就是@Param修饰的变量名称,适用于参数较少的情况

3.通过Map

 public User selectTest(Map<String,String> items);
 ​
 <select id = "selectTest" parameterType = "java.util.Map" resultMap = "UserResult">
     select * from user where name = #{NameKey} and dept = #{deptKey}
 </select>

#{}中的内容就是Map的Key

4.通过实体类

 public User selectTest(User user)
     
 <select id = "selectTest" parameterType = "cn.tineaine.pojo.User" resultMap = "UserResult">
     select * from user where name = #{userName} and dept = #{dept}
 </select>

#{}中的内容是POJO中成员属性的名称,其需要通过实体类进行操作,如果需要扩展,则必须修改pojo的内容

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章