日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Mybatis】MyBatis 实现多表查询

發布時間:2024/2/28 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Mybatis】MyBatis 实现多表查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

大綱

Auto Mapping 單表實現(別名方式)
<resultMap>實現單表配置
單個對象關聯查詢(N+1,外連接)
集合對象關聯查詢
注解開發
MyBatis 運行原理


準備:創建數據庫

  • 創建表、錄入測試數據
  • -- 教師表 create table teacher(id int(10) primary key auto_increment,name varchar(20) );insert into teacher values(default,'魚老師'); insert into teacher values(default,'綠老師'); insert into teacher values(default,'酸老師'); insert into teacher values(default,'魔老師');-- 學生表:tid含有外鍵約束 create table student(id int(10) primary key auto_increment,name varchar(20),age int(3),tid int(10),constraint fk_teacher foreign key (tid) REFERENCES teacher(id) );insert into student values(default,'學生1',25,1); insert into student values(default,'學生2',28,1); insert into student values(default,'學生3',24,3); insert into student values(default,'學生4',24,3); insert into student values(default,'學生5',26,2); insert into student values(default,'學生6',25,2); insert into student values(default,'學生7',28,4); insert into student values(default,'學生8',21,4); insert into student values(default,'學生9',21,1); insert into student values(default,'學生10',22,1); insert into student values(default,'學生11',27,1);

    創建結果(學生表、教師表)

    2. 模糊查詢語法where 列 like '%內容%'

    select * from student,teacher where student.name like '%學生%' and tid in (select id from teacher where name like '%魚%') and tid=teacher.id;

    查詢結果:


    課上筆記

    使用JQuery

    關于字符編碼:tomcat默認使用iso-8859-1,網頁中使用utf-8,所以會導致中文亂碼。
    在實際開發中,不要去修改tomcat的全局配置,會誤傷到別人的項目。只能修改自己項目中的編碼方式。如下:


    一、MyBatis 實現多表查詢

  • Mybatis 實現多表查詢方式
    1.1 業務裝配.對兩個表編寫單表查詢語句,在業務(Service)把查詢的兩個結果進行關聯.
    1.2 使用Auto Mapping 特性,在實現兩表聯合查詢時通過別名完成映射.
    1.3 使用MyBatis 的<resultMap>標簽進行實現.
  • 多表查詢時,類中包含另一個類的對象的分類
    2.1 單個對象
    2.2 集合對象.

  • 二、resultMap 標簽

  • <resultMap>標簽寫在mapper.xml 中,由程序員控制SQL 查詢結果與實體類的映射關系.
    1.1 默認MyBatis 使用Auto Mapping 特性.
  • 使用<resultMap>標簽時,<select>標簽不寫 resultType 屬性,而是使用 resultMap 屬性引用<resultMap>標簽.
  • 使用resultMap 實現單表映射關系
    3.1 數據庫設計

    3.2 實體類設計
  • public class Teacher{private int id1;private String name1;}

    3.3 mapper.xml 代碼

    <resultMap type="teacher" id="mymap"><!-- 主鍵使用id 標簽配置映射關系--><id column="id" property="id1" /><!-- 其他列使用result 標簽配置映射關系--><result column="name" property="name1"/> </resultMap><select id="selAll" resultMap="mymap">select * from teacher </select>

    4. 使用resultMap實現關聯單個對象(N+1 方式)
    4.1 N+1 查詢方式,先查詢出某個表的全部信息,根據這個表的信息查詢另一個表的信息.
    4.2 與業務裝配的區別:
    4.3.1 在 service 里面寫的代碼,由 mybatis 完成裝配
    4.3 實現步驟:
    4.3.1 在 Student 實現類中包含了一個Teacher 對象

    public class Student {private int id;private String name;private int age;private int tid;private Teacher teacher;...

    4.3.2 在TeacherMapper 中提供一個查詢

    <select id="selById" resultType="teacher" parameterType="int">select * from teacher where id=#{0} </select>

    4.3.3 在StudentMapper 中
    4.3.3.1 <association> 裝配一個對象時使用
    4.3.3.2 property: 對象在類中的屬性名
    4.3.3.3 select:通過哪個查詢查詢出這個對象的信息
    4.3.3.4 column: 把當前表的哪個列的值做為參數傳遞給另一個查詢
    4.3.3.5 大前提使用N+1 方式.時如果列名和屬性名相同可以不配置,使用Auto mapping 特性.但是mybatis 默認只會給列
    專配一次

    5. 使用resultMap 實現關聯單個對象(聯合查詢方式)
    5.1 只需要編寫一個SQL,在StudentMapper 中添加下面效果
    5.1.1 只要專配一個對象就用這個標簽
    5.1.2 此時把<association/>小的<resultMap>看待
    5.1.3 javaType 屬性:<association/>專配完后返回一個什么類型的對象.取值是一個類(或類的別名)

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.bjsxt.mapper.StudentMapper"><resultMap type="Student" id="stuMap"><result column="tid" property="tid"/><!-- 如果關聯一個對象 --><association property="teacher" select="com.bjsxt.mapper.TeacherMapper.selById" column="tid"></association></resultMap><select id="selAll" resultMap="stuMap">select * from student</select><resultMap type="Student" id="stuMap1"><!-- column是數據庫查詢語句起的別名,property是javabean對象屬性名稱.id是主鍵,result是普通鍵 --><id column="sid" property="id"/><result column="sname" property="name"/><result column="age" property="age"/><result column="tid" property="tid"/><association property="teacher" javaType="Teacher" ><id column="tid" property="id"/><result column="tname" property="name"/></association></resultMap><select id="selAll1" resultMap="stuMap1">select s.id sid,s.name sname,age age,t.id tid,t.name tname FROM student s left outer join teacher t on s.tid=t.id</select> </mapper>

    test.java

    package com.bjsxt.test;import java.io.IOException; import java.io.InputStream; import java.util.List;import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder;import com.bjsxt.pojo.Student;public class Test {public static void main(String[] args) throws IOException { InputStream is = Resources.getResourceAsStream("mybatis.xml");SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);SqlSession session = factory.openSession();List<Student> list = session.selectList("com.bjsxt.mapper.StudentMapper.selAll");System.out.println(list);List<Student> list1 = session.selectList("com.bjsxt.mapper.StudentMapper.selAll1");System.out.println(list1);session.close();System.out.println("程序執行結束");} }
  • N+1 方式和聯合查詢方式對比
    6.1 N+1:需求不確定時.
    6.2 聯合查詢:需求中確定查詢時兩個表一定都查詢.
  • N+1 名稱由來
    7.1 舉例:學生中有3 條數據
    7.2 需求:查詢所有學生信息級授課老師信息
    7.3 需要執行的SQL 命令
    7.3.1 查詢全部學生信息:select * from 學生
    7.3.2 執行3 遍select * from 老師where id=學生的外鍵
    7.4 使用多條SQl 命令查詢兩表數據時,如果希望把需要的數據都查詢出來,需要執行N+1 條SQl 才能把所有數據庫查詢出來.
    7.5 缺點:
    7.5.1 效率低
    7.6 優點:
    7.6.1 如果有的時候不需要查詢學生是同時查詢老師.只需要執行一個select * from student;
    7.7 適用場景: 有的時候需要查詢學生同時查詢老師,有的時候只需要查詢學生.
    7.8 如果解決N+1 查詢帶來的效率低的問題
    7.8.1 默認帶的前提: 每次都是兩個都查詢.
    7.8.2 使用兩表聯合查詢.

  • 三.使用查詢關聯集合對象(N+1)

  • 在Teacher 中添加List<Student>
  • public class Teacher {private int id;private String name;private List<Student> list;
  • 在StudentMapper.xml 中添加通過 tid 查詢
  • <select id="selByTid" parameterType="int" resultType="student">select * from student where tid=#{0} </select>
  • 在TeacherMapper.xml 中添加查詢全部
    3.1 <collection/> 當屬性是集合類型時使用的標簽.
  • <resultMap type="teacher" id="mymap"><id column="id" property="id"/><result column="name" property="name"/><collection property="list" select="com.bjsxt.mapper.StudentMapper.selByTid" column="id"></collection> </resultMap><select id="selAll" resultMap="mymap">select * from teacher </select>

    總結

    以上是生活随笔為你收集整理的【Mybatis】MyBatis 实现多表查询的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。