Java架构师-技术专家练习笔记查询轮播图方法:Pojo实体类:package com.imooc.pojo;import javax.pe rsistence.Column;import javax.persistence.Id;import javax.persist ence.Table;import java.util.Date;/ 表名:carouse/@Table(name = "carouse")public class Carouse { / 主键 / @Id private String i d; / 图片 图片地址 / @Column(name = "image_url") private String im ageUrl; / 背景色 背景颜色 / private String background; / 商品id 商 品id / @Column(name = "item_id") private String itemId; / 商品分 类Id 商品分类id / @Column(name = "cat_id") private String catId; / 轮播图类型 轮播图类型,用于判断,可以根据商品id或者分类进行页面跳转 1:商品 2:分类 / private Inte ger type; / 轮播图展示顺序 轮播图展示顺序,从小到大 / private Integer sort; / 是否展示 是否展示,1:展示,0:不展示 / @Column(name = "is_show") private Inte ger isShow; / 创建时间 创建时间 / @Column(name = "created_time") pri vate Date createdTime; / 更新时间 更新时间 / @Column(name = "updated _time") private Date updatedTime; / 获取主键 @return id - 主键 / public String getId() { return id; } / 设置主键 @param id 主键 / public void setId(String id) { this.id = id; } / 获取图片 图 片地址 @return imageUrl - 图片 图片地址 / public String getImageUrl() { return imageUrl; } / 设置图片 图片地址 @param imageUrl 图片 图片地址 / public void setImageUrl(String imageUrl) { this.imageUrl = im ageUrl; } / 获取背景色 背景颜色 @return background - 背景色 背景颜色 / p ublic String getBackground() { return background; } / 设置背景色 背 景颜色 @param background 背景色 背景颜色 / public void setBackground(S tring background) { this.background = background; } / 获取商品id 商品id @return itemId - 商品id 商品id / public String getItemId() { return itemId; } / 设置商品id 商品id @param itemId 商品id 商品id / public void setItemId(String itemId) { this.itemId = itemId; } / 获取商品分类Id 商品分类id @return catId - 商品分类Id 商品分类id / publi c String getCatId() { return catId; } / 设置商品分类Id 商品分类id @ param catId 商品分类Id 商品分类id / public void setCatId(String catId) { this.catId = catId; } / 获取轮播图类型 轮播图类型,用于判断,可以根据商品id或者分类进行页面跳 转 1:商品 2:分类 @return type - 轮播图类型 轮播图类型,用于判断,可以根据商品id或者分类进行页 面跳转 1:商品 2:分类 / public Integer getType() { return type; } / 设置轮播图类型 轮播图类型,用于判断,可以根据商品id或者分类进行页面跳转 1:商品 2:分类 @param ty pe 轮播图类型 轮播图类型,用于判断,可以根据商品id或者分类进行页面跳转 1:商品 2:分类 / public void setType(Integer type) { this.type = type; } / 获取轮播图展示顺序 轮播图展 示顺序,从小到大 @return sort - 轮播图展示顺序 轮播图展示顺序,从小到大 / public Intege r getSort() { return sort; } / 设置轮播图展示顺序 轮播图展示顺序,从小到大 @pa ram sort 轮播图展示顺序 轮播图展示顺序,从小到大 / public void setSort(Integer sort ) { this.sort = sort; } / 获取是否展示 是否展示,1:展示,0:不展示 @return isShow - 是否展示 是否展示,1:展示,0:不展示 / public Integer getIsShow() { ret urn isShow; } / 设置是否展示 是否展示,1:展示,0:不展示 @param isShow 是否展示 是否展示,1:展示,0:不展示 / public void setIsShow(Integer isShow) { this. isShow = isShow; } / 获取创建时间 创建时间 @return createdTime - 创建 时间 创建时间 / public Date getCreatedTime() { return createdTime; } / 设置创建时间 创建时间 @param createdTime 创建时间 创建时间 / public void setCreatedTime(Date createdTime) { this.createdTime = createdTime ; } / 获取更新时间 更新时间 @return updatedTime - 更新时间 更新时间 / publ ic Date getUpdatedTime() { return updatedTime; } / 设置更新时间 更新时 间 @param updatedTime 更新时间 更新时间 / public void setUpdatedTime( Date updatedTime) { this.updatedTime = updatedTime; }}Mapper接口:pa ckage com.imooc.mapper;import com.imooc.my.mapper.MyMapper;import com.imooc.pojo.Carouse;public interface CarouseMapper extends My Mapper {}Mapper xml: ?> ="com.imooc.pojo.Carouse"> column="id" jdbcType="VARCHAR" property="id" /> n="background" jdbcType="VARCHAR" property="background" /> t column="item_id" jdbcType="VARCHAR" property="itemId" /> t column="cat_id" jdbcType="VARCHAR" property="catId" /> column="type" jdbcType="INTEGER" property="type" /> n="sort" jdbcType="INTEGER" property="sort" /> t column="updated_time" jdbcType="TIMESTAMP" property="updatedTim e" /> Service接口:package com.imooc.service;im port com.imooc.pojo.Carouse;import java.util.List;/ @Descript ion @Author zhangxin @Date 20230116 /public interface Carou seService { / 功能描述: 查询所有轮播图列表 @author zhangxin @date 2023/ 1/16 @params @return java.util.List / public List queryAll(Integer isShow);}Service接口实现:pack age com.imooc.service.impl;import com.imooc.mapper.CarouseMapper; import com.imooc.pojo.Carouse;import com.imooc.service.CarouseSer vice;import org.springframework.beans.factory.annotation.Autowire d;import tk.mybatis.mapper.entity.Example;import java.util.List;/ @Description @Author zhangxin @Date 20230116 /public c lass CarouseServiceImpl implements CarouseService { @Autowired pr ivate CarouseMapper carouseMapper; @Override public List queryAll(Integer isShow) { Example exception =new Example(Carous e.class); //排序 exception.orderBy("sort").desc(); Example.Criteria criteria =exception.createCriteria(); //查询条件 criteria.andEqualTo ("isShow",isShow); List result = carouseMapper.selectByE xample(exception); return result; }}枚举类 YesOrNo :package com.imoo c.enums;/ @Description 性别枚举 @Author zhangxin @Date 202301 13 /public enum YesOrNo { NO(0, "否"), YES(1, "是"); public final Integer type; public final String value; YesOrNo(Integer type, S tring value) { this.type = type; this.value = value; }}Api入口 Inde xController:package com.imooc.controller;import com.imooc.enums.Y esOrNo;import com.imooc.pojo.Carouse;import com.imooc.service.Car ouseService;import com.imooc.utils.IMOOCJSONResult;import io.swag ger.annotations.Api;import org.springframework.beans.factory.anno tation.Autowired;import org.springframework.web.bind.annotation.G etMapping;import org.springframework.web.bind.annotation.RequestM apping;import org.springframework.web.bind.annotation.RestControl ler;import java.util.List;/ @Description @Author zhangxin @Date 20230116 /@Api(value = "首页",tags = "首页展示的相关接口")@RequestM apping("index")@RestControllerpublic class IndexController { @Aut owired private CarouseService carouseService; @GetMapping("/carou sel") public IMOOCJSONResult carousel(){ List list = car ouseService.queryAll(YesOrNo.YES.type); return new IMOOCJSONResul t().ok(list); }}三级分类查询:一级分类查询:Pojo实体类:package com.imooc.pojo;impo rt javax.persistence.Column;import javax.persistence.Id;import ja vax.persistence.Table;/ 表名:category 表注释:商品分类/@Table(name = "category")public class Category { / 主键 分类id主键 / @Id privat e Integer id; / 分类名称 分类名称 / private String name; / 分类类型 分类类型 1:一级大类 2:二级大类:3:三级大类 / private Integer type; / 父ID 父ID / @Column(name = "father_id") private Integer fatherId; / 图标 图标 / private String logo; / 口号 口号 / private String slogan; / 分类图 分类图 / @Column(name = "cat_image") private String catI mage; / 背景颜色 背景颜色 / @Column(name = "bg_color") private Strin g bgColor; / 获取主键 分类id主键 @return id - 主键 分类id主键 / public Integer getId() { return id; } / 设置主键 分类id主键 @param id 主 键 分类id主键 / public void setId(Integer id) { this.id = id; } / 获取分类名称 分类名称 @return name - 分类名称 分类名称 / public String getNam e() { return name; } / 设置分类名称 分类名称 @param name 分类名称 分类名称 / public void setName(String name) { this.name = name; } / 获 取分类类型 分类类型 1:一级大类 2:二级大类:3:三级大类 @return type - 分类类型 分类类型 1:一级 大类 2:二级大类:3:三级大类 / public Integer getType() { return type; } / 设置分类类型 分类类型 1:一级大类 2:二级大类:3:三级大类 @param type 分类类型 分类类型 1:一 级大类 2:二级大类:3:三级大类 / public void setType(Integer type) { this.typ e = type; } / 获取父ID 父ID @return fatherId - 父ID 父ID / pub lic Integer getFatherId() { return fatherId; } / 设置父ID 父ID @param fatherId 父ID 父ID / public void setFatherId(Integer fath erId) { this.fatherId = fatherId; } / 获取图标 图标 @return log o - 图标 图标 / public String getLogo() { return logo; } / 设置图标 图标 @param logo 图标 图标 / public void setLogo(String logo) { th is.logo = logo; } / 获取口号 口号 @return slogan - 口号 口号 / pub lic String getSlogan() { return slogan; } / 设置口号 口号 @para m slogan 口号 口号 / public void setSlogan(String slogan) { this.slo gan = slogan; } / 获取分类图 分类图 @return catImage - 分类图 分类图 / public String getCatImage() { return catImage; } / 设置分类图 分类图 @param catImage 分类图 分类图 / public void setCatImage(String ca tImage) { this.catImage = catImage; } / 获取背景颜色 背景颜色 @retu rn bgColor - 背景颜色 背景颜色 / public String getBgColor() { return bgC olor; } / 设置背景颜色 背景颜色 @param bgColor 背景颜色 背景颜色 / public void setBgColor(String bgColor) { this.bgColor = bgColor; }}Mappe r接口:package com.imooc.mapper;import com.imooc.my.mapper.MyMapper; import com.imooc.pojo.Category;public interface CategoryMapper ex tends MyMapper {}Mapper xml: g="UTF-8"?> space="com.imooc.mapper.CategoryMapper"> t column="name" jdbcType="VARCHAR" property="name" /> umn="type" jdbcType="INTEGER" property="type" /> mn="logo" jdbcType="VARCHAR" property="logo" /> ="bg_color" jdbcType="VARCHAR" property="bgColor" /> Service 接口:package com.imooc.service;import com.imooc.po jo.Category;import java.util.List;/ @Description @Author zh angxin @Date 20230117 /public interface CategoryService { pub lic List queryAllRootLeveCat();}Service实现类:package com. imooc.service.impl;import com.imooc.mapper.CategoryMapper;import com.imooc.pojo.Category;import com.imooc.service.CategoryService; import org.springframework.beans.factory.annotation.Autowired;imp ort org.springframework.stereotype.Service;import tk.mybatis.mapp er.entity.Example;import java.util.List;/ @Description @Aut hor zhangxin @Date 20230117 /@Servicepublic class CategorySer viceImpl implements CategoryService { @Autowired private Category Mapper categoryMapper; @Override public List queryAllRo otLeveCat() { Example example =new Example(Category.class); Examp le.Criteria criteria = example.createCriteria(); criteria.andEq ualTo("type",1); List result = categoryMapper.selectByE xample(example); return result; }}Api接口:package com.imooc.control ler;import com.imooc.enums.YesOrNo;import com.imooc.pojo.Carouse; import com.imooc.pojo.Category;import com.imooc.service.CarouseSe rvice;import com.imooc.service.CategoryService;import com.imooc.u tils.IMOOCJSONResult;import io.swagger.annotations.Api;import io. swagger.annotations.ApiOperation;import org.springframework.beans .factory.annotation.Autowired;import org.springframework.web.bind .annotation.GetMapping;import org.springframework.web.bind.annota tion.RequestMapping;import org.springframework.web.bind.annotatio n.RestController;import java.util.List;/ @Description @Auth or zhangxin @Date 20230116 /@Api(value = "首页",tags = "首页展示的相关 接口")@RequestMapping("index")@RestControllerpublic class IndexCont roller { @Autowired private CarouseService carouseService; @Autow ired private CategoryService categoryService; @ApiOperation(value = "获取首页轮播图列表", notes = "获取首页轮播图列表" ,httpMethod = "GET") @GetMapp ing("/carousel") public IMOOCJSONResult carousel(){ List list = carouseService.queryAll(YesOrNo.YES.type); return new IMO OCJSONResult().ok(list); } / 功能描述: 首页分类展示需求: 1、第一次刷新主页查询大分类 ,渲染展示到首页 2、如果鼠标上移到大分类,则加载其子分类的内容,如果已经存在子分类,则不需要加载(懒加载) @param s @return com.imooc.utils.IMOOCJSONResult / @ApiOperation(valu e = "获取商品分类(一级分类)", notes = "获取商品分类(一级分类)" ,httpMethod = "GET") @ GetMapping("/cats") public IMOOCJSONResult cats(){ List list = categoryService.queryAllRootLeveCat(); return new IMOOCJS ONResult().ok(list); }}二级分类查询:自定义Mapper实现懒加载子分类展示二级实体类:package co m.imooc.pojo.vo;import java.util.List;/ 显示层数据结构 @Description 二级分类 VO @Author zhangxin @Date 20230117 /public class Cate goryVO { private Integer id; private String name; private Stri ng type; private Integer fatherId; private List s ubCatList; public Integer getId() { return id; } public void setI d(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } pu blic String getType() { return type; } public void setType(String type) { this.type = type; } public Integer getFatherId() { retur n fatherId; } public void setFatherId(Integer fatherId) { this.fa therId = fatherId; } public List getSubCatList() { return subCatList; } public void setSubCatList(List O> subCatList) { this.subCatList = subCatList; }}三级实体类:package co m.imooc.pojo.vo;import java.util.List;/ 显示层数据结构 @Description 二级分类 VO @Author zhangxin @Date 20230117 /public class SubC ategoryVO { private Integer subId; private String subName; priv ate String subType; private Integer subFatherId; public Integer getSubId() { return subId; } public void setSubId(Integer subId) { this.subId = subId; } public String getSubName() { return subN ame; } public void setSubName(String subName) { this.subName = su bName; } public String getSubType() { return subType; } public vo id setSubType(String subType) { this.subType = subType; } public Integer getSubFatherId() { return subFatherId; } public void setS ubFatherId(Integer subFatherId) { this.subFatherId = subFatherId; }}Mapper接口:package com.imooc.mapper;import java.util.List;/ 功 能描述: 自定义 分类mapper @params @return /public interface Category MapperCustom { / 功能描述: 根据 父类的编码,查询二级目录 @params @return jav a.util.List / public List getSubCatList(Integer rootCatId);}Mapp er xml: LIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/ mybatis-3-mapper.dtd"> /> < result column="type" jdbcType="INTEGER" property="type" /> t column="fatherId" jdbcType="INTEGER" property="fatherId" /> - collection 标签:用于定义关联的list集合类型的封装规则 property 对应三级分类的list属性名 ofTy pe 集合的类型,三级分类的VO --> apper>Service接口:package com.imooc.service;import com.imooc.pojo.C ategory;import com.imooc.pojo.vo.CategoryVO;import java.util.List ;/ @Description @Author zhangxin @Date 20230117 /public interface CategoryService { / 功能描述: 查询所有一级分类 @params @ret urn java.util.List / public List ry> queryAllRootLeveCat(); / 功能描述: 根据一级分类id查询子分类信息 @params @return java.util.List / public List getSubCatList( Integer rootCatId);}Service接口实现:package com.imooc.service.impl;im port com.imooc.mapper.CategoryMapper;import com.imooc.mapper.Cate goryMapperCustom;import com.imooc.pojo.Category;import com.imooc. pojo.vo.CategoryVO;import com.imooc.service.CategoryService;impor t org.springframework.beans.factory.annotation.Autowired;import o rg.springframework.stereotype.Service;import org.springframework. transaction.annotation.Propagation;import org.springframework.tra nsaction.annotation.Transactional;import tk.mybatis.mapper.entity .Example;import java.util.List;/ @Description @Author zhang xin @Date 20230117 /@Servicepublic class CategoryServiceImpl implements CategoryService { @Autowired private CategoryMapper ca tegoryMapper; @Autowired private CategoryMapperCustom categoryMap perCustom; @Transactional(propagation = Propagation.SUPPORTS) @Ov erride public List queryAllRootLeveCat() { Example exam ple =new Example(Category.class); Example.Criteria criteria = e xample.createCriteria(); criteria.andEqualTo("type",1); List gory> result = categoryMapper.selectByExample(example); return re sult; } @Transactional(propagation = Propagation.SUPPORTS) @Overr ide public List getSubCatList(Integer rootCatId) { re turn categoryMapperCustom.getSubCatList(rootCatId); }}Api接口:packa ge com.imooc.controller;import com.imooc.enums.YesOrNo;import com .imooc.pojo.Carouse;import com.imooc.pojo.Category;import com.imo oc.pojo.vo.CategoryVO;import com.imooc.service.CarouseService;imp ort com.imooc.service.CategoryService;import com.imooc.utils.IMOO CJSONResult;import io.swagger.annotations.Api;import io.swagger.a nnotations.ApiOperation;import io.swagger.annotations.ApiParam;im port org.springframework.beans.factory.annotation.Autowired;impor t org.springframework.web.bind.annotation.GetMapping;import org.s pringframework.web.bind.annotation.PathVariable;import org.spring framework.web.bind.annotation.RequestMapping;import org.springfra mework.web.bind.annotation.RestController;import java.util.List;/ @Description @Author zhangxin @Date 20230116 /@Api(val ue = "首页",tags = "首页展示的相关接口")@RequestMapping("index")@RestControl lerpublic class IndexController { @Autowired private CarouseServi ce carouseService; @Autowired private CategoryService categorySer vice; @ApiOperation(value = "获取首页轮播图列表", notes = "获取首页轮播图列表" ,htt pMethod = "GET") @GetMapping("/carousel") public IMOOCJSONResult carousel(){ List list = carouseService.queryAll(YesOrNo. YES.type); return new IMOOCJSONResult().ok(list); } / 功能描述: 首 页分类展示需求: 1、第一次刷新主页查询大分类,渲染展示到首页 2、如果鼠标上移到大分类,则加载其子分类的内容,如果已经存 在子分类,则不需要加载(懒加载) @params @return com.imooc.utils.IMOOCJSONRes ult / @ApiOperation(value = "获取商品分类(一级分类)", notes = "获取商品分类(一级分类 )" ,httpMethod = "GET") @GetMapping("/cats") public IMOOCJSONResu lt cats(){ List list = categoryService.queryAllRootLeve Cat(); return new IMOOCJSONResult().ok(list); } @ApiOperation(val ue = "获取商品子分类", notes = "获取商品子分类" ,httpMethod = "GET") @GetMappin g("/subCat/{rootCatId}") //路径参数 public IMOOCJSONResult subCat( @A piParam(name = "rootCatId",value = "一级分类ID",required = true) @Pat hVariable Integer rootCatId){ if(rootCatId==null){ return new IM OOCJSONResult().errorMsg("分类不存在!"); } List list = cat egoryService.getSubCatList(rootCatId); return new IMOOCJSONResult ().ok(list); }}分页实现-mybatis-pagehelper插件SpringBoot整合mybatis-pageh elperPom.xml 文件: roupId>com.github.pagehelper pagehelper-spr ing-boot-starter 1.2.5 cy>Yml 文件配置:#分页插件配置pagehelper: h elperDialect: mysql supportMethodsArguments: true使用分页插件,在查询前使用分页插 件,原理:统一拦截sql 为其提供分页功能/ page 第几页 pageSize 每页显示条数 /PageHelper.sta rtPage(page,pageSize);List list = 查询出的list数据;PageInfo> pageLis t =new PageInfo<>(list); PagedGridResult grid = new PagedGridResu lt(); grid.setPage(page); grid.setRows(list); grid.setTotal(pageL ist.getPages()); grid.setRecords(pageList.getTotal());提取此代码为方法:pr ivate PagedGridResult setterPagedGrid(List> list ,Integer page) { PageInfo> pageList =new PageInfo<>(list); PagedGridResult gri d = new PagedGridResult(); grid.setPage(page); grid.setRows(list) ; grid.setTotal(pageList.getPages()); grid.setRecords(pageList.ge tTotal()); return grid; }通用脱敏工具类package com.imooc.utils;/ @D escription 通用脱敏工具类 可用于: 用户名 手机号 邮箱 地址等 @A uthor zhangxin @Date 20230128 /public class DesensitizationUt il { private static final int SIZE = 6; private static final Stri ng SYMBOL =""; public static void main(String[] args) { String n ame = commonDisplay("小小孩"); String mobile = commonDisplay("131000 00005"); String mail = commonDisplay("1163000000@qq.com"); String address = commonDisplay("湖南省湘潭市岳塘区岳塘大道"); System.out.println(nam e); System.out.println(mobile); System.out.println(mail); System. out.println(address); } / 通用脱敏方法 @param value @return / public static String commonDisplay(String value) { if (null == va lue || "".equals(value)) { return value; } int len = value.length (); int pamaone = len / 2; int pamatwo = pamaone - 1; int pamathr ee = len % 2; StringBuilder stringBuilder = new StringBuilder(); if (len <= 2) { if (pamathree == 1) { return SYMBOL; } stringBuil der.append(SYMBOL); stringBuilder.append(value.charAt(len - 1)); } else { if (pamatwo <= 0) { stringBuilder.append(value.substring (0, 1)); stringBuilder.append(SYMBOL); stringBuilder.append(value .substring(len - 1, len)); } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) { int pamafive = (len - SIZE) / 2; stringBuilder.app end(value.substring(0, pamafive)); for (int i = 0; i < SIZE; i++) { stringBuilder.append(SYMBOL); } if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) { stringBuilder.append(value.substring(len - pamafive, len)); } else { stringBuilder.append(value.substring(len - (pamafive + 1), len)); } } else { int pamafour = len - 2; stringBuilder.append(value.substring(0, 1)); for (int i = 0; i < pamafour; i++) { stringBuilder.append(SYMBOL); } stringBuilder.append(value.substring(len - 1, len)); } } return stringBuilder.toString(); }}模糊查询:XML中的sql拼写:%% 中间使用的是$占位符来拼接where 1=1 and ic.name like ''%${map.name}%'' 条件选择的语句:’c’ 单字符中的单引号:使用转义字符: "order by i.sell_counts desc tempSpec.price_discount asc i.item_name asc 前端轮询:定时任务启动类上加 @EnableScheduling 定时任务才会被执行:package com.penghaisoft.wms.config;import com.ctc.wstx.util.DataUtil;import org.apache.cxf.service.invoker.SingletonFactory;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;import java.util.Date;/ @Description @Author zhangxin @Date 2023-02-01 /@Component //组件public class OrderJob { / 功能描述: 定时关闭订单 @author zhangxin @date @params @return void / @Scheduled( cron = "0/3 ?") //定时任务注解 每隔3秒 public void autoCloseOrder(){ System.out.println("执行定时任务,当前时间为:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); }}多环境配置:例如:挂载不同端口: |
|