zoukankan      html  css  js  c++  java
  • mybatis 关联查询

    什么是嵌套结果?:

    即一条 sql 语句搞定关联关系

    什么是嵌套查询:?

    两个以上 的 方式搞定 关联关系

    第一种 管理系统用的比较多

     

     设置 fetchType 可以实现一个 按需加载

    result Map  高级用法:  使用 extend 关键字 集成 resultMap

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.enjoylearning.mybatis.mapper.TUserMapper">
    
        <resultMap id="BaseResultMap" type="TUser">
            <id column="id" property="id" />
            <result column="note" property="note" />
        </resultMap>
        <resultMap id="userAndPosition1" extends="BaseResultMap" type="TUser">
            <association property="position" javaType="TPosition" columnPrefix="post_">
                <id column="id" property="id"/>
            </association>
        </resultMap>
    
        <sql id="Base_Column_List">
            id, user_name, real_name, sex, mobile, email, note,
            position_id
        </sql>
        
        <select id="selectUserPosition1" resultMap="userAndPosition1">
            select   a.id,user_name,real_name,sex,    mobile,
            from t_user a,
                t_position b
            where a.position_id = b.id
        </select>
        
        <insert id="insertForeach4Batch" useGeneratedKeys="true" keyProperty="id">
            insert into t_user (user_name, real_name,sex, mobile,email,note,position_id)
            values(
                #{user.userName,jdbcType=VARCHAR},
                #{user.realName,jdbcType=VARCHAR},
                #{user.note,jdbcType=VARCHAR},
                #{user.position.id,jdbcType=INTEGER})
        </insert>
    
    </mapper>

    一些常用的技巧:

    比如 A,B 两个表 都有 id, 这个时候需要 给两个相同字段取个别名

    最好是加个前缀

    比如 A_id , B_id

    建议是 给子表重命名字段

    建议给 mapper 添加个命名空间,这样可以 直接引用别的 xml 的 代码片段,达到复用的效果

    比如:

    <mapper namespace="com.enjoylearning.mybatis.mapper.TUserMapper">
    

      

    mybatis 还有一种用法:嵌套查询

    可以在 resultMap 中进行制定:

    <resultMap id="userAndPosition2" extends="BaseResultMap" type="TUser">
            <association property="position" fetchType="lazy"  column="{position_id=id}" select="com.enjoylearning.mybatis.mapper.TPositionMapper.selectByPrimaryKey" />
        </resultMap>

    <resultMap type="domain.User" id="user">
      <id column="id" property="id"/>
      <result column="age" property="age"/>
          <collection column="id" property="orders" ofType="domain.User_orders"   select="selectOrderByUser"> 
          <id column="id" property="id"/>
      <result column="name" property="name"/>
     </collection>


    <resultMap id="blogResult" type="Blog">
    
      <association property="author" column="{id=author_id,likename=author_name}" javaType="Author" select="selectAuthor"/>
    
    </resultMap>

    相关的博客 文章: https://www.cnblogs.com/whb11/p/7230096.html

    https://www.cnblogs.com/jimmy-muyuan/p/5459970.html

    column = " { param1 = col1, param2 = col2 } "  这样子传递参数

    discriminator 鉴别器 的使用

        <resultMap id="userAndHealthReport" extends="BaseResultMap" type="TUser">
                     
            <discriminator column="sex"  javaType="int">
                <case value="1" resultMap="userAndHealthReportMale"/>
                <case value="2" resultMap="userAndHealthReportFemale"/>
            </discriminator>
        </resultMap>
        

    可以面向接口编程, 当 sex = 1 的时候,使用 男性体检报告.class new 一个对象

    当 sex=2  的时候,使用 女性体检报告 new 一个对象

    这种 就是在返回数据的 时候 动态 使用策略【类似于策略模式】,  通过鉴别器 考虑使用哪种类型

    举个例子:

        <resultMap id="userAndHealthReportMale" extends="userAndHealthReport" type="TUser">
            <collection property="healthReports" column="id"
                select= "com.enjoylearning.mybatis.mapper.THealthReportMaleMapper.selectByUserId"></collection>
        </resultMap>
        
        <resultMap id="userAndHealthReportFemale" extends="userAndHealthReport" type="TUser">
            <collection property="healthReports" column="id"
                select= "com.enjoylearning.mybatis.mapper.THealthReportFemaleMapper.selectByUserId"></collection>
        </resultMap>
        
        <resultMap id="userAndHealthReport" extends="BaseResultMap" type="TUser">
                     
            <discriminator column="sex"  javaType="int">
                <case value="1" resultMap="userAndHealthReportMale"/>
                <case value="2" resultMap="userAndHealthReportFemale"/>
            </discriminator>
        </resultMap>
        

    举个例子:

    如果 orderType = 1, 去查询 order_consumer 表, 如果 orderType= 2, 去查询 order_pay 表

    这个时候,就可以这样搞 ,配合嵌套查询,就特别方便

  • 相关阅读:
    Java实现蓝桥杯突击战
    Java实现蓝桥杯突击战
    Java实现蓝桥杯勇者斗恶龙
    Java实现蓝桥杯勇者斗恶龙
    利用cmake来搭建开发环境
    cmake总结
    cmake手册详解----转
    Boost::filesystem 使用小笔记
    Boost学习之可移植路径操作--filesystem
    C++ 檔案、資料夾、路徑處理函式庫:boost::filesystem
  • 原文地址:https://www.cnblogs.com/lyr-2000/p/13858100.html
Copyright © 2011-2022 走看看