mybatis中实现多表查询

axu521      2022-04-21     619

关键词:

如果查询的数据量大,推荐使用N+1次查询。数据量少使用联合查询。。。

一、

1、Mybatis是实现多表查询方式

  1.1  业务装配:对两个表编写单表查询语句,在业务(Service)把查询的两表结果合并

  1.2  使用Auto Mapping 特性,在实现两表联合查询时通过别名完成映射

  1.3  使用MyBatis<resultMap>属性进行实现

2、多表查询时,类中包含另一个类的对象的分类

  2.1 单个对象

  2.2 集合对象

二、resultMap属性

  1、<resultMap>标签写在mapper.xml中,由程序员控制SQL查询结果与实体类的映射关系。

    1.2 默认MyBatis使用Auto Mapping特性

  2、使用<resultMap> 标签时,<select>标签不写resultType属性,而是使用resultMap属性 引用<resultMap>标签

  3、使用resultMap实现单表映射关系

    3.1 数据库设计

  

     3.2 实体类设计

    

      3.3 xxxmapper.xml代码

 1  <mapper namespace="com.bjsxt.mapper.TeacherMapper">
 2      <resultMap type="Teacher" id="mymap">
 3          <!-- 主键使用id标签配置映射关系-->
 4          <id column="id" property="id1"/>
 5          <!-- 其他列使用result标签配置映射关系 -->
 6          <result  column="name" property="name1"/>
 7      </resultMap>
 8      <select id="selall"  resultMap="mymap">
 9          select * from teacher
10      </select>
11  </mapper>

 三使用resultMap 实现关联单个对象(N+1方式)

    4.1  N+1 查询方式,先查询出某个表的全部信息,根据这个表的信息查询另一个表的信息

    4.2 与业务装配的区别:

      4.2.1 之前在service里面写代码,现在由mybatis完成装配

    4.3 实现步骤:

      4.3.1 在Student 实现类中包含了一个Teacher 对象

      

      4.3.2 在TeacherMapper 中提供一个查询

1  <mapper namespace="com.bjsxt.mapper.TeacherMapper">
2      <select id="selById" resultType="teacher" parameterType="int" >
3          select * from teacher where id=#{0}
4      </select>        
5  </mapper>

      4.3.3 在StudentMapper中

          4.3.3.1 <association> 装配一个对象时使用(包含在student中的对象)

          4.3.3.2 property 关联对象

          4.3.3.3 select 通过哪个查询查询出这个对象的信息

          4.3.3.4 column 把当前表的哪个列的值作为参数传递给另一个查询

          4.3.3.5  大前提使用N+1方式时,如果列名和属性名相同可以不配置,使用Auto mapping特性,但是mybatis默认只会给列专配一次。。

  <mapper namespace="com.bjsxt.mapper.StudentMapper">
      <resultMap type="student" id="stumap">
          <id property="id" column="id"/>
          <result property="name" column="name"/>
          <result property="age" column="age"/>
          <result property="tid" column="tid"/>
          <!-- 如果关联一个对象 -->
          <association property="teacher" select="com.bjsxt.mapper.TeacherMapper.selById" column="tid"></association>
      </resultMap>
          <select id="selall"  resultMap="stumap">
              select * from student
          </select>
  </mapper>

          4.3.3.6 可以把代码简化

  <mapper namespace="com.bjsxt.mapper.StudentMapper">
      <resultMap type="student" id="stumap">
          <result property="tid" column="tid"/>
          <!-- 如果关联一个对象 -->
          <association property="teacher" select="com.bjsxt.mapper.TeacherMapper.selById" column="tid"></association>
          
      </resultMap>
          <select id="selall"  resultMap="stumap">
              select * from student
          </select>
  </mapper>

 四、使用<resultMap>查询关联集合对象(N+1)

   1、在Teacher 中添加List<Student>

    

   2、 在StudentMapper.xml中添加通过tid查询

  <mapper namespace="com.bjsxt.mapper.StudentMapper">
      <select id="selByTid" parameterType="int" resultType="student">
          select * from student where tid=#{0}
      </select> 
  </mapper>

   3、 在TeacherMapper.xml 中添加查询全部

      3.1、<collection/> 当属性是集合类型时使用的标签

 <mapper namespace="com.bjsxt.mapper.TeacherMapper">
         <resultMap type="teacher" id="mymap">
              <id column="id" property="id"/>
              <result column="name" property="name"/>
              <collection property="list" ofType="student" select="com.bjsxt.mapper.StudentMapper.selByTid" column="id"></collection>
          </resultMap>
          <select id="selAll" resultMap="mymap">
            select * from teacher              
          </select>
 </mapper>

 

 五、使用<resultMap> 实现加载集合数据(联合查询方式)

    1、在teacherMapper.xml中添加

      1.1 mybatis可以通过主键判断对象是否被加载过

        不需要担心创建重复 teacher

 <mapper namespace="com.bjsxt.mapper.TeacherMapper">        
 
          <resultMap type="teacher" id="mymap">
              <id property="id" column="tid"/>
              <result property="name" column="tname"/>
              <collection property="list" ofType="student" >
                  <id property="id" column="sid"/>
                  <result property="name" column="sname"/>
                  <result property="age" column="age"/>
                  <result property="tid" column="tid"/>
              </collection>
          </resultMap>
          
          <select id="selAll" resultMap="mymap">
            select t.id tid,t.name tname,s.id sid,s.name sname,age,tid from teacher t left join student s on t.id=s.tid              
          </select>
 </mapper>

 

 六、 使用 Auto Mapping结合别名实现多表查询

  6.1 只能使用多表联合查询方式

  6.2 要求:查询出的列和属性名相同

  6.3  实现方式

    6.3.1  . 在SQL是关键字符,两侧添加返单引号

  <mapper namespace="com.bjsxt.mapper.StudentMapper">
      <select id="selAll" resultType="student">
      select t.id `teacher.id`,t.name `teacher.name`,s.id id,s.name name,age,tid 
          from student s left join teacher t on s.tid=t.id;
      </select>
  </mapper>

    

    注意点:::使用Auto Mapping 特性查询集合不好用 !!!!

 

