Mybatis多参传递(四种方法)

方法一 将参数封装成pojo 推荐

测试

Student student = new Student();
student.setClassid(1);
student.setSsex("男");
System.out.println(studentMapper.selectStuByClassIdAndSsex(student));

接口

List<Student> selectStuByClassIdAndSsex(Student student);

实现类

@Override
public List<Student> selectStuByClassIdAndSsex(Student student) {
    SqlSession session = MybatisUtil.getSession();
    StudentMapper mapper = session.getMapper(StudentMapper.class);
    List<Student> studentList = mapper.selectStuByClassIdAndSsex(student);
    session.close();
    return studentList;
}

xml

<select id="selectStuByClassIdAndSsex" resultType="Student" parameterType="Student">
  select *
  from student
  where classid = #{classid}
  and ssex = #{ssex}
</select>

方法二 使用Map集合 不推荐

测试

HashMap<String, Object> map = new HashMap<>();
map.put("classid",1);
map.put("ssex","男");
map.put("page",(2-1)*5); //起始条数=(页码-1)*显示条数
map.put("pagesize",5);
System.out.println(studentMapper.selectStuByClassIdAndSsexLimit(map));

接口

 List<Student> selectStuByClassIdAndSsexLimit(Map<String, Object> map);

实现

@Override
public List<Student> selectStuByClassIdAndSsexLimit(Map<String, Object> map) {
    SqlSession session = MybatisUtil.getSession();
    StudentMapper mapper = session.getMapper(StudentMapper.class);
    List<Student> studentList = mapper.selectStuByClassIdAndSsexLimit(map);
    session.close();
    return studentList;
}

xml

<select id="selectStuByClassIdAndSsexLimit" parameterType="Student" resultType="Student">
  select *
  from student
  where classid = #{classid}
  and ssex = #{ssex} limit #{page},#{pagesize}
</select>

方法三 使用 param数字 数字从1开始 推荐

测试

System.out.println(studentMapper.selectStuByClassIdAndSsexLimitParam(1, "男", (1 - 1) * 5, 5));

接口

List<Student> selectStuByClassIdAndSsexLimitParam(int classid, String ssex, int page, int pagesize);

实现

@Override
public List<Student> selectStuByClassIdAndSsexLimitParam(int classid, String ssex, int page, int pagesize) {
    SqlSession session = MybatisUtil.getSession();
    StudentMapper mapper = session.getMapper(StudentMapper.class);
    List<Student> studentList = mapper.selectStuByClassIdAndSsexLimitParam(classid,ssex,page,pagesize);
    session.close();
    return studentList;
}

xml

<select id="selectStuByClassIdAndSsexLimitParam" resultType="Student">
  select *
  from student
  where classid = #{param1}
  and ssex = #{param2} limit #{param3},#{param4}
</select>

方法四 使用 arg数字 数字从0开始(不推荐 --》与之后学习框架的内置参数冲突)

测试

System.out.println(studentMapper.selectStuByClassIdAndSsexLimitArg(1, "男", (1 - 1) * 5, 5));

接口

List<Student> selectStuByClassIdAndSsexLimitArg(int classid, String ssex, int page, int pagesize);

实现

@Override
public List<Student> selectStuByClassIdAndSsexLimitArg(int classid, String ssex, int page, int pagesize) {
    SqlSession session = MybatisUtil.getSession();
    StudentMapper mapper = session.getMapper(StudentMapper.class);
    List<Student> studentList = mapper.selectStuByClassIdAndSsexLimitArg(classid,ssex,page,pagesize);
    session.close();
    return studentList;
}

xml

<select id="selectStuByClassIdAndSsexLimitArg" resultType="Student">
  select *
  from student
  where classid = #{arg0}
  and ssex = #{arg1} limit #{arg2}
  , #{arg3}
</select>
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis中,传递多个参数,包含集合作为入参可以通过以下方法实现: 1. 将多个参数封装成一个Java对象 可以创建一个Java对象,包含所有需要的参数,包括集合。然后将该对象作为方法的入参传递MyBatis。在SQL语句中可以直接引用该对象的属性值。 例如,创建一个包含多个参数和集合的Java对象: ```java public class MyParams { private int id; private String name; private List<Integer> ids; // 省略getters和setters方法 } ``` 在Mapper接口中定义方法: ```java void myMethod(MyParams params); ``` 在XML配置文件中使用该方法: ```xml <select id="myMethod" parameterType="com.example.MyParams"> SELECT * FROM my_table WHERE id = #{id} AND name = #{name} AND id IN <foreach item="item" collection="ids" open="(" separator="," close=")"> #{item} </foreach> </select> ``` 2. 使用@Param注解 可以在Mapper方法的参数上使用@Param注解来指定参数的名称,在SQL语句中可以直接引用该名称。 例如,定义Mapper方法: ```java void myMethod(@Param("id") int id, @Param("name") String name, @Param("ids") List<Integer> ids); ``` 在XML配置文件中使用该方法: ```xml <select id="myMethod" parameterType="java.util.Map"> SELECT * FROM my_table WHERE id = #{id} AND name = #{name} AND id IN <foreach item="item" collection="ids" open="(" separator="," close=")"> #{item} </foreach> </select> ``` 以上两种方法都可以实现在MyBatis传递多个参数,包含集合作为入参。根据实际情况选择合适的方法来使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值