zoukankan      html  css  js  c++  java
  • mybatis 一对多映射踩过的坑

    实体类Sharing(省略get set):

    public class Sharing implements Serializable {
        private Long sharingId;
    
        private Long userId;
    
        private Integer sharingTypeId;
    
        private String sharingDescribe;
    
        private String sharingUrl;
    
        private Integer sharingState;
    }

    实体类SharingType(省略get set):

    public class SharingType implements Serializable{
        private Integer sharingTypeId;
    
        private Long userId;
    
        private String sharingName;
    
        private Integer sharingTypeState;
    
        private List<Sharing> sharingList;
    }

    SharingMapper.xml:

      <resultMap id="BaseResultMap" type="com.lemon.boboke.entity.Sharing">
        <id column="sharing_id" jdbcType="BIGINT" property="sharingId" />
        <result column="user_id" jdbcType="BIGINT" property="userId" />
        <result column="sharing_type_id" jdbcType="INTEGER" property="sharingTypeId" />
        <result column="sharing_describe" jdbcType="VARCHAR" property="sharingDescribe" />
        <result column="sharing_url" jdbcType="VARCHAR" property="sharingUrl" />
        <result column="sharing_state" jdbcType="INTEGER" property="sharingState" />
      </resultMap>

    SpringTypeMapper.xml:

      <resultMap id="BaseResultMap" type="com.lemon.boboke.entity.SharingType">
        <id column="sharing_type_id" jdbcType="INTEGER" property="sharingTypeId" />
        <result column="user_id" jdbcType="BIGINT" property="userId" />
        <result column="sharing_name" jdbcType="VARCHAR" property="sharingName" />
        <result column="sharing_type_state" jdbcType="INTEGER" property="sharingTypeState" />
        <collection property="sharingList" resultMap="com.lemon.boboke.dao.SharingMapper.BaseResultMap"/>
      </resultMap>

    踩坑的地方(返回类型:resultType="com.lemon.boboke.entity.SharingType"

      <select id="selectAllGroupByType" resultType="com.lemon.boboke.entity.SharingType">
        SELECT sht.*,shar.sharing_id,shar.sharing_describe,shar.sharing_url,shar.sharing_state FROM `sharing_type` AS sht
        inner JOIN `sharing` AS shar  ON sht.sharing_type_id = shar.sharing_type_id
        WHERE sht.user_id = (SELECT `user_id` FROM `user` WHERE `user`.user_account = #{userAccount})
        AND sht.sharing_type_state = 0 AND shar.sharing_state = 0
      </select>

     因为是多表查询,返回结果应该是新的映射BaseResultMap

     

  • 相关阅读:
    dev、test和prod是什么意思?
    构建项目时依赖库下载不下来的解决
    自定义view规范步骤步骤
    【转】MIUI8以及ViVO X9上在Android Studio运行出错集及其解决方案
    Ubuntu 16.04使用git
    Android中WebView与H5的交互,Native与JS方法互调
    [转] Fragment——startActivityForResult后onActivityResult无反应之问题总结
    解决运行github项目build时间长问题
    SVN回滚至某个版本
    【算法】种花问题
  • 原文地址:https://www.cnblogs.com/ldl326308/p/12720353.html
Copyright © 2011-2022 走看看