定义全局异常处理机制
package cn.tedu.testjt.config;
import cn.tedu.testjt.vo.SysResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
//定义全局异常处理机制,返回值json串
@RestControllerAdvice
public class MyExceptionAdvice {
//异常类型可以自定义
@ExceptionHandler(RuntimeException.class)//标识异常的类型,匹配就拦截
public Object exception(Exception e){
e.printStackTrace();
return SysResult.fail();//遇到异常直接提醒失败
}
}
测试
@GetMapping("/a")
public void a(){
int i=1/0;
System.out.println(i);
}
前端输出结果
{
"statue": 201,
"msg": "调用失败",
"data": null
}
MP自动填充
//需要自动填充的数据
package cn.tedu.testjt.pojo;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import java.util.Date;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Accessors(chain = true)
@Data
public class BasePojo implements Serializable {
@TableField(fill = FieldFill.INSERT)//入库时赋值
private Date created;
@TableField(fill = FieldFill.INSERT_UPDATE)//更新时赋值
private Date update;
}
package cn.tedu.testjt.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class MyMeatObjectHandler implements MetaObjectHandler {
// 当数据库做更新操作时自动调用
// MetaObject对象时mp自动充填的配置
@Override
public void insertFill(MetaObject metaObject) {
Date date=new Date();
this.setFieldValByName("create", date, metaObject);
this.setFieldValByName("update", date, metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
Date date=new Date();
this.setFieldValByName("update", date, metaObject);
}
}
使用时要继承BasePojo
注意:增删改操作时最好加上@Transactional作为事务自动回滚
三级商品查询:未优化版本
先准备测试类ItemCat封装数据
package cn.tedu.testjt.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.List;
@TableName("item_cat")
@Data
@Accessors(chain = true)
public class ItemCat {
@TableId(type= IdType.AUTO)
private Integer id;
private Integer parentId;
private String name;
private boolean status;
private Integer level;
@TableField(exist = false)
private List<ItemCat> children;//封装下一级商品信息
}
编写controller层
package cn.tedu.testjt.conrtoller;
import cn.tedu.testjt.pojo.ItemCat;
import cn.tedu.testjt.service.ItemCatService;
import cn.tedu.testjt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/itemcat")
public class ItemCatController {
@Autowired
ItemCatService itemCatService;
//按等级查询商品分类
@GetMapping("/findItemCat/{level}")
public SysResult findItemCat(ItemCat itemCat){
List<ItemCat> list=itemCatService.findItemCat(itemCat);
return SysResult.Success(list);
}
}
编写service层
package cn.tedu.testjt.service;
import cn.tedu.testjt.pojo.ItemCat;
import java.util.List;
public interface ItemCatService {
List<ItemCat> findItemCat(ItemCat itemCat);
}
package cn.tedu.testjt.service;
import cn.tedu.testjt.mapper.ItemCatMapper;
import cn.tedu.testjt.pojo.ItemCat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ItemCatServiceImpl implements ItemCatService{
@Autowired
ItemCatMapper itemCatMapper;
@Override
public List<ItemCat> findItemCat(ItemCat itemCat) {
//查询3级商品嵌套,思路:一级套二级,二级套三级,一级id是二级父id
long start=System.currentTimeMillis();//开始查询时间
//先查一级商品
QueryWrapper queryWrapper=new QueryWrapper();
queryWrapper.eq("parent_id", 0);
List <ItemCat>onelist = itemCatMapper.selectList(queryWrapper);
//查询二级
for (ItemCat onecat : onelist) {
queryWrapper.clear();//清空上一次查询条件
queryWrapper.eq("parent_id", onecat.getId());
List <ItemCat> twolist = itemCatMapper.selectList(queryWrapper);
//查询三级,在二级的基础上
for (ItemCat twocat : twolist) {
queryWrapper.clear();
queryWrapper.eq("parent_id", twocat.getId());
List<ItemCat> threelist=itemCatMapper.selectList(queryWrapper);
// //将三级商品信息放入二级目录下
twocat.setChildren(threelist);
}
//将二级商品信息塞进一级目录下
onecat.setChildren(twolist);
}
//查询完毕时间
long end=System.currentTimeMillis();
System.out.println("查询时间为"+(end-start)+"毫秒");
return onelist;
}
}
编写Mappr层
package cn.tedu.testjt.mapper;
import cn.tedu.testjt.pojo.ItemCat;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ItemCatMapper extends BaseMapper<ItemCat> {
}
前端发起请求进行测试:localhost:8091/itemcat/findItemCat/3
//查询时间为609毫秒
优化查询方法
//查询商品分类的优化
@GetMapping("/findItemCat2/{level}")
public SysResult findItemCat2(@PathVariable Integer level){
List <ItemCat> list=itemCatService.findItemCat2(level);
return SysResult.Success(list);
}
//查询方法二
// Integer代表父级id List<ItemCat>>代表下一级数据
public Map<Integer, List<ItemCat>> map() {
long start=System.currentTimeMillis();//记录开始时间
Map<Integer, List<ItemCat>> map=new HashMap<>();
//先将所有数据查出来
List<ItemCat> itemCatlist =itemCatMapper.selectList(null);
for (ItemCat itemCat : itemCatlist) {
//查询父类ID
int key=itemCat.getParentId();
//查询map中有没有这个key
if (map.containsKey(key)){//存在该key
List<ItemCat> list = map.get(key);//根据父类ID对应的key来查询到所对应的ItemCat集合
list.add(itemCat);//将符合该条件的商品对象封装进去该集合
//例如:当查到了key=0 将该itemCat存进map,则该list里面装的就都是一级商品数据
//<0,<一级商品集合>>
}else {
List <ItemCat> list=new ArrayList<>();
list.add(itemCat);
map.put(key, list);
}
long en=System.currentTimeMillis();
System.out.println("耗时:"+(en-start));
}
return map;
}
//先将1/2查出来 Integer是父类id 一级id等于二级父类id
public List<ItemCat> list2( Map<Integer,List<ItemCat>> map){
//先查一级
List<ItemCat> onelist = map.get(0);
//查到一级商品集合
for (ItemCat oneitemCat : onelist) {
List<ItemCat> twolist = map.get(oneitemCat.getId());//一级id等于二级父类id
oneitemCat.setChildren(twolist);
}
return onelist;
}
// 查询1/2/3级
public List<ItemCat> list3( Map<Integer,List<ItemCat>> map){
long star=System.currentTimeMillis();
List<ItemCat> one=list2(map);//先获取一级菜单
for (ItemCat oneitemCat : one) {
List<ItemCat> two = oneitemCat.getChildren();//获取2级菜单
if (two==null||two.size()==0){
continue;//判断该是否有下一级菜单
}
for (ItemCat twoitemCat : two) {
List<ItemCat> three= map.get(twoitemCat.getId());//根据二级菜单查出三级菜单
twoitemCat.setChildren(three);//把三级菜单塞进二级
}
}
long end=System.currentTimeMillis();
System.out.println(end-star);
return one;
}
@Override
public List<ItemCat> findItemCat2(Integer level){
Map<Integer,List<ItemCat>> map=map();
//查询一级菜单
if (level==1){
return map.get(0);
}
//查询1/2级菜单,即1级下的2级,调用list2
if(level==2){
return list2(map);
}
//查询三级目录
return list3(map);
}
前端发起请求进行测试:localhost:8091/itemcat/findItemCat/3
//查询时间为16毫秒