欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在MyBatis中實現(xiàn)一對多查詢和多對一查詢的方式詳解(各兩種方式)

 更新時間:2022年01月26日 11:35:27   作者:小羅要有出息  
今天通過兩種方法分別給大家介紹在MyBatis中實現(xiàn)一對多查詢和多對一查詢的方式,每種方式通過實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧

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&amp;useUnicode=true&amp;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)文章

  • Java版仿QQ驗證碼風(fēng)格圖片驗證碼

    Java版仿QQ驗證碼風(fēng)格圖片驗證碼

    這篇文章主要為大家分享了java圖片驗證碼實例代碼,感興趣的小伙伴們可以參考一下
    2016-04-04
  • JAVA回顧:封裝,繼承,多態(tài)

    JAVA回顧:封裝,繼承,多態(tài)

    這篇文章主要介紹了java封裝繼承多態(tài),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • 關(guān)于HashMap 并發(fā)時會引起死循環(huán)的問題解析

    關(guān)于HashMap 并發(fā)時會引起死循環(huán)的問題解析

    JDK1.8之前采用頭插,即在鏈表結(jié)構(gòu)上每次都把數(shù)據(jù)放在鏈表頭部。JDK1.8采用尾插方法,很多朋友在學(xué)習(xí)Java并發(fā)容器和框架時,看到為什么要使用ConcurrentHashMap時不知道究其原因,今天小編通過本文給大家介紹下HashMap 并發(fā)死循環(huán)問題,一起看看吧
    2021-05-05
  • SpringBoot淺析安全管理之Spring Security配置

    SpringBoot淺析安全管理之Spring Security配置

    安全管理是軟件系統(tǒng)必不可少的的功能。根據(jù)經(jīng)典的“墨菲定律”——凡是可能,總會發(fā)生。如果系統(tǒng)存在安全隱患,最終必然會出現(xiàn)問題,這篇文章主要介紹了SpringBoot安全管理Spring Security基本配置
    2022-08-08
  • SpringSecurity 測試實戰(zhàn)

    SpringSecurity 測試實戰(zhàn)

    這篇文章主要介紹了SpringSecurity 測試實戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Java實現(xiàn)動態(tài)生成GIF圖像詳解

    Java實現(xiàn)動態(tài)生成GIF圖像詳解

    在互聯(lián)網(wǎng)上有許多有趣的場景,其中的一種就是動圖。這不是視頻,而是一種GIF圖像信息。本文將利用Java實現(xiàn)動態(tài)生成GIF圖像功能,需要的可以參考一下
    2022-09-09
  • Java語言class類用法及泛化(詳解)

    Java語言class類用法及泛化(詳解)

    這篇文章主要介紹了Java語言class類用法及泛化(詳解),大家都知道Java程序在運行過程中,對所有的對象今夕類型標(biāo)識,也就是RTTI。這項信息記錄了每個對象所屬的類,需要的朋友可以參考下
    2015-07-07
  • 復(fù)雜JSON字符串轉(zhuǎn)換為Java嵌套對象的實現(xiàn)

    復(fù)雜JSON字符串轉(zhuǎn)換為Java嵌套對象的實現(xiàn)

    這篇文章主要介紹了復(fù)雜JSON字符串轉(zhuǎn)換為Java嵌套對象的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Spring Data JPA 復(fù)雜/多條件組合分頁查詢

    Spring Data JPA 復(fù)雜/多條件組合分頁查詢

    本文主要介紹了Spring Data JPA 復(fù)雜/多條件組合分頁查詢的相關(guān)資料。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • Java?System#exit無法退出程序的問題及解決

    Java?System#exit無法退出程序的問題及解決

    這篇文章主要介紹了Java?System#exit無法退出程序的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04

最新評論