Mybatis 多对多注解 查询
来源:互联网 发布:淘宝出售虚拟物品 编辑:程序博客网 时间:2024/06/08 09:16
学习mybatis多对多注解,记录下来以便日后查询。
这里多对多关系用到了学生(student)和课程(course),一个学生可以选多门课,一门课可以被多名学生选择,中间表stu_cou.
Student.java
public class Student { private int id; private String name; private Classes classes; private List<Course> courseList; //getter and setter }
Course.java
public class Course { private int id; private String name; private int credit; private List<Student> studentList; //getter and setter }
StudentMapper.java,在其中添加通过courseId查询student的select语句:
@Select("select * from student where id in(select stu_id from stu_cou where cou_id=#{courseId})") @Results({ @Result(id=true,property="id",column="id"), @Result(property="name",column="name"), @Result(property="classes",column="classes_id",javaType=Classes.class, one=@One(select="com.lsj.test.mybatis.annotation.mapper.ClassesMapper.selectClasses",fetchType=FetchType.LAZY)), @Result(property="courseList",column="id",many=@Many(select="com.lsj.test.mybatis.annotation.mapper.CourseMapper.selectCourseByStudent",fetchType=FetchType.LAZY)) }) public List<Student> selectStudentByCourse(int courseId);
CourseMapper.java中添加通过学生Id查询Course的select语句
@Select("select * from course where id in(select cou_id from stu_cou where stu_id=#{studentId})") @Results({ @Result(id=true,property="id",column="id"), @Result(property="name",column="name"), @Result(property="credit",column="credit"), @Result(property="studentList",column="id", many=@Many(select="com.lsj.test.mybatis.annotation.mapper.StudentMapper.selectStudentByCourse",fetchType=FetchType.LAZY)) }) public List<Course> selectCourseByStudent(int studentId);
之后在StudentMapper.java中的select语句中的Result添加many,指向CourseMapper中的通过学生Id查询Course的select语句,同理CourseMapper也是一样的操作指向StudentMapper。
@Select("select * from student where id=#{id}") @Results({ @Result(id=true,property="id",column="id"), @Result(property="name",column="name"), @Result(property="classes",column="classes_id",javaType=Classes.class, one=@One(select="com.lsj.test.mybatis.annotation.mapper.ClassesMapper.selectClasses")),//这里添加many,指向CourseMapper中的selectCourseByStudent查询语句 @Result(property="courseList",column="id",many=@Many(select="com.lsj.test.mybatis.annotation.mapper.CourseMapper.selectCourseByStudent",fetchType=FetchType.LAZY)) }) public Student selectStudent(int id);
阅读全文
0 0
- Mybatis 多对多注解 查询
- mybatis多对多查询
- MyBatis 多对多 查询
- Mybatis(多对多查询)
- mybatis多对一查询
- Mybatis 多对一查询
- Mybatis 注解形式多条件查询
- mybatis(9) 多对多查询
- Mybatis多对多关联查询
- Mybatis多对多关联查询
- Mybatis高级映射多对多查询
- 【Mybatis】---高级映射之一对多查询
- 【Mybatis】---高级映射之多对多查询
- MyBatis之一对多关联查询
- MyBatis之多对多关联查询
- mybatis之一对多查询实例
- mybatis之一对多映射查询(十一)
- mybatis之多对多映射查询(十二)
- grep及正则表达式
- mysql外链、索引、约束
- 线程同步之Semaphore
- linux常用知识点
- [教程]K米商家平台营销中心通用版本短信使用指南
- Mybatis 多对多注解 查询
- Android R 文件丢失
- 乒乓球
- java加锁的意义
- Java疯狂讲义读书笔记第十章
- 数据挖掘系列(3)--关联规则评价
- 新建maven项目
- C语言实现动态顺序表
- 获取Crash信息