使用mybatis不需要拼凑各种resultMap,通过extends和resultMap直接对应自定义实体类

 商品信息表对应的实体类PmsProduct

/**
 * 商品信息表
 */
public class PmsProduct implements Serializable {
    private Long id;

    private Long brandId;

    private Long productCategoryId;

    private Long feightTemplateId;

    private Long productAttributeCategoryId;

    private String name;

    private String pic;

    @ApiModelProperty(value = "货号")
    private String productSn;

    @ApiModelProperty(value = "删除状态:0->未删除;1->已删除")
    private Integer deleteStatus;

    @ApiModelProperty(value = "上架状态:0->下架;1->上架")
    private Integer publishStatus;

    @ApiModelProperty(value = "新品状态:0->不是新品;1->新品")
    private Integer newStatus;

    @ApiModelProperty(value = "推荐状态;0->不推荐;1->推荐")
    private Integer recommandStatus;

    @ApiModelProperty(value = "审核状态:0->未审核;1->审核通过")
    private Integer verifyStatus;

    @ApiModelProperty(value = "排序")
    private Integer sort;

    @ApiModelProperty(value = "销量")
    private Integer sale;

    private BigDecimal price;

    @ApiModelProperty(value = "促销价格")
    private BigDecimal promotionPrice;

    @ApiModelProperty(value = "赠送的成长值")
    private Integer giftGrowth;

    @ApiModelProperty(value = "赠送的积分")
    private Integer giftPoint;

    @ApiModelProperty(value = "限制使用的积分数")
    private Integer usePointLimit;

    @ApiModelProperty(value = "副标题")
    private String subTitle;

    @ApiModelProperty(value = "市场价")
    private BigDecimal originalPrice;

    @ApiModelProperty(value = "库存")
    private Integer stock;

    @ApiModelProperty(value = "库存预警值")
    private Integer lowStock;

    @ApiModelProperty(value = "单位")
    private String unit;

    @ApiModelProperty(value = "商品重量,默认为克")
    private BigDecimal weight;

    @ApiModelProperty(value = "是否为预告商品:0->不是;1->是")
    private Integer previewStatus;

    @ApiModelProperty(value = "以逗号分割的产品服务:1->无忧退货;2->快速退款;3->免费包邮")
    private String serviceIds;

    private String keywords;

    private String note;

    @ApiModelProperty(value = "画册图片,连产品图片限制为5张,以逗号分割")
    private String albumPics;

    private String detailTitle;

    @ApiModelProperty(value = "促销开始时间")
    private Date promotionStartTime;

    @ApiModelProperty(value = "促销结束时间")
    private Date promotionEndTime;

    @ApiModelProperty(value = "活动限购数量")
    private Integer promotionPerLimit;

    @ApiModelProperty(value = "促销类型:0->没有促销使用原价;1->使用促销价;2->使用会员价;3->使用阶梯价格;4->使用满减价格;5->限时购")
    private Integer promotionType;

    @ApiModelProperty(value = "品牌名称")
    private String brandName;

    @ApiModelProperty(value = "商品分类名称")
    private String productCategoryName;

    @ApiModelProperty(value = "商品描述")
    private String description;

    private String detailDesc;

    @ApiModelProperty(value = "产品详情网页内容")
    private String detailHtml;

    @ApiModelProperty(value = "移动端网页详情")
    private String detailMobileHtml;

    private static final long serialVersionUID = 1L;

get方法....
set方法....

