在MyBatis中實現(xiàn)一對多查詢和多對一查詢的方式詳解(各兩種方式)
1、多對一
1、1環(huán)境搭建
數(shù)據(jù)庫
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配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/><!--日記log4j-->
</settings>
<typeAliases>
<package name="com.Google.pojo"/><!--給實體類取別名-->
</typeAliases>
<!--<typeAliases>
<typeAlias type="com.Google.pojo.User" alias="user"/>
</typeAliases>-->
<environments default="development"><!--可以創(chuàng)建多個環(huán)境-->
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/><!--加載驅(qū)動-->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/><!--連接數(shù)據(jù)庫-->
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--注冊接口-->
<mappers>
<mapper resource="com/Google/Dao/StudentMapper.xml"/>
<mapper resource="com/Google/Dao/TeacherMapper.xml"/>
</mappers>
</configuration>1、2編寫實體類、
學(xué)生·
@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<Student> getStudentList();
List<Student> getStudentList1();
}1、4編寫Mapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.Google.Dao.StudentMapper">
<!--按照查詢嵌套處理-->
<select id="getStudentList" resultMap="StudentMap">
select * from student
</select>
<resultMap id="StudentMap" type="Student" >
<result property="id" column="id"/>
<result property="name" column="name"/>
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher
</select>
<!--按照結(jié)果嵌套處理-->
<select id="getStudentList1" resultMap="StudentMap1">
select s.id sid,s.name sname,t.name tname
from Student s,Teacher t
where s.tid=t.id
</select>
<resultMap id="StudentMap1" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher" >
<result property="name" column="tname"/>
</association>
</resultMap>
</mapper>1、5實現(xiàn)
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<Student> studentList = mapper.getStudentList();
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<Student> studentList = mapper.getStudentList1();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
}1、6運行結(jié)果
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環(huán)境搭建和一對多一樣
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;
//一個老師擁有多個學(xué)生,給老師創(chuàng)建一個學(xué)生集合
private List<Student> student;
}2、3編寫接口的方法
public interface TeacherMapper {
Teacher getTeacher(@Param("tid") int id);
Teacher getTeacher1(@Param("tid") int id);
}2、4編寫Mapper配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.Google.Dao.TeacherMapper">
<!--按結(jié)果嵌套查詢-->
<select id="getTeacher" resultMap="TeacherStudent">
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>
<resultMap id="TeacherStudent" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<!--ofType="" 用于獲取集合中泛型的信息-->
<collection property="student" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
<!--嵌套表查詢(子查詢)-->
<select id="getTeacher1" resultMap="TeacherStudent1">
select * from teacher where id=#{tid}
</select>
<resultMap id="TeacherStudent1" type="Teacher">
<result property="id" column="id"/>
<collection property="student" javaType="ArrayList" ofType="Student" select="getStudetByID" column="id"/>
</resultMap>
<select id="getStudetByID" resultType="Student">
select * from student where tid=#{tid}
</select>
</mapper>2、5實現(xiàn)
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運行結(jié)果
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)])
到此這篇關(guān)于在MyBatis中實現(xiàn)一對多查詢,和多對一查詢(各兩種方式)的文章就介紹到這了,更多相關(guān)MyBatis一對多查詢和多對一查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot+SpringSecurity實現(xiàn)圖片驗證碼登錄的示例
本文主要介紹了Springboot+SpringSecurity實現(xiàn)圖片驗證碼登錄的示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
java實現(xiàn)字符串匹配求兩個字符串的最大公共子串
這篇文章主要介紹了java實現(xiàn)求兩個字符串最大公共子串的方法,詳細的描述了兩個字符串的最大公共子串算法的實現(xiàn),需要的朋友可以參考下2016-10-10
SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解
這篇文章主要介紹了SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解,ApplicationListener是應(yīng)用程序的事件監(jiān)聽器,繼承自java.util.EventListener標(biāo)準(zhǔn)接口,采用觀察者設(shè)計模式,需要的朋友可以參考下2023-11-11

