美文网首页
mybtis关系映射及多表查询

mybtis关系映射及多表查询

作者: Zak1 | 来源:发表于2018-12-23 16:24 被阅读0次

实体关系问题

  • 1:1

多表查询

package com.zakl.pojo;

import java.io.Serializable;
//返回的查询类
public class Userwithdetailpojo extends User2pojo implements Serializable {

    private  Userdetailpojo userdetailpojo;
  //省略get set toString
}

 <resultMap id="userwithdetail" type="com.zakl.pojo.Userwithdetailpojo">
        <id  property="userid" column="uid" />
        <result property="phone" column="phone"></result>
        <result property="password" column="password"></result>
        <result property="createDate" column="creatdate"></result>
        <result property="status" column="status"></result>
      <!--关联的信息 -->
        <!--持有Userdetialpojo的封装
      property是Userwithdetailpojo类的属性
      不像result简单属性
      需要用javaType来额外指明他的java数据类型
      官方推荐方式
      -->
      <!--列名有重复的时候,使用别名-->
        <association property="userdetailpojo" javaType="com.zakl.pojo.Userdetailpojo">
            <id property="udid" column="udid"></id>
            <result property="address" column="address"></result>
            <result property="cid" column="cid"></result>
            
            <!--也可以使用连缀点法 -->
            <!--
            <result property="Userdetailpojo.udid" column="udid"></result>
            <result property="Userdetailpojo.address" column="address"></result>
            <result property="Userdetailpojo.cid" column="cid"></result>
          -->
        </association>

    </resultMap>

    <select id="querybyid" resultMap="userwithdetail">
        select t1.userid as uid, t1.phone, t1.password ,t1.creatdate, t1.status, t2.udid , t2.address, t2.cid
        from user2 as t1 ,userdetail as t2
        <where>
            t1.userid=t2.userid and t1.userid =#{id};
        </where>

使用泛型方法进行测试

    public static<T> void  queryautocommit(String methodname,Object[] objs) throws InvocationTargetException, IllegalAccessException {
        SqlSession sqlSession = MybatisUtil.getSession();
        User2Mapper user2Mapper = sqlSession.getMapper(User2Mapper.class);
        Method[] methods=user2Mapper.getClass().getMethods();
        Method method=null;
        for (int i=0;i<methods.length;i++){
            if (methods[i].getName().equals(methodname)){
                method=methods[i];
                System.out.println(methods[i].getName());
            }
        }
        List<T> userwithdetailpojos= (List<T>) method.invoke(user2Mapper,objs);
        System.out.println(userwithdetailpojos);
        sqlSession.close();
    }

    @Test
    public void testdemo() throws InvocationTargetException, IllegalAccessException {
        Object[] objects=new Object[]{1};
        queryautocommit("querybyid",objects);
    }

使用分步查詢

  <resultMap id="basic" type="com.zakl.pojo.User2pojo">
        <id  property="userid" column="userid" />
        <result property="phone" column="phone"></result>
        <result property="password" column="password"></result>
        <result property="createDate" column="creatdate"></result>
        <result property="status" column="status"></result>
    </resultMap>
  
 <resultMap id="resultmap2" extends="basic" type="com.zakl.pojo.Userwithdetailpojo" >     <!-- select属性是用来选择接口中的方法 -->
        <association property="userdetailpojo" select="com.zakl.mapper.UserdetailMapper.querybyUseridbystep" column="userid"></association>
    </resultMap>

<select id="querybystepid" resultMap="resultmap2">
        select userid , phone,password ,creatdate, status
        from user2
        <where>
            userid =#{id};
        </where>
    </select>

UserdetailMapper查詢文件

    <select id="querybyUseridbystep" resultType="com.zakl.pojo.Userdetailpojo">
        select * from userdetail where userid=#{userid}
    </select>
public interface User2Mapper {
    List<Userwithdetailpojo> querybyid(@Param("id") Integer id);
    List<Userwithdetailpojo> querybystepid(@Param("id") Integer id);
}
public interface UserdetailMapper {
    List<Userwithdetailpojo> querybyUseridbystep(@Param("userid") Integer userid);
}


 @Test
    public void demo11() {
        SqlSession sqlSession = MybatisUtil.getSession();
        User2Mapper user2Mapper=sqlSession.getMapper(User2Mapper.class);
        List<Userwithdetailpojo> userwithdetailpojos = user2Mapper.querybystepid(1);
        System.out.println(userwithdetailpojos);
        sqlSession.commit();
        sqlSession.close();
    }