 商品信息表对应的BaseResultMap

<resultMap id="BaseResultMap" type="com.macro.mall.model.PmsProduct">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="brand_id" jdbcType="BIGINT" property="brandId" />
    <result column="product_category_id" jdbcType="BIGINT" property="productCategoryId" />
    <result column="feight_template_id" jdbcType="BIGINT" property="feightTemplateId" />
    <result column="product_attribute_category_id" jdbcType="BIGINT" property="productAttributeCategoryId" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="pic" jdbcType="VARCHAR" property="pic" />
    <result column="product_sn" jdbcType="VARCHAR" property="productSn" />
    <result column="delete_status" jdbcType="INTEGER" property="deleteStatus" />
    <result column="publish_status" jdbcType="INTEGER" property="publishStatus" />
    <result column="new_status" jdbcType="INTEGER" property="newStatus" />
    <result column="recommand_status" jdbcType="INTEGER" property="recommandStatus" />
    <result column="verify_status" jdbcType="INTEGER" property="verifyStatus" />
    <result column="sort" jdbcType="INTEGER" property="sort" />
    <result column="sale" jdbcType="INTEGER" property="sale" />
    <result column="price" jdbcType="DECIMAL" property="price" />
    <result column="promotion_price" jdbcType="DECIMAL" property="promotionPrice" />
    <result column="gift_growth" jdbcType="INTEGER" property="giftGrowth" />
    <result column="gift_point" jdbcType="INTEGER" property="giftPoint" />
    <result column="use_point_limit" jdbcType="INTEGER" property="usePointLimit" />
    <result column="sub_title" jdbcType="VARCHAR" property="subTitle" />
    <result column="original_price" jdbcType="DECIMAL" property="originalPrice" />
    <result column="stock" jdbcType="INTEGER" property="stock" />
    <result column="low_stock" jdbcType="INTEGER" property="lowStock" />
    <result column="unit" jdbcType="VARCHAR" property="unit" />
    <result column="weight" jdbcType="DECIMAL" property="weight" />
    <result column="preview_status" jdbcType="INTEGER" property="previewStatus" />
    <result column="service_ids" jdbcType="VARCHAR" property="serviceIds" />
    <result column="keywords" jdbcType="VARCHAR" property="keywords" />
    <result column="note" jdbcType="VARCHAR" property="note" />
    <result column="album_pics" jdbcType="VARCHAR" property="albumPics" />
    <result column="detail_title" jdbcType="VARCHAR" property="detailTitle" />
    <result column="promotion_start_time" jdbcType="TIMESTAMP" property="promotionStartTime" />
    <result column="promotion_end_time" jdbcType="TIMESTAMP" property="promotionEndTime" />
    <result column="promotion_per_limit" jdbcType="INTEGER" property="promotionPerLimit" />
    <result column="promotion_type" jdbcType="INTEGER" property="promotionType" />
    <result column="brand_name" jdbcType="VARCHAR" property="brandName" />
    <result column="product_category_name" jdbcType="VARCHAR" property="productCategoryName" />
  </resultMap>

 sku库存表 PmsSkuStock


/**
 * sku的库存
 */
public class PmsSkuStock implements Serializable {
    private Long id;

    private Long productId;

    @ApiModelProperty(value = "sku编码")
    private String skuCode;

    private BigDecimal price;

    @ApiModelProperty(value = "库存")
    private Integer stock;

    @ApiModelProperty(value = "预警库存")
    private Integer lowStock;

    @ApiModelProperty(value = "展示图片")
    private String pic;

    @ApiModelProperty(value = "销量")
    private Integer sale;

    @ApiModelProperty(value = "单品促销价格")
    private BigDecimal promotionPrice;

    @ApiModelProperty(value = "锁定库存")
    private Integer lockStock;

    @ApiModelProperty(value = "商品销售属性,json格式")
    private String spData;

    private static final long serialVersionUID = 1L;

get方法....
set方法....