七、 使用注解查询

  1、注解:为了简化配置文件

  2、Mybatis 的注解简化 xxxmapper.xml文件

    2.1 如果涉及动态SQL 依然使用 xxxmapper.xml 

   3、xxxmapper.xml 和注解可以共存

   4、使用注解时 Mybatis.xml 中<mappers> 使用

    4.1 <package/>

    4.2 <mapper class="  "/>

    5、实现查询

	@Select("select * from teacher")
	List<Teacher> selAll();

   6、实现插入

	@Insert("insert into teacher values(default,#{name})")
	int inTeacher(Teacher teacher);

   7、实现修改

	@Update("update teacher set name=#{name} where id=#{id}")
	int upTeacher(Teacher teacher);

   8、实现删除

	@Delete("delete from teacher where id=#{0}")
	int delTeacher(int id);

   9、使用注解实现 <resultMap> 功能

    9.1、以 N+1 举例

    9.2、在StudentMapper 接口添加查询

@Select ("select * from student where tid=#{0}")
 List<Student> selByTid(int tid);

       9.3、在TeacherMapper 接口添加

      9.3.1 @Results() 相当于 <resultMap>

      9.3.2 @Result() 相当于<id/> 或 <result/>

        9.3.2.1 @Result(id=true)  相当于<id/>

      9.3.3 @Many() 相当于<collection/>

      9.3.4 @One() 相当于<association/>

@Results(value={ 
    @Result(id=true,property="id",column="id"),
    @Result(property="name",column="name"),
        
    @Result(property="list",column="id",many=@Many(select="com.bjsxt.mapper.StudentMapper.selByTid" ))
    })
@Select("select * from teacher")
List<Teacher> selTeacher();

 

如何在solr中实现多core查询

参考技术A  /****多核查询测试*@throwsException*/publicstaticvoidqueryMultiCore()throwsException//查询a和b下面的数据,HttpSolrClientsc=newHttpSolrClient("http://192.168.1.215:8983/solr/a");Stringshards="192.168.1.215:8983/solr/a,192.168.1.214:8983/solr/b";Mod... 查看详情

