package com.itheima.reggie.domain;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import sun.tracing.dtrace.DTraceProviderFactory;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
菜品
*/
@Data
public class Dish implements Serializable {
private Long id;
//菜品名称
private String name;
//菜品分类id
private Long categoryId;
//菜品价格
private BigDecimal price;
//商品码
private String code;
//图片
private String image;
//描述信息
private String description;
//0 停售 1 起售
private Integer status;
//顺序
private Integer sort;
@TableField (fill = FieldFill.INSERT)
private Date createTime;//创建时间
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;//更新时间
@TableField(fill = FieldFill.INSERT)
private Long createUser;//创建用户
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;//更新用户
//菜品分类名称
@TableField(exist = false)
private String categoryName;
//设置一个口味集合
//菜品口味列表
@TableField(exist = false)
private List flavors=new ArrayList<>();
@TableField(exist = false)
private Integer copies;
}
package com.itheima.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.reggie.domain.DishFlavor;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DishFlavorMapper extends BaseMapper {
}
package com.itheima.reggie.service.impl;
import com.itheima.reggie.service.DishFlavorService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class DishFlavorServiceImpl implements DishFlavorService {
}
package com.itheima.reggie.service;
public interface DishFlavorService {
}
package com.itheima.reggie;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication//启动类
@MapperScan(“com.itheima.reggie.mapper”)//扫描mybatis
@Slf4j//lombok提供了打印日志
//@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class WebManageApplication {
public static void main(String[] args) {
SpringApplication.run(WebManageApplication.class,args);//选没有[]的那个
log.info(“项目启动成功”);
}
@Bean //分页插件配置
public MybatisPlusInterceptor plusInterceptor() {
//创建MybatisPlus拦截器
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//添加分页拦截器
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//返回
return mybatisPlusInterceptor;
}
}
package com.itheima.reggie.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class DishServiceImpl implements DishService {
@Autowired
private DishMapper dishMapper;
@Autowired
private CategoryMapper categoryMapper;
@Autowired
private DishFlavorMapper dishFlavorMapper;
@Override
public IPage<Dish> findPage(int page, int pageSize) {
//1.分页查询所有菜品
IPage<Dish> pg=new Page<Dish>(page,pageSize);
QueryWrapper<Dish> qw = new QueryWrapper<>();
pg = dishMapper.selectPage(pg, qw);
List<Dish> list = pg.getRecords();//分页查询的10条菜品
if (list.size()>0){
//2.查询菜品所属分类
for (Dish dish : list){
Category category = categoryMapper.selectById(dish.getCategoryId());
//组装当前菜品
dish.setCategoryName(category.getName());
//查询菜品的多个口味选择
QueryWrapper<DishFlavor> qw2 = new QueryWrapper<>();
qw2.eq("dish_id",dish.getId());
List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(qw2);
//组装当前菜品
dish.setFlavors(dishFlavorList);
}
}
return pg;
}
}
模糊查询+分页
package com.itheima.reggie.service.impl;
import com.itheima.reggie.service.DishFlavorService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class DishFlavorServiceImpl implements DishFlavorService {
}
package com.itheima.reggie.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.domain.Dish;
public interface DishService {
IPage findPage(int page, int pageSize,String name);
}
package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DishController {
@Autowired
private DishService dishService;
@GetMapping("/dish/page")
public ResultInfo findPage(@RequestParam(defaultValue = "1")int page,
@RequestParam(defaultValue = "10")int pageSize,
String name){
System.out.println(page);
System.out.println(pageSize);
IPage<Dish> pg = dishService.findPage(page, pageSize,name);
return ResultInfo.success(pg);
//
}
}