1、将数据导入Map集合
public Map> getMap(){
//1.查询全部数据库记录
List list = itemCatMapper.selectList(null);
//2.将list集合数据,封装到Map集合中
Map> map = new HashMap<>();
for (ItemCat itemCat : list) { //遍历所有的数据
int parentId = itemCat.getParentId();
//containsKey: 检查key值存不存在
if (map.containsKey(parentId)) { //key存在,将数据追加即可
map.get(parentId).add(itemCat);
} else {//key不存在 我是当前parentId的第一个子级
List temp = new ArrayList<>();
temp.add(itemCat);
map.put(parentId,temp);
}
}
return map;
}
2、获取一级、二级、三级菜单
//Map二、application文件子级> @Override public List findItemCatList(Integer level) { Map > map = getMap(); if (level == 1){ return map.get(0); } if (level == 2){ //查询一级和二级 return getTwoList(map); } return getThreeList(map); } //获取一级 / 二级 / 三级菜单 private List getThreeList(Map > map) { //1.获取一级和二级 List oneList = getTwoList(map);//一级数据不可能为空 //2.遍历一级集合 获取二级的数据 for (ItemCat oneItemCat : oneList){ //二级数据可能为null List twoList = oneItemCat.getChildren(); if (twoList == null || twoList.size() == 0){ //表示数据没有二级 本次循环应该终止,开始下一次循环 continue; } //二级列表一定有值,可以查询三级列表 for (ItemCat twoItemCat : twoList){ //获取二级Id 查询三级数据 int parentId = twoItemCat.getId(); List threeList = map.get(parentId); //将三级数据保存到二级数据中 twoItemCat.setChildren(threeList); } } return oneList; } //获取二级菜单 private List getTwoList(Map > map) { //1.获取一级菜单 List oneList = map.get(0); //2.遍历数据 for (ItemCat oneItemCat : oneList){ //key int parentId = oneItemCat.getId(); //根据父级查询子级 List twoList = map.get(parentId); //封装子级数据 oneItemCat.setChildren(twoList); } return oneList; }
#配置端口号
server:
port: 8091
#管理数据源
spring:
datasource:
#高版本驱动使用
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
#设定用户名和密码
username: root
password: 2323
#SpringBoot整合Mybatis-plus
mybatis-plus:
#指定别名包
type-aliases-package: com.jt.pojo
#扫描指定路径下的映射文件
mapper-locations: classpath:/mappers/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
# 一二级缓存默认开始 所以可以简化
#打印mysql日志
logging:
level:
com.jt.mapper: debug
三、自动填充操作时间
basePojo类:数据创建时间以及修改时间
//pojo基类,完成2个任务,2个日期,实现序列化
@Data
@Accessors(chain=true)//控制是否开启链式加载结构
public class basePojo implements Serializable{
//新增操作时,自动填充
@TableField(fill = FieldFill.INSERT)
private Date created; //表示入库时需要赋值
//@TableField MybatisPlus注解 标识属性是否存在,及名称是否一致
//新增-修改操作时,自动填充
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updated; //表示入库/更新时赋值.
}
MyMetObjectHandler配置类:实现自动填充
@Component //将当前类未来的对象交给容器管理
public class MyMetObjectHandler implements metaObjectHandler {
//优势: 以后操作数据表无需手动操作时间!!! 都会自动填充
@Override
public void insertFill(metaObject metaObject) {
Date date = new Date();
this.setFieldValByName("created", date, metaObject);
this.setFieldValByName("updated", date, metaObject);
}
@Override
public void updateFill(metaObject metaObject) {
this.setFieldValByName("updated", new Date(), metaObject);
}
}
四、商品分页展现
1、PageResult类
@Data
@Accessors(chain = true)
public class PageResult implements Serializable {
private String query; //用户查询的数据
private Integer pageNum; //查询页数
private Integer pageSize; //查询条数
private Long total; //查询总记录数
private Object rows; //分页查询的结果
}
2、分页步骤
@Override
public PageResult getItemList(PageResult pageResult) {
//1.用户查询的where条件
QueryWrapper- queryWrapper = new QueryWrapper<>();
boolean flag = StringUtils.hasLength(pageResult.getQuery());//判断字符串是否为空
queryWrapper.like(flag,"title", pageResult.getQuery());
//2.page是MP分页对象 参数1:页数 参数2:条数
Page
- page =
new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
//MP在内部自动的获取 总数和分页后的结果
page = itemMapper.selectPage(page,queryWrapper);
//3.获取数据之后封装
long total = page.getTotal();
List
- rows = page.getRecords();
return pageResult.setTotal(total).setRows(rows);
}
3、添加配置类
@Configuration //添加配置类
public class MybatisPlusConfig {
// 最新版
// 核心功能: 指定数据库版本,自动生成Sql
@Bean //标识该方法的返回值交给Spring容器管理
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MARIADB));
return interceptor;
}
}



