方法一 将参数封装成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>