欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Mybatis中Collection集合标签的使用详解

时间:2021-03-29 09:42:33|栏目:JAVA代码|点击:

mybatis简单的CURD就不用多说了,网上相关博客文档一大堆。分析一下Mybatis里面的collection聚集查询。
假设一个班级有多名学生为例,通过班级号查询出该班级的信息,和班级里面的所有学生的信息,一般的做法就是通过班级号把班级的信息查询出来,再通过班级ID号把该班级里面的所有学生查询出来,我们不用这种通用的方法

1.班级实体类可以定义为这样:

import java.util.List;

public class ClazzEntity {

  private int clazzID;

  private String clazzName;

  private List<StudentEntity> studentList;

  public int getClassID() {
    return clazzID;
  }

  public int getClazzID() {
    return clazzID;
  }

  public void setClazzID(int clazzID) {
    this.clazzID = clazzID;
  }

  public String getClazzName() {
    return clazzName;
  }

  public void setClazzName(String clazzName) {
    this.clazzName = clazzName;
  }

  public List<StudentEntity> getStudentList() {
    return studentList;
  }

  public void setStudentList(List<StudentEntity> studentList) {
    this.studentList = studentList;
  }
}

学生实体类定义:

package com.cn.hnust.pojo;

public class StudentEntity {

  private int stuID;

  private String stuName;

  private int stuAge;

  private String stuAddress;

  public int getStuID() {
    return stuID;
  }

  public void setStuID(int stuID) {
    this.stuID = stuID;
  }

  public String getStuName() {
    return stuName;
  }

  public void setStuName(String stuName) {
    this.stuName = stuName;
  }

  public int getStuAge() {
    return stuAge;
  }

  public void setStuAge(int stuAge) {
    this.stuAge = stuAge;
  }

  public String getStuAddress() {
    return stuAddress;
  }

  public void setStuAddress(String stuAddress) {
    this.stuAddress = stuAddress;
  }
}

2.数据库建表语句:

CREATE TABLE student_t
(
stuno INT PRIMARY KEY,
stuname VARCHAR(20),
stuage INT,
stuaddress VARCHAR(20) ,
classid INT
);
CREATE TABLE class_t
(
classid INT PRIMARY KEY,
classname VARCHAR(20)
);

3.查询ClazzEntity中的学生信息列表StudentEntity,通过mybatis中的collection标签来配置,其中,ofType是查询返回的学生信息对应的实体类,select为要执行的查询学生列表的查询语句,mybatis的xml配置文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.cn.hnust.dao.InfoManageDao" >

 <resultMap id="ClazzResultMap" type="com.cn.hnust.pojo.ClazzEntity" >
  <id column="classID" property="clazzID" jdbcType="INTEGER" />
  <result column="className" property="clazzName" jdbcType="VARCHAR" />
  <collection property="studentList" column="classID" javaType="ArrayList" 
        ofType="com.cn.hnust.pojo.StudentEntity" select="getStudentByClassID"/>
 </resultMap>

 <resultMap id="StudentResultMap" type="com.cn.hnust.pojo.StudentEntity"> 
  <id property="stuID" column="stuID" /> 
  <result property="stuName" column="stuName" /> 
  <result property="stuAge" column="stuAge" /> 
  <result property="stuAddress" column="stuAddress" />
 </resultMap> 

<select id="getClassByID" resultMap="ClazzResultMap" parameterType="java.lang.Integer" >
  select classID,className
  from class_t
  where classID = #{clazzID}
</select>

<select id="getStudentByClassID" resultMap="StudentResultMap" parameterType="java.lang.Integer" >
  select stuID,stuName,stuAge,stuAddress,classID
  from student_t
  where classID = #{clazzID}
</select>

</mapper>

这样就可以查到一个班级的信息,和班级里面的所有学生信息:

ClazzEntity [clazzID=1, clazzName=junior, studentList=[StudentEntity [stuID=1001, stuName=wanghai, stuAge=18, stuAddress=beijing], StudentEntity [stuID=1002, stuName=zhangdong, stuAge=20, stuAddress=shanghai]]]

上一篇:java操作cookie示例(删除cookie)

栏    目:JAVA代码

下一篇:Hibernate基于ThreadLocal管理Session过程解析

本文标题:Mybatis中Collection集合标签的使用详解

本文地址:http://www.codeinn.net/misctech/90370.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有