联动
因为是查询要放在查询的位置
HTML页面(显示)
<li> 学生学校: <!-- <select id="school" name="sysSchool.schoolId">--> <select id="school" name="sysGrade.gradeSchool"> <option value="">请选择学校</option> </select> 学生年级: <!-- <select id="grade" name="sysGrade.gradeId">--> <select id="grade" name="sysClasses.classesGrade"> <option value="">请选择年级</option> </select> 学生年级: <!-- <select id="classes" name="sysClasses.classesId">--> <select id="classes" name="studentClass"> <option value="">请选择班级</option> </select> </li>
js方法(都是重复的,区别:第一个是查全部,之后都是传参查的)
<script type="text/javascript"> $(document).ready(function() {//页面加载时运行此函数 放在最前端 $.ajax({ url: ctx + "system/school/select", type: 'GET', success:function (data) { <!--清空下拉框中的缓存--> $("#school").empty(); <!--避免下拉框的值不变--> $("#school").append("<option value=''>请选择学校</option>"); <!--使用循环解析后端传来的数据,并用使用js动态拼接html语言--> for (var i = 0; i < data.length; i++){ $("#school").append("<option value='" + data[i].schoolId + "'>" + data[i].schoolName + "</option>"); } } }); $("#school").change(function () {//同上面一样 // alert($("#school").val()) $.ajax({ url: ctx + "system/grade/select", type: 'GET', data: { gradeSchool: $("#school").val(), }, success:function (data) { $("#grade").empty(); $("#grade").append("<option value=''>请选择年级</option>"); for (var i = 0; i < data.length; i++){ $("#grade").append("<option value='" + data[i].gradeId + "'>" + data[i].gradeName + "</option>"); } } }); }); $("#grade").change(function () {//同上面一样 $.ajax({ url: ctx + "system/classes/select", type: 'GET', data: { classesGrade: $("#grade").val(), }, success:function (data) { $("#classes").empty(); $("#classes").append("<option value=''>请选择班级</option>"); for (var i = 0; i < data.length; i++){ $("#classes").append("<option value='" + data[i].classesId + "'>" + data[i].classesName + "</option>"); } } }) }); }) </script>
controler层
school
@GetMapping("/select") @ResponseBody public List<SysSchool> select() { List<SysSchool> list = schoolService.selectSchoolList(new SysSchool()); return list; }
grade
@GetMapping("/select") @ResponseBody public List<SysGrade> select(Long gradeSchool) { // System.out.println("11111111111111111111"); // System.out.println(gradeSchool); List<SysGrade> list1 = gradeService.selectGradeById1(gradeSchool); List<SysGrade> list = gradeService.selectGradeList(new SysGrade()); return list1; }
classes
@GetMapping("/select") @ResponseBody public List<SysClasses> select(Long classesGrade) { List<SysClasses> list1 = classesService.selectClassesById1(classesGrade); List<SysClasses> list = classesService.selectClassesList(new SysClasses()); return list1; }
mapper.xml(写sql语句的地方)
方法都一样,这里就不都写了,只写一个grade表的
<select id="selectClassesById1" parameterType="Long" resultMap="SysClassesResult"> <include refid="selectClassesVo"/> where classes_grade = #{classesGrade} </select>
mapper.java
public List<SysGrade> selectGradeById1(Long gradeSchool);
service层
public List<SysGrade> selectGradeById1(Long gradeSchool);
serviceImpl层
@Override public List<SysGrade> selectGradeById1(Long gradeSchool) { return gradeMapper.selectGradeById1(gradeSchool); }
下拉框查询的传值
要先进行表连接,建立关系,不然怎么查,表连接的几个字段,还有SQL语句
<sql id="selectStudentVo"> SELECT * FROM sys_student INNER JOIN sys_classes INNER JOIN sys_grade INNER JOIN sys_school ON sys_student.`student_class` = sys_classes.`classes_id` AND sys_classes.`classes_grade` = sys_grade.`grade_id` AND sys_grade.`grade_school` = sys_school.`school_id` </sql>
下拉框查询的
注意,这里需要改一下id的格式,不然查不了,是一个本人解决不了,或者本身就存在的问题,改字段类型,char,varchar都可以。后期解决会发布,也求大神改正。
<select id="selectStudentList" parameterType="SysStudent" resultMap="SysStudentResult"> <include refid="selectStudentVo"/> <where> <if test="studentName != null and studentName != ''"> AND student_name like concat('%', #{studentName}, '%') </if> <if test="abs != null and abs != ''"> AND abs like concat('%', #{abs}, '%') </if> <if test="sysGrade.gradeSchool != null and sysGrade.gradeSchool != ''"> AND sys_grade.grade_school =#{sysGrade.gradeSchool} </if> <if test="sysClasses.classesGrade != null and sysClasses.classesGrade != ''"> AND sys_classes.classes_grade =#{sysClasses.classesGrade} </if> <if test="studentClass != null and studentClass != ''"> AND student_class =#{studentClass} </if> </where> ORDER BY sys_student.`abs` DESC ,sys_student.`student_id` ASC </select>