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

网友投稿 236 2023-05-21


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

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

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

两个实体类

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;

}

puhttp://blic 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 + ", stSfSWSyudentmajor="

+ 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 students;

public StudentMajor() {

super();

}

public StudentMajor(Integer id, String mcode, String mname, List 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 getStudents() {

return students;

}

public void setStudents(List students) {

this.students = students;

http://}

}

定义两个接口

package com.dao;

import java.util.List;

import java.util.Map;

import com.model.Student;

public interface StudentMapper {

/**

* 全表查询

*/

public List selectall();

  /**

* 根据专业查人员,给一对多用

*/

public List selectz(Integer major);

}

package com.dao;

import java.util.List;

import com.model.StudentMajor;

public interface StudentMajorMapper {

/**

* 全表查询

* @return

*/

public List selectAll();

/**

* 根据主键查数据,给多对一用

* @param id

* @return

*/

public StudentMajor select(Integer id);

}

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

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select="com.dao.StudentMajorMapper.select"/>

select * from student

select * from student s where s.major=#{major}

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select="com.dao.StudentMapper.selectz">

select * from studentmajor

select * from studentmajor where id=#{id}

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(StudentMahttp://jorMapper.class);

}

@After

public void tearDown() throws Exception {

ss.commit();

ss.close();

}

//一对多查询

public void test() {

List list=smm.selectAll();

for(StudentMajor a:list){

System.out.println(a);

}

}

//根据专业查人员,给一对多用

public void selectz(){

List l=sm.selectz(3);

for(Student a:l){

System.out.println(a);

}

}

//多对一查询

@Test

public void selectall() {

List st=sm.selectall();

for(Student tt:st){

System.out.println(tt);

}

}

//根据主键查询,给多对一用

public void select(){

StudentMajor a=smm.select(1);

System.out.println(a);

}

}

一对多查询结果

多对一查询结果

以上所述是给大家介绍的Mybatis 一对多和多对一关联查询问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!

select="com.dao.StudentMajorMapper.select"/>

select * from student

select * from student s where s.major=#{major}

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select="com.dao.StudentMapper.selectz">

select="com.dao.StudentMapper.selectz">

select * from studentmajor

select * from studentmajor where id=#{id}

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(StudentMahttp://jorMapper.class);

}

@After

public void tearDown() throws Exception {

ss.commit();

ss.close();

}

//一对多查询

public void test() {

List list=smm.selectAll();

for(StudentMajor a:list){

System.out.println(a);

}

}

//根据专业查人员,给一对多用

public void selectz(){

List l=sm.selectz(3);

for(Student a:l){

System.out.println(a);

}

}

//多对一查询

@Test

public void selectall() {

List st=sm.selectall();

for(Student tt:st){

System.out.println(tt);

}

}

//根据主键查询,给多对一用

public void select(){

StudentMajor a=smm.select(1);

System.out.println(a);

}

}

一对多查询结果

多对一查询结果

以上所述是给大家介绍的Mybatis 一对多和多对一关联查询问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Boot启动过程完全解析(二)
下一篇:java利用delayedQueue实现本地的延迟队列
相关文章

 发表评论

暂时没有评论,来抢沙发吧~