 sku库存表对应的BaseResultMap

<resultMap id="BaseResultMap" type="com.macro.mall.model.PmsSkuStock">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="product_id" jdbcType="BIGINT" property="productId" />
    <result column="sku_code" jdbcType="VARCHAR" property="skuCode" />
    <result column="price" jdbcType="DECIMAL" property="price" />
    <result column="stock" jdbcType="INTEGER" property="stock" />
    <result column="low_stock" jdbcType="INTEGER" property="lowStock" />
    <result column="pic" jdbcType="VARCHAR" property="pic" />
    <result column="sale" jdbcType="INTEGER" property="sale" />
    <result column="promotion_price" jdbcType="DECIMAL" property="promotionPrice" />
    <result column="lock_stock" jdbcType="INTEGER" property="lockStock" />
    <result column="sp_data" jdbcType="VARCHAR" property="spData" />
  </resultMap>

 产品阶梯价格表 PmsProductLadder


/**
 * 产品阶梯价格表(只针对同商品)
 */
public class PmsProductLadder implements Serializable {
    private Long id;

    private Long productId;

    @ApiModelProperty(value = "满足的商品数量")
    private Integer count;

    @ApiModelProperty(value = "折扣")
    private BigDecimal discount;

    @ApiModelProperty(value = "折后价格")
    private BigDecimal price;

    private static final long serialVersionUID = 1L;

get方法...
set方法....

产品阶梯价格表 对应的BaseResultMap

<resultMap id="BaseResultMap" type="com.macro.mall.model.PmsProductLadder">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="product_id" jdbcType="BIGINT" property="productId" />
    <result column="count" jdbcType="INTEGER" property="count" />
    <result column="discount" jdbcType="DECIMAL" property="discount" />
    <result column="price" jdbcType="DECIMAL" property="price" />
  </resultMap>

 产品满减表(只针对同商品) PmsProductFullReduction

/**
 * 产品满减表(只针对同商品)
 */
public class PmsProductFullReduction implements Serializable {
    private Long id;

    private Long productId;

    private BigDecimal fullPrice;

    private BigDecimal reducePrice;

    private static final long serialVersionUID = 1L;

 产品满减表(只针对同商品) 对应的BaseResultMap

<resultMap id="BaseResultMap" type="com.macro.mall.model.PmsProductFullReduction">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="product_id" jdbcType="BIGINT" property="productId" />
    <result column="full_price" jdbcType="DECIMAL" property="fullPrice" />
    <result column="reduce_price" jdbcType="DECIMAL" property="reducePrice" />
  </resultMap>

现在有个dao要通过产品id  获取促销商品信息列表

public interface PortalProductDao {
    /**
     * 获取促销商品信息列表
     */
    List<PromotionProduct> getPromotionProductList(@Param("ids") List<Long> ids);

}

对应的mapper.xml

<select id="getPromotionProductList" resultMap="promotionProductMap">
        SELECT
            p.id,
            p.`name`,
            p.promotion_type,
            p.gift_growth,
            p.gift_point,
            sku.id sku_id,
            sku.price sku_price,
            sku.sku_code sku_sku_code,
            sku.promotion_price sku_promotion_price,
            sku.stock sku_stock,
            sku.lock_stock sku_lock_stock,
            ladder.id ladder_id,
            ladder.count ladder_count,
            ladder.discount ladder_discount,
            full_re.id full_id,
            full_re.full_price full_full_price,
            full_re.reduce_price full_reduce_price
        FROM
            pms_product p
            LEFT JOIN pms_sku_stock sku ON p.id = sku.product_id
            LEFT JOIN pms_product_ladder ladder ON p.id = ladder.product_id
            LEFT JOIN pms_product_full_reduction full_re ON p.id = full_re.product_id
        WHERE
            p.id IN
        <foreach collection="ids" open="(" close=")" item="id" separator=",">
            #{id}
        </foreach>
    </select>

PromotionProduct 类对应的resultMap

<resultMap id="promotionProductMap" type="com.macro.mall.portal.domain.PromotionProduct" extends="com.macro.mall.mapper.PmsProductMapper.BaseResultMap">
        <id column="id" jdbcType="BIGINT" property="id" />
        <collection property="skuStockList" columnPrefix="sku_" resultMap="com.macro.mall.mapper.PmsSkuStockMapper.BaseResultMap">
        </collection>
        <collection property="productLadderList" columnPrefix="ladder_" resultMap="com.macro.mall.mapper.PmsProductLadderMapper.BaseResultMap">
        </collection>
        <collection property="productFullReductionList" columnPrefix="full_" resultMap="com.macro.mall.mapper.PmsProductFullReductionMapper.BaseResultMap">
        </collection>
</resultMap>

PromotionProduct 

/**
 * 
 * 商品的促销信息,包括sku、打折优惠、满减优惠
 */
public class PromotionProduct extends PmsProduct {
    //商品库存信息
    private List<PmsSkuStock> skuStockList;
    //商品打折信息
    private List<PmsProductLadder> productLadderList;
    //商品满减信息
    private List<PmsProductFullReduction> productFullReductionList;

get...
set...
和其他方法

