Mybatis传入多个参数

使用Mybatis时,存在向Mybatis传多个参数的情况,以下介绍5种方式
1、直接传递多个参数,在mapper.xml文件中,根据参数出现的顺序引用(arg从0开始,param从1开始)

@Setter
@Getter
public class StudentVO {
    private Long id;
    private String name;
    private Short age;
    private String gender;
}

public interface StudentVOMapper {
    List<StudentVO> selectByCondition(Short age, String gender);
}

<!--mapper.xml文件中,参数顺序-->
<select id="selectByCondition" parameterType="java.lang.Long" resultMap="BaseResultMap">
  select * from student
  where age = #{arg0} and gender = #{arg1}
</select>
或
<select id="selectByCondition" parameterType="java.lang.Long" resultMap="BaseResultMap">
  select * from student
  where age = #{param1} and gender = #{param2}
</select>

2、使用注解

@Setter
@Getter
public class StudentVO {
    private Long id;
    private String name;
    private Short age;
    private String gender;
}

public interface StudentVOMapper {
    //映射的接口方法参数加上@Param注解
    List<StudentVO> selectByCondition(@Param("name") Short age, @Param("name") String gender);
}

<select id="selectByCondition" parameterType="java.lang.Long" resultMap="BaseResultMap">
  select * from student
  where age = #{age} and gender = #{gender}
</select>

3、使用map封装参数

@Setter
@Getter
public class StudentVO {
    private Long id;
    private String name;
    private Short age;
    private String gender;
}

public interface StudentService {
    List<StudentVO> selectByCondition(Short age, String gender);
}

public class StudentService implements StudentService {
    @Autowired
    StudentVOMapper studentVOMapper;

    //把多个参数组成map传入
    List<StudentVO> selectByCondition(Short age, String gender) {
        Map<String, String> paramMap = new HashMap();
        param.put("age", age);
        param.put("gender", gender);
        return studentVOMapper.selectByCondition(map);
    }
}

public interface StudentVOMapper {
    List<StudentVO> selectByCondition(Map map);
}

<!--传入参数类型修改为Map-->
<select id="selectByCondition" parameterType="java.util.Map" resultMap="BaseResultMap">
  select * from student
  where age = #{age} and gender = #{gender}
</select>

4、使用List封装参数

@Setter
@Getter
public class StudentVO {
    private Long id;
    private String name;
    private Short age;
    private String gender;
}

public class StudentService implements StudentService {
    @Autowired
    StudentVOMapper studentVOMapper;

    //把javabean的List传入
    List<StudentVO> selectByCondition() {
        List<StudentVO> studentList = new ArrayList<>();
        StudentVO studentVO1 = new StudentVO();
        studentVO1.setName("John");
        studentVO1.setGender("male");
        studentList.add(studentVO1);
        StudentVO studentVO2 = new StudentVO();
        studentVO2.setName("Lily");
        studentVO2.setGender("female");
        studentList.add(studentVO2);
        return studentVOMapper.selectByCondition(studentList);
    }
}

public interface StudentVOMapper {
    List<StudentVO> selectByCondition(List<StudentVO> studentList);
}

<!--传入参数类型修改为List-->
<select id="selectByCondition" parameterType="java.util.List" resultMap="BaseResultMap">
  select * from student where 1 = 1
  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
    <if test="item.name != null" >
      and name = {item.name}
    </if>
    <if test="item.gender != null" >
      and gender = {item.gender}
    </if>
  </foreach>
</select>


5、封装一个javabean,以javabean形式传递参数进mapper.xml文件

@Setter
@Getter
public class StudentVO {
    private Long id;
    private String name;
    private Short age;
    private String gender;
}

@Setter
@Getter
public class StudentCriteria {
    private Short age;
    private String name;
}

public interface StudentService {
    List<StudentVO> selectByCondition(Short age, String gender);
}

public class StudentService implements StudentService {
    @Autowired
    StudentVOMapper studentVOMapper;

    //把多个参数注入一个javabean传入
    List<StudentVO> selectByCondition(Short age, String gender) {
        StudentCriteria criteria = new StudentCriteria();
        criteria.setAge(age);
        criteria.setName(name);
        return studentVOMapper.selectByCondition(criteria);
    }
}

public interface StudentVOMapper {
    List<StudentVO> selectByCondition(StudentCriteria criteria);
}

<!--传入参数类型修改为javabean-->
<select id="selectByCondition" parameterType="**.***.StudentCriteria" resultMap="BaseResultMap">
  select * from student
  where age = #{age} and gender = #{gender}
</select>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值