洞察纵观鸿蒙next版本,如何凭借FinClip加强小程序的跨平台管理,确保企业在数字化转型中的高效运营和数据安全?
671
2022-11-04
在MyBatis中实现一对多查询和多对一查询的方式详解(各两种方式)
目录1、多对一1、1环境搭建1、2编写实体类、1、3编写接口方法1、4编写Mapper1、5实现1、6运行结果2、一对多2、1环境搭建和一对多一样2、3编写接口的方法2、4编写Mapper配置2、5实现2、6运行结果
1、多对一
1、1环境搭建
数据库
CREATE TABLE teacher (
id INT(10) NOT NULL,
NAME VARCHAR(64) DEFAULT NULL,
PRIMARY KEY (id),
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO teacher (id ,NAME) VALUES (1,'罗老师');
CREATE TABLE student (
id INT(10) NOT NULL,
NAME VARCHAR(64) DEFAULT NULL,
tid INT(10) DEFAULT NULL,
PRIMARY KEY (id),
KEY fktid (tid),
CONSTRAINT fktid FOREIGN KEY (tid) REFERENCES teacher (id)
)
ALTER TABLE student ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO student (id ,NAME ,tid) VALUES (1,'小明',1);
INSERT INTO student (id ,NAME ,tid) VALUES (2,'小红',1);
INSERT INTO student (id ,NAME ,tid) VALUES (3,'小张',1);
INSERT INTO student (id ,NAME ,tid) VALUES (5,'小罗',1);
MyBatis.xml配置
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
1、2编写实体类、
学生·
@Data
public class Student {
private int id;
private String name;
private Teacher teacher;
}
老师·
@Data
public class Teacher {
private int id;
private String name;
}
1、3编写接口方法
public interface StudentMapper {
List
List
}
1、4编写Mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from student
select * from teacher
select s.id sid,s.name sname,t.name tname
from Student s,Teacher t
where s.tid=t.id
1、5实现
package com.Google.Dao;
import com.Google.pojo.Student;
import com.Google.units.sqlSessionFactory;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class StudentMapperText {
@Test
public void getStudent(){
SqlSession sqlSession = sqlSessionFactory.getsqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
@Test
public void getStudent1(){
SqlSession sqlSession = sqlSessionFactory.getsqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
}
1、6运行结果
Student(id=1, name=小明, teacher=Teacher(id=0, name=罗老师))Student(id=2, name=小红, teacher=Teacher(id=0, name=罗老师))Student(id=3, name=小张, teacher=Teacher(id=0, name=罗老师))Student(id=4, name=小王, teacher=Teacher(id=0, name=罗老师))Student(id=5, name=小罗, teacher=Teacher(id=0, name=罗老师))
2、一对多
2、1环境搭建和一对多一样
2、2编写实体类
@Data
public class Student {
private int id;
private String name;
private int tid;
}
@Data
public class Teacher {
private int id;
private String name;
//一个老师拥有多个学生,给老师创建一个学生集合
private List
}
2、3编写接口的方法
public interface TeacherMapper {
Teacher getTeacher(@Param("tid") int id);
Teacher getTeacher1(@Param("tid") int id);
}
2、4编写Mapper配置
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select s.id sid, s.name sname, t.id tid, t.name tname
from student s,
teacher t
where s.tid = t.id
and t.id = #{tid}
select * from teacher where id=#{tid}
select * from student where tid=#{tid}
2、5实现
public class TeacherMapperText {
@Test
public void getTeacher(){
SqlSession sqlSession = sqlSessionFactory.getsqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
@Test
public void getTeacher1(){
SqlSession sqlSession = sqlSessionFactory.getsqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher1 = mapper.getTeacher1(1);
System.out.println(teacher1);
sqlSession.close();
}
}
2、6运行结果
Teacher(id=1, name=罗老师, student=[Student(id=1, name=小明, tid=1), Student(id=2, name=小红, tid=1), Student(id=3, name=小张, tid=1), Student(id=4, name=小王, tid=1), Student(id=5, name=小罗, tid=1)])
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~