美文网首页
mybatis查询

mybatis查询

作者: 江小石 | 来源:发表于2017-07-24 16:45 被阅读0次

    mybatis一对一联合查询

    需求:现有表t_fee(id,channel_id,amount),其中channel_id是外键,与表t_channel(id,name)的主键id对应,一对一的关系,需要查询fee表的同时也得到对应channel中的记录
    步骤:

    1. 表t_fee的实体类com.business.entity.Fee添加一个属性channel,类型是表t_channel的实体类
    com.business.entity.Channel
    package com.business.entity;
    import java.math.BigDecimal;
    import java.util.Date;
    public class Fee {
        private Integer id;
    
        private Long channelId;
    
        private BigDecimal amount;
    
        private String copyWriter;
        
        //额外属性
        private RepayChannel repayChannel;
    
        //getter and setter
    }
    

    虽然现在fee的实体类和表没有完全一致,但是不会引起错误

    1. 在FeeMapper.xml定义一个FeeMap,接收t_fee字段和t_channel字段的resultMap
      (1)extends="BaseResultMap" :FeeMap只是在BaseResultMap的基础上加上channel的属性,不需要再写一遍t_fee的列名
      (2)加上<association property="channel" column="channel_id" select="selectChannel"/>
      property表示t_channel的属性在t_fee实体类中对应的变量名
      column表示t_channel的主键在t_fee中的列名
      select表示根据t_fee的channel_id查询t_channel的记录。
    2. 编写selectChannel,根据fee的channel_id查询channel。代码里只查询了channel的name字段
      注意:要加上distinct,以确保最多返回一条数据,否则报错:

    Statement returned more than one row, where no more than one was expected.

    1. 最后的接口selectFee,只需要注意把resultMap改为FeeMap
    <?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.business.mapper.FeeMapper">
      <resultMap id="BaseResultMap" type="com.business.entity.Fee">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="channel_id" jdbcType="BIGINT" property="channelId" />
        <result column="amount" jdbcType="DECIMAL" property="amount" />
        <result column="copy_writer" jdbcType="VARCHAR" property="copyWriter" />
      </resultMap>
      <sql id="Base_Column_List">
        id, channel_id, amount, copy_writer
      </sql>
      <resultMap id="FeeMap" type="com.business.entity.Fee" extends="BaseResultMap">
        <association property="channel" column="channel_id" select="selectChannel"/>
      </resultMap>
        <select id="selectChannel"  resultType="com.business.entity.Channel">
        select 
        distinct C.name
        from t_fee F, t_channel C
        where t.channel_id=c.id
      </select>
      <select id="selectFee" resultMap="FeeMap">
        select 
        <include refid="Base_Column_List" />
        from t_fee
      </select>
    </mapper>
    

    mybatis一对多联合查询

    1、主表t_a,实体类com.A,关联表t_b,实体类com.B,t_a对t_b是1:n
    在实体类A中加入B的属性:

    private List<B> bList;
    // getter and setter
    

    新建一个resultMap,继承A的BaseResultMap,加入collection
    column是列名,不一定是表的列名,它是对应sql语句查出记录的结果的列名
    property是在实体类中的属性名称

    <resultMap id="ABResult" type="com.A" extends="BaseResultMap">  
        <collection property="bList" column="id"
                    ofType="com.B" 
                    select="selectBForA">
        </collection>
    </resultMap>
    <select id="selectBForA" resultType="com.B" parameterType="int">
            #注意这里的查询结果字段需要和类B的变量名相同才能映射,不要求B的每个字段都有,但是有的需要对应
            select
            channel_type as channelType
            from t_b
            where deposit_id=#{id}
    </select>
    <select id="selectABById" resultMap="ABResult" parameterType="map">
            select 
            A.id, A.type ,R.name
            from t_a A left join t_b B on A.id=B.a_id
            where A.id=#{id}
    </select>
    

    如果查询结果有两张表的相同列名name,则将会是A.name、B.name,可以在collection 属性加入columnPrefix="B."

    相关文章

      网友评论

          本文标题:mybatis查询

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