欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mybatis 一对多和多对一关联查询问题

程序员文章站 2024-02-29 13:49:34
首先  数据库量表之间字段关系(没有主外键) studentmajor表的id字段对应student表里major字段 两个实体类 packag...

首先  数据库量表之间字段关系(没有主外键)

studentmajor表的id字段对应student表里major字段

Mybatis 一对多和多对一关联查询问题

两个实体类

package com.model;
import java.util.date;
public class student {
  private integer sno;
  private string sname;
  private string ssex;
  private integer sclass;
  private studentmajor studentmajor;
  public student() {
    super();
  }
  public student(integer sno, string sname, string ssex, integer sclass, studentmajor studentmajor) {
    super();
    this.sno = sno;
    this.sname = sname;
    this.ssex = ssex;
    this.sclass = sclass;
    this.studentmajor = studentmajor;
  }
  public studentmajor getstudentmajor() {
    return studentmajor;
  }
  public void setstudentmajor(studentmajor studentmajor) {
    this.studentmajor = studentmajor;
  }
  public integer getsno() {
    return sno;
  }
  public void setsno(integer sno) {
    this.sno = sno;
  }
  public string getsname() {
    return sname;
  }
  public void setsname(string sname) {
    this.sname = sname;
  }
  public string getssex() {
    return ssex;
  }
  public void setssex(string ssex) {
    this.ssex = ssex;
  }
  @override
  public string tostring() {
    return "student [sno=" + sno + ", sname=" + sname + ", ssex=" + ssex + ", sclass=" + sclass + ", studentmajor="
        + studentmajor + "]";
  }
  public integer getsclass() {
    return sclass;
  }
  public void setsclass(integer sclass) {
    this.sclass = sclass;
  }
}
package com.model;
import java.util.list;
public class studentmajor {
  private integer id;
  private string mcode;
  private string mname;
  private list<student> students;
  public studentmajor() {
    super();
  }
  public studentmajor(integer id, string mcode, string mname, list<student> students) {
    super();
    this.id = id;
    this.mcode = mcode;
    this.mname = mname;
    this.students = students;
  }
  @override
  public string tostring() {
    return "studentmajor [id=" + id + ", mcode=" + mcode + ", mname=" + mname + ", students=" + students + "]";
  }
  public integer getid() {
    return id;
  }
  public void setid(integer id) {
    this.id = id;
  }
  public string getmcode() {
    return mcode;
  }
  public void setmcode(string mcode) {
    this.mcode = mcode;
  }
  public string getmname() {
    return mname;
  }
  public void setmname(string mname) {
    this.mname = mname;
  }
  public list<student> getstudents() {
    return students;
  }
  public void setstudents(list<student> students) {
    this.students = students;
  }
}

定义两个接口

package com.dao;
import java.util.list;
import java.util.map;
import com.model.student;
public interface studentmapper {
  /**
   * 全表查询
   */
  public list<student> selectall();
  /**
   * 根据专业查人员,给一对多用
   */
  public list<student> selectz(integer major);
}
package com.dao;
import java.util.list;
import com.model.studentmajor;
public interface studentmajormapper {
  /**
   * 全表查询
   * @return
   */
  public list<studentmajor> selectall();
  /**
   * 根据主键查数据,给多对一用
   * @param id
   * @return
   */
  public studentmajor select(integer id);
}

定义两个实体类的映射方法

<?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.dao.studentmapper">
  <!-- 多对一查询 -->
  <resultmap type="student" id="slist">
    <!-- 跟一对一一样用association标签,实体类定义的成员,要跟数据库字段名对应上 -->
    <association property="studentmajor" column="major" 
    select="com.dao.studentmajormapper.select"/> <!-- 用接口里定义的方法,根据student表中的major字段查出对应数据 -->
  </resultmap>
  <!-- 查全部 -->
  <select id="selectall" resultmap="slist" >
    select * from student
  </select>
  <!-- 根据专业查人员 -->
  <select id="selectz" parametertype="integer" resulttype="student">
    select * from student s where s.major=#{major}
  </select>
 </mapper>
<?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.dao.studentmajormapper">
   <!-- 一对多查询关联 -->
   <resultmap type="studentmajor" id="slist">
     <!-- 实体类属性对应数据库的主键字段,不然主键会查不到 -->
     <id property="id" column="id"/>
     <!-- 用collection标签 ,也是实体类属性要对应数据库字段-->
     <collection property="students" column="id"
     select="com.dao.studentmapper.selectz">
     </collection>  
   </resultmap>
   <!-- 全表查询 -->
   <select id="selectall" resultmap="slist">
     select * from studentmajor
   </select>
   <!-- 根据主键查 -->
   <select id="select" parametertype="integer" resulttype="studentmajor">
     select * from studentmajor where id=#{id}
   </select>
 </mapper>

junit测试

package com.util;
import java.util.list;
import org.apache.ibatis.session.sqlsession;
import org.junit.after;
import org.junit.before;
import org.junit.test;
import com.dao.studentmajormapper;
import com.dao.studentmapper;
import com.model.student;
import com.model.studentmajor;
public class jjjtest {
  private sqlsession ss;
  private studentmapper sm;
  private studentmajormapper smm;
  @before
  public void setup() throws exception {
    ss=sqlsessionutil.getsqlsession();
    sm=ss.getmapper(studentmapper.class);
    smm=ss.getmapper(studentmajormapper.class);
  }
  @after
  public void teardown() throws exception {
    ss.commit();
    ss.close();
  }
  //一对多查询
  public void test() {
    list<studentmajor> list=smm.selectall();
    for(studentmajor a:list){
      system.out.println(a);
    }
  }
  //根据专业查人员,给一对多用
  public void selectz(){
    list<student> l=sm.selectz(3);
    for(student a:l){
      system.out.println(a);
    }
  }
  //多对一查询
  @test
  public void selectall() {
    list<student> st=sm.selectall();
    for(student tt:st){
      system.out.println(tt);
    }
  }
  //根据主键查询,给多对一用
  public void select(){
    studentmajor a=smm.select(1);
    system.out.println(a);
  }
}

一对多查询结果

Mybatis 一对多和多对一关联查询问题

多对一查询结果

Mybatis 一对多和多对一关联查询问题

以上所述是小编给大家介绍的mybatis 一对多和多对一关联查询问题,希望对大家有所帮助