 通过这种办法就不需要去拼凑各种resultMap,可以通过extends和<collection  resultMap=“  ”></collection>直接对应实体,例如这里的PromotionProduct

 

 

 

如果 MySQL 数据库中某字段为 JSON 类型,且返回的是 JSON 对象数组,那么在 MyBatis 中需要进行特殊的处理。一般情况下,我们可以使用 `@Result` 注解或 `<result>` 标签来对查询结果进行映射,但是这种方式对于 JSON 对象数组来说并适用。 针对这种情况,可以使用 MyBatis 的 `TypeHandler` 接口来自定义类型处理器,将 JSON 字符串转化为 Java 对象。具体步骤如下: 1. 创建一个实体类,用于存储 JSON 对象数组的数据。 ```java public class MyJsonArray { private List<MyJsonObject> jsonArray; // getter setter 方法省略 } ``` 2. 创建一个 `TypeHandler` 接口的实现类,用于将 JSON 字符串转化为 `MyJsonArray` 对象。代码如下: ```java public class MyJsonArrayTypeHandler extends BaseTypeHandler<MyJsonArray> { @Override public void setNonNullParameter(PreparedStatement ps, int i, MyJsonArray parameter, JdbcType jdbcType) throws SQLException { // 将 MyJsonArray 对象转化为 JSON 字符串,并设置到 PreparedStatement 中 ps.setString(i, JSON.toJSONString(parameter.getJsonArray())); } @Override public MyJsonArray getNullableResult(ResultSet rs, String columnName) throws SQLException { // 从 ResultSet 中获取 JSON 字符串,并将其转化为 MyJsonArray 对象 return parseJson(rs.getString(columnName)); } @Override public MyJsonArray getNullableResult(ResultSet rs, int columnIndex) throws SQLException { // 从 ResultSet 中获取 JSON 字符串,并将其转化为 MyJsonArray 对象 return parseJson(rs.getString(columnIndex)); } @Override public MyJsonArray getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { // 从 CallableStatement 中获取 JSON 字符串,并将其转化为 MyJsonArray 对象 return parseJson(cs.getString(columnIndex)); } private MyJsonArray parseJson(String jsonString) { // 将 JSON 字符串转化为 MyJsonArray 对象 if (StringUtils.isEmpty(jsonString)) { return null; } return JSON.parseObject(jsonString, MyJsonArray.class); } } ``` 3. 在实体类的 `resultMap` 中,使用 `typeHandler` 属性指定刚才创建的 `MyJsonArrayTypeHandler` 类型处理器。代码如下: ```xml <resultMap id="myResultMap" type="com.example.MyEntity"> <id property="id" column="id" /> <result property="jsonArray" column="json_column" javaType="com.example.MyJsonArray" typeHandler="com.example.MyJsonArrayTypeHandler" /> </resultMap> ``` 这样就可以将 MySQL 中的 JSON 对象数组映射到 Java 对象中了。需要注意的是,如果 JSON 对象数组中的元素类型一致,需要在 `MyJsonArray` 类中定义多个字段来存储同类型的元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值