zoukankan      html  css  js  c++  java
  • 自定义Mappter

    自定义Mappter

    package com.imooc.service;
    
    import com.imooc.pojo.Category;
    import com.imooc.pojo.vo.CategoryVO;
    import com.imooc.pojo.vo.NewItemsVO;
    
    import java.util.List;
    
    public interface CategoryService {
    
        /**
         * 查询所有一级分类
         * @return
         */
        public List<Category> queryAllRootLevelCat();
    
        /**
         * 根据一级分类id查询子分类信息
         * @param rootCatId
         * @return
         */
        public List<CategoryVO> getSubCatList(Integer rootCatId);
    
        /**
         * 查询首页每个一级分类下的6条最新商品数据
         * @param rootCatId
         * @return
         */
        public List<NewItemsVO> getSixNewItemsLazy(Integer rootCatId);
    
    }
    View Code
    package com.imooc.service.impl;
    
    import com.imooc.mapper.CarouselMapper;
    import com.imooc.mapper.CategoryMapper;
    import com.imooc.mapper.CategoryMapperCustom;
    import com.imooc.pojo.Carousel;
    import com.imooc.pojo.Category;
    import com.imooc.pojo.vo.CategoryVO;
    import com.imooc.pojo.vo.NewItemsVO;
    import com.imooc.service.CarouselService;
    import com.imooc.service.CategoryService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    import tk.mybatis.mapper.entity.Example;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @Service
    public class CategoryServiceImpl implements CategoryService {
    
        @Autowired
        private CategoryMapper categoryMapper;
    
        @Autowired
        private CategoryMapperCustom categoryMapperCustom;
    
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public List<Category> queryAllRootLevelCat() {
    
            Example example = new Example(Category.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("type", 1);
    
            List<Category> result =  categoryMapper.selectByExample(example);
    
            return result;
        }
    
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public List<CategoryVO> getSubCatList(Integer rootCatId) {
            return categoryMapperCustom.getSubCatList(rootCatId);
        }
    
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public List<NewItemsVO> getSixNewItemsLazy(Integer rootCatId) {
    
            Map<String, Object> map = new HashMap<>();
            map.put("rootCatId", rootCatId);
    
            return categoryMapperCustom.getSixNewItemsLazy(map);
        }
    }
    View Code
    package com.imooc.mapper;
    
    import com.imooc.pojo.vo.CategoryVO;
    import com.imooc.pojo.vo.NewItemsVO;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    import java.util.Map;
    
    public interface CategoryMapperCustom {
    
        public List<CategoryVO> getSubCatList(Integer rootCatId);
    
        public List<NewItemsVO> getSixNewItemsLazy(@Param("paramsMap") Map<String, Object> map);
    }
    View Code
    <?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.mapper.CategoryMapperCustom" >
    
      <resultMap id="myCategoryVO" type="com.imooc.pojo.vo.CategoryVO">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="type" property="type"/>
        <result column="fatherId" property="fatherId"/>
    
        <!--
          collection 标签:用于定义关联的list集合类型的封装规则
          property:对应三级分类的list属性名
          ofType:集合的类型,三级分类的vo
        -->
        <collection property="subCatList" ofType="com.imooc.pojo.vo.SubCategoryVO">
          <id column="subId" property="subId"/>
          <result column="subName" property="subName"/>
          <result column="subType" property="subType"/>
          <result column="subFatherId" property="subFatherId"/>
        </collection>
      </resultMap>
    
      <select id="getSubCatList" resultMap="myCategoryVO" parameterType="int">
        SELECT
            f.id as id,
            f.`name` as `name`,
            f.type as type,
            f.father_id as fatherId,
            c.id as subId,
            c.`name` as subName,
            c.type as subType,
            c.father_id as subFatherId
        FROM
            category f
        LEFT JOIN
            category c
        on
            f.id = c.father_id
        WHERE
            f.father_id = #{rootCatId}
      </select>
    
        <resultMap id="myNewItemsVO" type="com.imooc.pojo.vo.NewItemsVO">
            <id column="rootCatId" property="rootCatId"/>
            <result column="rootCatName" property="rootCatName"/>
            <result column="slogan" property="slogan"/>
            <result column="catImage" property="catImage"/>
            <result column="bgColor" property="bgColor"/>
    
            <collection property="simpleItemList" ofType="com.imooc.pojo.vo.SimpleItemVO">
                <id column="itemId" property="itemId"/>
                <result column="itemName" property="itemName"/>
                <result column="itemUrl" property="itemUrl"/>
            </collection>
        </resultMap>
    
        <select id="getSixNewItemsLazy" resultMap="myNewItemsVO" parameterType="Map">
            SELECT
                f.id as rootCatId,
                f.`name` as rootCatName,
                f.slogan as slogan,
                f.cat_image as catImage,
                f.bg_color as bgColor,
                i.id as itemId,
                i.item_name as itemName,
                ii.url as itemUrl,
                i.created_time as createdTime
            FROM
                category f
            LEFT JOIN items i ON f.id = i.root_cat_id
            LEFT JOIN items_img ii ON i.id = ii.item_id
            WHERE
                f.type = 1
            AND
                i.root_cat_id = #{paramsMap.rootCatId}
            AND
                ii.is_main = 1
            ORDER BY
                i.created_time
            DESC
            LIMIT 0,6
        </select>
    
    
    </mapper>
    View Code
  • 相关阅读:
    一个母亲一生撒的八个谎言(含泪推荐)!
    穷人必须做的四件大事
    八种人不太可能驰骋职场,有你吗?
    是谁让我如此忧伤?
    拒绝平淡:我用十年的时间名满天下
    高性能利器!华为云MRS ClickHouse重磅推出!
    MySQL 连接为什么挂死了?
    解读登录双因子认证(MFA)特性背后的TOTP原理
    云图说|初识数据仓库服务:云时代的数据分析助手
    号外!5G+X联创营华为云官网上线,5G 创业春天来了!
  • 原文地址:https://www.cnblogs.com/callbin/p/15643653.html
Copyright © 2011-2022 走看看