1.数据库关联关系:
SC(s_id , c_id , score) 成绩表(学生id,课程id,分数)
Student(sid,sname,sage,ssex)学生表(学生id,姓名,年龄,性别)
course(cid,cname,t_id)课程表(课程id,课程名称,代课老师)
teacher(tid,tname)老师表(老师id,老师姓名)
2.前台代码
<!-- 数据网格 --> <table id="tt2"> </table> $("#tt2").datagrid({ title:'取嵌套对象', 1100, heigh:500, rownumbers:true, fitColumns:true, singleSelect:true, pagination:true, url:'/scInfo', columns:[[ {title:'sc s_id',field:'s_id',80}, {title:'sc c_id',field:'c_id',80}, {title:'sc score',field:'score',100,align:'right'}, {title:'stu sid',field:'sid',80, formatter:function(value,row,index){ return row.student.sid; } }, {title:'stu sname',field:'sname',80, formatter:function(value,row,index){ return row.student.sname; } }, {title:'stu sage',field:'sage',80, formatter:function(value,row,index){ return row.student.sage; } }, {title:'stu ssex',field:'ssex',80, formatter:function(value,row,index){ return row.student.ssex; } }, {title:'course cid',field:'cid',80, formatter:function(value,row,index){ return row.course.cid; } }, {title:'course cname',field:'cname',80, formatter:function(value,row,index){ return row.course.cname; } }, {title:'course t_id',field:'t_id',80, formatter:function(value,row,index){ return row.course.t_id; } }, {title:'teacher tid',field:'tid',80, formatter:function(value,row,index){ return row.course.teacher.tid; } }, {title:'teacher tname',field:'tname',80, formatter:function(value,row,index){ return row.course.teacher.tname; } } ]] });
3.分页展示:
开启分页 pagination:true
请求的时候带page和rows ,返回的时候我们返回total总数,和数据集合list就ok了, 就这么简单。
@RequestMapping("/scInfo") public String info(HttpServletRequest request) { int page , rows , offset;//页码 ,每页展示条数,偏移量 String input_page = request.getParameter("page"); page = (input_page==null)?1:Integer.parseInt(input_page); String input_rows = request.getParameter("rows"); rows = (input_rows==null)?10:Integer.parseInt(input_rows); offset = (page-1)*rows; return scService.info(offset, rows); }
@Service public class ScServiceImpl implements ScService{ @Autowired private ScMapper scMapper; @Override public String info(int offset , int rows) { HashMap<String, Object> map = new HashMap<>(); int total = scMapper.infoCount();//总共的记录数 List<Sc> list = scMapper.selectInfo(offset, rows); map.put("total", total); map.put("rows",list); return JSONObject.toJSONString(map); } }
xml代码
<resultMap type="com.example.demo.entity.Sc" id="baseResult"> <result column="s_id" property="s_id" jdbcType="DECIMAL"/> <result column="c_id" property="c_id" jdbcType="DECIMAL"/> <result column="score" property="score" jdbcType="DECIMAL"/> <association property="student" columnPrefix="STUDENT_" resultMap="com.example.demo.mapper.StudentMapper.baseResult"></association> <association property="course" columnPrefix="COURSE_" resultMap="com.example.demo.mapper.CourseMapper.baseResult"></association> </resultMap> <select id="infoCount" resultType="INTEGER"> select count(*) from sc , student , course , teacher where sc.s_id = student.sid and sc.c_id = course.cid and course.t_id = teacher.tid </select> <select id="selectInfo" resultMap="baseResult"> select sc.s_id, sc.c_id, sc.score, student.sname STUDENT_sname, student.sage STUDENT_sage, student.ssex STUDENT_ssex, course.cname COURSE_cname, teacher.tid COURSE_TEACHER_tid, teacher.tname COURSE_TEACHER_tname from sc , student , course , teacher where sc.s_id = student.sid and sc.c_id = course.cid and course.t_id = teacher.tid limit #{offset},#{rows} </select>