注意每次新增Mapper.xml時都應該在mybatis.cfg.xml中進行配置

    <mappers>
        <!-- resource : 相对路径查询资源的属性.
        相对于当前核心配置文件的位置开始查找映射文件.
        -->
        <mapper resource="com.zakl.pojo/userpojoMapper.xml"/>
        <mapper resource="com.zakl.pojo/AddressMapper.xml"/>
        <mapper resource="com.zakl.pojo/User2pojpMapper.xml"/>
        <mapper resource="com.zakl.pojo/UserdetailMapper.xml" />
    </mappers>
  • 1:n

多表联合查询

​ 三表结构

​ [图片上传失败...(image-b43e37-1545553459517)]

​ 三个pojo类

public class UserwithBlog extends User2pojo implements Serializable {
    private List<Blog> blogs;
}

public class Blog {
    private Integer id;
    private User2pojo user2pojo;
    private String title;
    private String content;
    private List<Comment> comments;
}

public class Comment {
    private Integer id;
    private String comment;
    private Blog blog;
}

//接口方法
@interface
  UserwithBlog querblogbyid(@Param("userid")Integer userid);

mapper.xml配置

<!--多表联合查询 有1对多关系时使用 connection标签 使用oftype来声明类型 -->
  <resultMap id="userwithblog" extends="basic" type="com.zakl.pojo.UserwithBlog">
        <collection property="blogs" ofType="com.zakl.pojo.Blog">
            <id property="id" column="bid"></id>
            <result property="title" column="title"></result>
            <result property="content" column="content"></result>
          <!--  <result property="user2pojo.userid" column="aid"></result>-->
            <collection property="comments" ofType="com.zakl.pojo.Comment">
                <id property="id" column="cid"></id>
                <result property="comment" column="comment"></result>
               <!-- <result property="blog.id" column="blogid"></result>-->
            </collection>
        </collection>
    </resultMap>


    <select id="querblogbyid" resultMap="userwithblog">
           select t1.userid as aid ,t1.phone ,t1.password,t1.creatdate,t1.status,
                  t2.id as bid,t2.userid,t2.title,t2.content,
                  t3.id as cid, t3.comment,t3.blogid
            from user2 as t1 ,blog as t2,comment as t3
            <where>
                t1.userid=t2.userid and
                 t2.id=t3.blogid and
                  t1.userid=#{userid}
            </where>

    </select>

相关文章

  • mybtis关系映射及多表查询

    实体关系问题 1:1 多表查询 使用泛型方法进行测试 使用分步查詢 UserdetailMapper查詢文件 注意...

  • 2018-09-10复习

    一的关系,association嵌套结果,使用多表查询语句。resultMap,绑定数据,多表查询就ok多的关系,...

  • mysql数据库-多表查询

    今日任务 完成对MYSQL数据库的多表查询及建表的操作 教学目标 掌握MYSQL中多表的创建及多表的查询 掌握MY...

  • 【Spring Boot + Kotlin 实战教程】Sprin

    【Spring Boot + Kotlin 实战教程】Spring Data JPA 多表关联查询 映射到 Dt...

  • mysql多表查询0529

    /*多表查询综合练习,测试显性查询和隐性查询,熟悉多表对应关系,写清中间表 显示内连接标准语法:SELECT 列名...

  • Django-关系映射

    关系映射 一对一映射 语法 查询 一对多映射 语法 查询 多对多映射 语法 查询 自定义查询对象

  • 56、【数据库技术】【MySQL】多表查询

    1、多表查询概述 多表查询,即查询时候的数据来源不再只有一张表。 多表查询在关系型数据库的基础理论中是有一些理论基...

  • SQL进阶2

    --表连接Join-- 查询所有学生的姓名、年龄及所在班级 使用子查询 使用from多表 使用多表连接 表 in...

  • 多表查询及子查询

    前言 在数据库中我们想查询的数据往往是存在多个独立的表中的,所以客户在浏览器页面操作时,服务器要到数据库中查找相应...

  • SSM框架的常用注解整理

    一.mybatis 1 配置一对多查询和多对多查询的注解方式映射关系: @Results:声明映射关系的配置 Va...

网友评论

      本文标题:mybtis关系映射及多表查询

      本文链接:https://www.haomeiwen.com/subject/xbynkqtx.html