源码下载:
https://gitee.com/qq1350048638/lj_ovls_course_server
针对多表中的查询操作。
course表–>课程信息Course
chapter表–>章节信息Chapter
表之间采用字段建立关系 course.id=chapter.course_id
Chapter表
项目结构
lj_ovls_course_server
主要以 course 课程 和 Chapter 章节 一对多 为例
Course实体类 增加 List chapters
/**
* 加载关联章节信息
*/
private List<Chapter> chapters;
...get set 方法省略
CourseMapper.java 增加
增加一个 查询方法 selectDetailByPrimaryKey
注:CourseMapper 是由 generator自动生成,此处增加一对多查询关系 手动增加即可;
/**
* 根据课程ID查询课程和章节信息
* @mbggenerated Fri Dec 22 10:23:10 CST 2017
*/
@Select({
"select",
"id, name, intro, learn_count, difficulty, score, image, publish_time, price, ",
"subject_id, direction_id, learn_time",
"from course",
"where id = #{id,jdbcType=INTEGER}"
})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
@Result(column="intro", property="intro", jdbcType=JdbcType.VARCHAR),
@Result(column="learn_count", property="learnCount", jdbcType=JdbcType.INTEGER),
@Result(column="difficulty", property="difficulty", jdbcType=JdbcType.VARCHAR),
@Result(column="score", property="score", jdbcType=JdbcType.INTEGER),
@Result(column="image", property="image", jdbcType=JdbcType.VARCHAR),
@Result(column="publish_time", property="publishTime", jdbcType=JdbcType.TIMESTAMP),
@Result(column="price", property="price", jdbcType=JdbcType.INTEGER),
@Result(column="subject_id", property="subjectId", jdbcType=JdbcType.INTEGER),
@Result(column="direction_id", property="directionId", jdbcType=JdbcType.INTEGER),
@Result(column="learn_time", property="learnTime", jdbcType=JdbcType.INTEGER),
@Result(property="chapters",javaType=List.class,column="id",
many=@Many(select="com.lj.ovls.course.dao.ChapterMapper.selectByCourseId",
fetchType=FetchType.EAGER))
})
Course selectDetailByPrimaryKey(Integer id);
注:
property 就是刚才在course里面增加的 chapters;
javaType 是 list集合;
many=@Many 加载多个元素;
select=”com.lj.ovls.course.dao.ChapterMapper.selectByCourseId” 查询 .ChapterMapper类里面的方法selectByCourseId;
fetchType=FetchType.EAGER 饥饿加载,查course时立刻就调用查询;
ChapterMapper.java 里面的查询语句
/**
* 根据课程查询章节信息
* @mbggenerated Fri Dec 22 10:23:10 CST 2017
*/
@Select({
"select",
"id, name, course_id",
"from chapter",
"where course_id = #{cid,jdbcType=INTEGER}"
})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
@Result(column="course_id", property="courseId", jdbcType=JdbcType.INTEGER)
})
List<Chapter> selectByCourseId(Integer cid);
CourseServiceImpl 类
获取到的数据存到ResponseResult result 里面 返回给前端 遍历出来
@Service
public class CourseServiceImpl implements CourseService
@Autowired
private CourseMapper courseDao;
@Override
public ResponseResult loadFreeCourse(int size) {
PageHelper.startPage(1,size);
List<Course> list = courseDao.selectFreePrice();
ResponseResult result = new ResponseResult();
if(list.isEmpty()){
result.setStatus(2);
result.setMsg("未找到记录");
}else{
result.setStatus(1);
result.setMsg("查询成功");
result.setData(list);
}
//System.out.println(result+"---loadFreeCourse----");
return result;
}
前端course.jsp
ajax代码
$.ajax({
url:"http://localhost:8882/course/"+id,
type:"get",
dataType:"json",
success:function(result){
if(result.status==1){//有结果
var course = result.data;
//将课程信息添加到显示位置
$("#s_name").html(course.name);
$("#s_intro").html(course.intro);
$("#s_learnCount").html(course.learnCount);
$("#s_difficulty").html(course.difficulty);
$("#s_score").html(course.score);
$("#s_learnTime").html(course.learnTime);
//循环章节信息,生成章节列表 获取到的章节信息 循环遍历出来,再展示出来
var chapters = course.chapters;//章节结合
for(var i=0;i<chapters.length;i++){
var chapter_id = chapters[i].id;
var chapter_name = chapters[i].name;
}
}
}
});
多对一加载
根据一个Chapter章节对象加载一个Course课程对象
在Chapter实体类中添加Course课程对象属性
在Chapter查询SQL部分,使用@Result和@One注解描述SQL加载
@Result(property="course",column="course_id",
one=@One(select="cn.xdl.ovls.course.dao.CourseMapper.selectByPrimaryKey",
fetchType=FetchType.EAGER)