Mybatis查询多对一,一对多Mapper语句(返回集合,返回对象)

Mybatis查询多对一,一对多Mapper语句(返回集合,返回对象)

1.多对一

多个学生对应一个老师

学生类:

Student{
	private int id;
	private String name;
	private Teacher teacher;
}

学生数据表:

idnametid
1小明1
2小刚1

方法一:按照查询嵌套处理:

<select id = getStudent resultMap="StudentTeacher">
	select * from student
</select>

<resultMap id = "StudentTeacher" type = "Student">
	<result property="id" column="id"/>
	<result property="name" column="name"/>
	<!-- association用于处理对象,property代表这个对象,
	column只数据库字段名,用字段名来根据方法来获取对象,
	javaType为该对象所属类 -->
	<association property="teacher" column="tid" javaType="Teacher" select="getTcherById" />
</resultMap>

<select id = "getTcherById">
	select * from teacher where id = #{id}
</select>

方法二(推荐):按照结果嵌套查询(联表查询):

<select id = "getStudent2" resultMap = "StudentTeacher2">
	select s.id sid,s.name sname,t.name tname
    from student s,teacher t
    where s.tid = t.id;
</select>

<resultMap id="StudentTeacher2" type = "Student">
	<result property = "id" column = "sid">
	<result property = "name" column = "sname"
	<association property = "teacher" javaType = "Teacher">
		<property = "name" column = " tname">
	</association>
</resultMap>

2.多对一

一个老师对应多个学生

学生类:

Student{
	private int id;
	private String name;
	private Teacher teacher;
}

老师类:

Teacher{
	private int id;
	private String name;
	private List<Student> students;
}

方法1:按照结果嵌套处理(联表查询):

<select id = "getTeacher" resultMap = "TeacherStudent">
	select s.id sid, s.name sname, t.name tname,t.id tid
    from student s,teacher t
    where s.tid = t.id and t.id = #{tid}
</select>

<resultMap id = "TeacherStudent" ofType = "Student">
	<result property = "id" column = "tid">
	<result property = "name" column = "tname">
	<!-- collection表示集合,ofType表示students泛型类 -->
	<collection property = "students" ofType = "Student">
		<result property = "id" column = "sid">
		<result property = "name" column = "sname">
		<result property = "tid" column = "tid">
	</collection>
</resultMap>

方法2:按照查询嵌套处理

<select id = "getTeacher2" resultMap="TeacherStudent2">
	select * from mybatis.teacher where id = #{tid}
</select>

<resultMap id="TeacherStudent2" type="Teacher">
	<!--这里省略id,name对应关系-->
    <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
    select * from mybatis.student where tid = #{tid}
</select>

3.javaType & ofType

	1.JavaType 用来指定实体类中属性的类型
	2.ofType 用来指定映射到List或者集合中的 pojo类型,泛型中的约束类型!
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis Plus提供了多种方式来实现多表一对多查询,以下是其中一种常用的方法: 1. 使用@ManyToOne注解和@OneToMany注解来建立一对多关系。 ```java // 父表实体类 public class ParentEntity { // 父表主键 @TableId private Long id; // 父表其他属性 // 子表集合 @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) private List<ChildEntity> children; // getter和setter方法 } // 子表实体类 public class ChildEntity { // 子表主键 @TableId private Long id; // 子表其他属性 // 父表外键 @ManyToOne @JoinColumn(name = "parent_id") private ParentEntity parent; // getter和setter方法 } ``` 2. 在Mapper接口中使用@Select注解来编写SQL查询语句。 ```java @Mapper public interface ParentMapper extends BaseMapper<ParentEntity> { @Select("SELECT * FROM parent WHERE id = #{id}") @Results({ @Result(property = "id", column = "id"), // 其他属性映射 @Result(property = "children", column = "id", many = @Many(select = "com.example.ChildMapper.getByParentId")) }) ParentEntity getById(Long id); } @Mapper public interface ChildMapper extends BaseMapper<ChildEntity> { @Select("SELECT * FROM child WHERE parent_id = #{parentId}") List<ChildEntity> getByParentId(Long parentId); } ``` 3. 在Service层调用Mapper接口的方法来实现查询。 ```java @Service public class ParentService { @Autowired private ParentMapper parentMapper; public ParentEntity getById(Long id) { return parentMapper.getById(id); } } ``` 通过以上步骤,你可以实现MyBatis Plus的多表一对多查询。你可以根据具体的业务需求来调整实体类和SQL查询语句

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值