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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一个优秀的Java持久化框架,它可以轻松地将数据库表和Java对象之间进行映射。在开发过程中,我们通常需要向MyBatis的SQL语句中传递多个参数。下面我们来介绍MyBatis传递多个参数的方法。 1. 使用Map传递参数。 通过使用Map来传递多个参数,可以将多个参数打包到一个Map对象中。在MyBatis的Mapper.xml文件中,使用#{key}来引用Map中的参数值。例如,如果我们要传递两个参数,一个是username,一个是age,可以使用如下的方式: ``` <select id="getUser" parameterType="map" resultType="User"> SELECT * FROM user WHERE username = #{username} AND age = #{age} </select> ``` 在Java中使用Map传递参数的例子如下: ``` Map<String, Object> parameterMap = new HashMap<>(); parameterMap.put("username", "张三"); parameterMap.put("age", 18); User user = sqlSession.selectOne("getUser", parameterMap); ``` 2. 使用@Param注解传递参数。 @Param注解可以用来指定参数的名称,从而在Mapper.xml文件中和Java中使用相同的参数。在Mapper.xml文件中,使用#{参数名}来引用参数值。例如,如果我们要传递两个参数,一个是username,一个是age,可以使用如下的方式: ``` <select id="getUser" parameterType="map" resultType="User"> SELECT * FROM user WHERE username = #{username} AND age = #{age} </select> ``` 在Java中使用@Param注解传递参数的例子如下: ``` public User getUser(@Param("username") String username, @Param("age") int age); ``` 3. 使用JavaBean传递参数。 在Java中,我们可以使用JavaBean来封装多个参数,然后在Mapper.xml文件中使用#{属性名}来引用JavaBean属性的值。例如,如果我们要传递两个参数,一个是username,一个是age,可以使用如下的JavaBean: ``` public class UserInfo { private String username; private int age; // getter/setter } ``` 在Mapper.xml文件中,可以如下使用JavaBean传递参数: ``` <select id="getUser" parameterType="UserInfo" resultType="User"> SELECT * FROM user WHERE username = #{username} AND age = #{age} </select> ``` 在Java中使用JavaBean传递参数的例子如下: ``` UserInfo userInfo = new UserInfo(); userInfo.setUsername("张三"); userInfo.setAge(18); User user = sqlSession.selectOne("getUser", userInfo); ``` 总之,MyBatis传递多个参数的方法有很多,主要是使用Map、@Param注解和JavaBean来封装参数。在使用的时候,我们需要根据具体情况,选择最适合的方法来传递参数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值