SSM框架中mybatis传递多个参数实现联合查询的两种方法

时间:2024-04-05 17:30:30
①将多个参数封装成一个类
1.首先创建一个类,将参数作为类的属性
public class Student {
private int studentId;
private String studentName;
private String studentClassid;

public int getStudentId() {
return studentId;
}

public void setStudentId(int studentId) {
this.studentId = studentId;
}

public String getStudentName() {
return studentName;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}

public String getStudentClassid() {
return studentClassid;
}

public void setStudentClassid(String studentClassid) {
this.studentClassid = studentClassid;
}

@Override
public String toString() {
return "Student{" +
"studentId=" + studentId +
", studentName='" + studentName + '\'' +
", studentClassid='" + studentClassid + '\'' +
'}';
}

public Student(int studentId, String studentName, String studentClassid) {
this.studentId = studentId;
this.studentName = studentName;
this.studentClassid = studentClassid;
}
}


2.DAO层
/**
* 联合查询,测试一:传参为类
* @param student
* @return
*/
List<Student> selectStudent(Student student);
3.mapper的SQL语句
<!--联合查询1-->
<select id="selectStudent" parameterType="Student" resultType="Student">
SELECT student_id,student_name,student_classid FROM student
<where>
<if test="studentId!=null and studentId!='' ">
AND student_id=#{studentId}
</if>
<if test="studentName!=null and studentName !='' ">
AND student_name LIKE '%' #{studentName} '%'
</if>
<if test="studentClassid!=null and studentClassid!='' ">
AND student_classid=#{studentClassid}
</if>
</where>
</select>
4.service层
@Override
public ServerResponse<List<Student>> queryStudent(Student student) {
List<Student> studentList = studentDao.selectStudent(student);
if (studentList.size()>0){
return ServerResponse.createBySuccess("查询成功",studentList);
}
return ServerResponse.createByError("查询记录为空");
}
5.controller层
@RequestMapping(value = "/queryStudent")
@ResponseBody
public ServerResponse queryStudent(Student student,HttpSession session){
ServerResponse serverResponse = CheckRole.checkIsLoginAndAdmin(session);
if (serverResponse.checkIsSuccess()){
return studentService.queryStudent(student);
}
return serverResponse;
}
6.测试(可以选择传递参数,也可以不传递参数,不传递参数即为查询所有记录)

SSM框架中mybatis传递多个参数实现联合查询的两种方法
SSM框架中mybatis传递多个参数实现联合查询的两种方法
②使用@Param注解
1.DAO层
/**
* 联合查询,测试二:传参为字符串组合
* @param studentId
* @param studentName
* @param studentClassid
* @return
*/
List<Student> selectStudentByParam(@Param("studentId")String studentId,
@Param("studentName")String studentName,
@Param("studentClassid")String studentClassid);
2.mapperSQL语句
<!--联合查询2-->
<!--使用@param注释的多个参数值也会组装成一个map数据结构,和直接传递map进来没有区别。-->
<select id="selectStudentByParam" parameterType="map" resultType="Student">
SELECT student_id,student_name,student_classid FROM student
<where>
<if test="studentId!=null and studentId!='' ">
AND student_id=#{studentId}
</if>
<if test="studentName!=null and studentName !='' ">
AND student_name LIKE '%' #{studentName} '%'
</if>
<if test="studentClassid!=null and studentClassid!='' ">
AND student_classid=#{studentClassid}
</if>
</where>
</select>
3.测试
@Test
public void queryStudent2(){
System.out.println(studentDao.selectStudentByParam("",
"咪总","1"));
}

SSM框架中mybatis传递多个参数实现联合查询的两种方法