多对多<ofType属性>

多对多关联查询

一个学生可以选修多门课程,一门课程也可以被多个学生选修。

假设有三个实体类Student、Course和StudentCourse,其中Student和Course是主实体类,StudentCourse是关联表。

public class Student {
    private Integer id;
    private String name;
    private List<Course> courses; // 多对多关联,使用List集合保存选修的课程对象

    // Getters and setters (省略)...
}

public class Course {
    private Integer id;
    private String name;
    private List<Student> students; // 多对多关联,使用List集合保存选修该课程的学生对象

    // Getters and setters (省略)...
}

public class StudentCourse {
    private Integer studentId;
    private Integer courseId;

    // Getters and setters (省略)...
}

Mapper接口StudentMapper中,定义多对多关联查询的方法:

javaCopy code
public interface StudentMapper {
    Student getStudentWithCoursesById(Integer id);
}

在MyBatis的映射文件StudentMapper.xml中,使用****标签来进行多对多关联查询:

<!-- 定义Course的结果映射 -->
<resultMap id="courseMap" type="Course">
  <id property="id" column="course_id"/>
  <result property="name" column="course_name"/>
  <!-- 其他Course属性的映射... -->
</resultMap>

<!-- 定义Student的结果映射,包含对Course对象的集合映射 -->
<resultMap id="studentMap" type="Student">
  <id property="id" column="student_id"/>
  <result property="name" column="student_name"/>
  <!-- 其他Student属性的映射... -->
  <collection property="courses" ofType="Course" resultMap="courseMap"/>
</resultMap>

<!-- 定义查询语句,通过resultMap属性引用studentMap结果映射 -->
<select id="getStudentWithCoursesById" resultMap="studentMap">
  SELECT s.id as student_id, s.name as student_name,
  c.id as course_id, c.name as course_name
  FROM students s
  LEFT JOIN student_course sc ON s.id = sc.student_id
  LEFT JOIN courses c ON sc.course_id = c.id
  WHERE s.id = #{id}
</select>
  • 定义了Course类的结果映射courseMap
  • 定义了Student类的结果映射studentMap
  • 在studentMap结果映射中,使用标签指定了属性courses与结果映射courseMap的嵌套关联
  • 通过ofType属性指定集合中元素的类型为Course
<collection property="propertyName" ofType="elementType" resultMap="resultMapId"/>

常用属性:

  • property:指定Java对象中的属性名,用于表示关联对象的集合属性。
  • ofType:指定集合中元素的类型,即关联对象的类型。ofType属性在多对多关联中使用
  • resultMap:指定关联对象的结果映射id,即关联对象的标签的id。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在这段代码中,`rows` 是通过循环判断计算得出的一个值,我们可以使用计算属性来获取它。 首先,在组件对象中定义一个计算属性 `rows`,在该属性的 getter 函数中进行循环判断并返回对应的值。例如: ```js import { defineComponent } from 'petite-vue'; const myComponent = defineComponent({ computed: { rows() { let count = 0; for (let item of this.data.list) { if (item.status === '1' && item.type === '附件') { count++; } } return count; } }, methods: { // ... }, data() { return { // ... }; } }); ``` 在上面的例子中,我们通过循环遍历 `data.list` 数组,统计满足条件的元素个数,并将其作为计算属性 `rows` 的值返回。 接下来,我们可以在模板中使用 `rows` 计算属性来设置 `<td>` 元素的 `rowspan` 属性。例如: ```html <table> <colgroup> <col width="20%"> <col width="40%"> <col width="40%"> </colgroup> <th> <td :colspan="rows">附件</td> </th> <tr> <td :rowspan="rows">附件</td> <td>名称</td> <td>数量</td> </tr> <tr v-for="(item, index) in data.list" v-show="item.status === '1' && item.type === '附件'"> <td><input type="text" v-model="item.info.名称"></td> <td><input type="text" v-model="item.info.数量"></td> <td><input type="button" value="删除" @click="del(index)"></td> </tr> <tr> <td colspan="3"> <input type="button" value="添加" @click="add3"> </td> </tr> </table> ``` 在上面的例子中,我们使用 `:rowspan="rows"` 将 `<td>` 元素的 `rowspan` 属性绑定到计算属性 `rows` 上,这样它的值就会自动更新并反映到模板中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值