自定义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); }

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); } }

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); }

<?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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理