12.服务治理-Eureka-电商系统集成Eureka - 商品

2020-05-01  本文已影响0人  溅十三
image.png

pojo

1.在domain下创建商品微服务的子文件夹item,商品相关的字Moudel都会放在这个下面
2.从依赖最少的模块pojo(dao和领域模型)开始
3.创建foodie-item-pojo子模块(把商品域item嵌入在foodie-pojo中)
4.添加依赖项,主要是支持Dao层(持久层依赖)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>foodie-cloud</artifactId>
        <groupId>com.imooc</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../../../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>foodie-item-pojo</artifactId>

    <dependencies>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
    </dependencies>


</project>

5.文件移植
a.移植Items:Items、ItemsComments、ItemsImg、ItemsParam、ItemsSpec
b.移植vo:
> do bo划分?不会把DO直接传递一次再透传回来,屏蔽底层的修改影响到上游

mapper

1.创建foodie-item-mapper项目

依赖pojo
pom的依赖不要写版本号,另外一种方式实现

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>foodie-cloud</artifactId>
        <groupId>com.imooc</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../../../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>foodie-item-mapper</artifactId>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>foodie-dev-pojo</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>foodie-cloud-common</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>


</project>

2.copy foodie下面的mapper到item

import路径正确但还是标红,重新导入

3.ItemsMapperCustom文件搜索功能的移植(TODO)

package com.imooc.item.mapper;

import com.imooc.pojo.vo.ItemCommentVO;
import com.imooc.pojo.vo.SearchItemsVO;
import com.imooc.pojo.vo.ShopcartVO;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

public interface ItemsMapperCustom {

    public List<ItemCommentVO> queryItemComments(@Param("paramsMap") Map<String, Object> map);

    //TODO 迁移到foodie-search
//    public List<SearchItemsVO> searchItems(@Param("paramsMap") Map<String, Object> map);
//
//    public List<SearchItemsVO> searchItemsByThirdCat(@Param("paramsMap") Map<String, Object> map);

    public List<ShopcartVO> queryItemsBySpecIds(@Param("paramsList") List specIdsList);

    public int decreaseItemSpecStock(@Param("specId") String specId,
                                     @Param("pendingCounts") int pendingCounts);
}

4.移植resource文件,将所有的mapper文件复制到resource/mapper文件夹下
修改mapper的路径(替换方式)
5.注释ItemsMapperCustom对应的sql

<?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.imooc.item.mapper.ItemsMapperCustom" >

  <select id="queryItemComments" parameterType="Map" resultType="com.imooc.item.pojo.vo.ItemCommentVO">
    SELECT
        ic.comment_level as commentLevel,
        ic.content as content,
        ic.sepc_name as specName,
        ic.created_time as createdTime,
        u.face as userFace,
        u.nickname as nickname
    FROM
        items_comments ic
    LEFT JOIN
        users u
    ON
        ic.user_id = u.id
    WHERE
        ic.item_id = #{paramsMap.itemId}
        <if test=" paramsMap.level != null and paramsMap.level != '' ">
          AND ic.comment_level = #{paramsMap.level}
        </if>
  </select>

<!--    <select id="searchItems" parameterType="Map" resultType="com.imooc.item.pojo.vo.SearchItemsVO">-->
<!--        SELECT-->
<!--            i.id as itemId,-->
<!--            i.item_name as itemName,-->
<!--            i.sell_counts as sellCounts,-->
<!--            ii.url as imgUrl,-->
<!--            tempSpec.price_discount as price-->
<!--        FROM-->
<!--            items i-->
<!--        LEFT JOIN-->
<!--            items_img ii-->
<!--        on-->
<!--            i.id = ii.item_id-->
<!--        LEFT JOIN-->
<!--            (SELECT item_id,MIN(price_discount) as price_discount from items_spec GROUP BY item_id) tempSpec-->
<!--        on-->
<!--            i.id = tempSpec.item_id-->
<!--        WHERE-->
<!--            ii.is_main = 1-->
<!--            <if test=" paramsMap.keywords != null and paramsMap.keywords != '' ">-->
<!--                AND i.item_name like '%${paramsMap.keywords}%'-->
<!--            </if>-->
<!--            order by-->
<!--            <choose>-->
<!--                <when test=" paramsMap.sort == &quot;c&quot; ">-->
<!--                    i.sell_counts desc-->
<!--                </when>-->
<!--                <when test=" paramsMap.sort == &quot;p&quot; ">-->
<!--                    tempSpec.price_discount asc-->
<!--                </when>-->
<!--                <otherwise>-->
<!--                    i.item_name asc-->
<!--                </otherwise>-->
<!--            </choose>-->
<!--    </select>-->
<!--    &lt;!&ndash; k: 默认,代表默认排序,根据name&ndash;&gt;-->
<!--    &lt;!&ndash; c: 根据销量排序&ndash;&gt;-->
<!--    &lt;!&ndash; p: 根据价格排序&ndash;&gt;-->


<!--    <select id="searchItemsByThirdCat" parameterType="Map" resultType="com.imooc.item.pojo.vo.SearchItemsVO">-->
<!--        SELECT-->
<!--            i.id as itemId,-->
<!--            i.item_name as itemName,-->
<!--            i.sell_counts as sellCounts,-->
<!--            ii.url as imgUrl,-->
<!--            tempSpec.price_discount as price-->
<!--        FROM-->
<!--          items i-->
<!--        LEFT JOIN-->
<!--          items_img ii-->
<!--        on-->
<!--          i.id = ii.item_id-->
<!--        LEFT JOIN-->
<!--          (SELECT item_id,MIN(price_discount) as price_discount from items_spec GROUP BY item_id) tempSpec-->
<!--        on-->
<!--          i.id = tempSpec.item_id-->
<!--        WHERE-->
<!--          ii.is_main = 1-->
<!--          and-->
<!--          i.cat_id = #{paramsMap.catId}-->
<!--        order by-->
<!--        <choose>-->
<!--            <when test=" paramsMap.sort == &quot;c&quot; ">-->
<!--                i.sell_counts desc-->
<!--            </when>-->
<!--            <when test=" paramsMap.sort == &quot;p&quot; ">-->
<!--                tempSpec.price_discount asc-->
<!--            </when>-->
<!--            <otherwise>-->
<!--                i.item_name asc-->
<!--            </otherwise>-->
<!--        </choose>-->
<!--    </select>-->


    <select id="queryItemsBySpecIds" parameterType="List" resultType="com.imooc.item.pojo.vo.ShopcartVO">
        SELECT
            t_items.id as itemId,
            t_items.item_name as itemName,
            t_items_img.url as itemImgUrl,
            t_items_spec.id as specId,
            t_items_spec.`name` as specName,
            t_items_spec.price_discount as priceDiscount,
            t_items_spec.price_normal as priceNormal
        FROM
            items_spec t_items_spec
        LEFT JOIN
            items t_items
        ON
            t_items.id = t_items_spec.item_id
        LEFT JOIN
            items_img t_items_img
        on
            t_items_img.item_id = t_items.id
        WHERE
            t_items_img.is_main = 1
        AND
            t_items_spec.id IN
            <foreach collection="paramsList" index="index" item="specId" open="(" separator="," close=")">
              #{specId}
            </foreach>
    </select>

    <update id="decreaseItemSpecStock">

        update
            items_spec
        set
            stock = stock - #{pendingCounts}
        where
            id = #{specId}
        and
            stock >= #{pendingCounts}

    </update>

</mapper>
上一篇下一篇

猜你喜欢

热点阅读