mybatis多表关联查询

  mybatis多表关联查询一:在一个对象(User)中建立另一个对象属性(userExtend):publicclassUserimplementsSerializable{ privatestaticfinallongserialVersionUID=1L; /**ID*/privateStringid;/**用户名*/privateStringusername;/**登 查看详情

mybatis框架——多表查询

MyBatis多表查询,  从表中映射主表,使用association标签,通过设置javaType属性关联实体类;  主表映射从表,使用 collection标签,通过ofType属性关联实体类。  示例: 1、创建数据库/*NavicatMySQLDataTransferSourceServer... 查看详情

mybatis多表关联查询(代码片段)

...用外键进行关联,在实体类中写的属性也是外键,在使用mybatis时,需要进行调整。需要用到的数据库如下:--------------------------------Tablestructurefordept------------------------------DROPTABLEIFEXISTS`dept`;CREATETABLE`dept`(`did`int 查看详情

mybatis多表查询

 /**作者:呆萌老师*☑csdn认证讲师*☑51cto高级讲师*☑腾讯课堂认证讲师*☑网易云课堂认证讲师*☑华为开发者学堂认证讲师*☑爱奇艺千人名师计划成员*在这里给大家分享技术、知识和生活*各种干货,记得关注哦!*/一个学... 查看详情

mybatis从入门到精通—mybatis多表查询和注解开发(代码片段)

Mybatis多表查询一对一查询一对一查询的模型MapperScannerConfigurer用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户一对一查询的语句... 查看详情

mybatis关联(多表)查询

一对一查询使用resultType:使pojo类继承一个表对应的javabean,添加另一个表的javabean属性。Xml配置和普通pojo是一样的使用resultMap:使pojo添加另一个表对象的javabean属性。Xml配置中使用<association property="user"javaType="user路径">... 查看详情

mybatis多表查询

一、创建部门表和员工表:创建部门信息表`t_department`,其中包括`id`, `name`    CREATETABLEt_department(        idINTAUTO_INCREMENT,      查看详情

mybatis的多表联合查询(代码片段)

...棘手的问题,(也算我自己太大意了)表1-多的情况,可以使用myBatis的关联查询查询出来,一对多的情况,在一的那张表中,添加:<collectionproperty="adImagePaths" 查看详情

mybatis怎么样使用mapper3实现多表关联查询

参考技术A首先,mybatis本身不会判断是否是多表查询,mybatis的多表查询和单表查询其实没有什么区别,你都需要一个对象接收返回值。例如:selectu.namename,p.gradegradefromtable_1u,table_2pwhereu.id=p.id。这里你从两张表里查出两个字段n...... 查看详情

千字文带你入门-mybatis多表查询

MyBatis多表操作经过了MyBatis基本增删改查的学习,而在实际的项目中,我们往往会接触到多表的操作,什么是多表呢,在实际生活中,每个实体之间往往是存在关系的,而我们的项目却是要依赖数据库将这些实体之间的关系串联... 查看详情

mybatis的多表(多对多)查询

Mybatis的多表(多对多)查询示例:用户和角色一个用户可以有多个角色一个角色可以赋予多个用户解决办法建立两张表:用户表,角色表让用户表和角色表具有多对多的关系。需要使用中间表,中间表包含各自的主键,在中间... 查看详情

mybatis04

一.MyBatis实现多表查询 Mybatis实现多表查询方式    1.1业务装配.对两个表编写单表查询语句,在业务(Service)把查询的两个结果进行关联.    1.2使用AutoMapping特性,在实现两表联合查询时通过别名完成映射.    1.3使... 查看详情

mybatis多表查询(一对多,多对一,多对多)(代码片段)

Mybatis多表查询1、一对多1.1、用户详情1.2、用户订单1.3、关联2、多对一2.1通过id查询订单详情2.2关联3、多对多3.1、查询所有学生3.2、查询指定学生的所有授课老师1、一对多查询用户详情,同时查询到用户管理的所有订单查询... 查看详情

mybatis-基于xml的多表查询

mybatis动态SQL简化SQLSql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的。<!--抽取重复片段--><sqlid="selectAll">select*fromuser</sql><!--使用重复片段--><selectid="findUserAll"resultType="user"> 查看详情

mybatis单表多表查询,动态sql使用(代码片段)

文章目录单表查询操作参数占位符#和$$VS#SQL注入like模糊查询多表查询操作一对一多表查询一对多多表查询动态SQL使用if标签trim标签where标签set标签foreach标签单表查询操作参数占位符#和$#:相当于JDBC里面替换占位符的操作方... 查看详情

java示例代码_在Java中实现多线程池

java示例代码_在Java中实现多线程池 查看详情

mybatis--多表关联查询

一 什么是多变关联1.所谓的多变关联,就是表结构中存在多对一,一对多的现象,当然也存在多对多。2.在真正的工作中,经常存在多表关联的存在,就是说外键的存在。比如说:商品表goods,会关联一个分类表,将每个商品... 查看详情