删除、修改菜品

This commit is contained in:
zhangsan 2024-04-11 12:56:00 +08:00
parent 211267c637
commit 75175519ac
19 changed files with 515 additions and 66 deletions

View File

@ -4,65 +4,99 @@ import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.sky.properties.AliOssProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
@Data
@AllArgsConstructor
@Slf4j
@Component
public class AliOssUtil {
@Autowired
private AliOssProperties aliOssProperties;
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
// private String endpoint;
// private String accessKeyId;
// private String accessKeySecret;
// private String bucketName;
//
// /**
// * 文件上传
// *
// * @param bytes
// * @param objectName
// * @return
// */
// public String upload(byte[] bytes, String objectName) {
//
// // 创建OSSClient实例
// OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//
// try {
// // 创建PutObject请求
// ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
// } catch (OSSException oe) {
// System.out.println("Caught an OSSException, which means your request made it to OSS, "
// + "but was rejected with an error response for some reason.");
// System.out.println("Error Message:" + oe.getErrorMessage());
// System.out.println("Error Code:" + oe.getErrorCode());
// System.out.println("Request ID:" + oe.getRequestId());
// System.out.println("Host ID:" + oe.getHostId());
// } catch (ClientException ce) {
// System.out.println("Caught an ClientException, which means the client encountered "
// + "a serious internal problem while trying to communicate with OSS, "
// + "such as not being able to access the network.");
// System.out.println("Error Message:" + ce.getMessage());
// } finally {
// if (ossClient != null) {
// ossClient.shutdown();
// }
// }
//
// //文件访问路径规则 https://BucketName.Endpoint/ObjectName
// StringBuilder stringBuilder = new StringBuilder("https://");
// stringBuilder
// .append(bucketName)
// .append(".")
// .append(endpoint)
// .append("/")
// .append(objectName);
//
// log.info("文件上传到:{}", stringBuilder.toString());
//
// return stringBuilder.toString();
// }
public String upload(MultipartFile file) throws IOException, com.aliyuncs.exceptions.ClientException {
InputStream inputStream = file.getInputStream();
// 避免文件覆盖
String originalFilename = file.getOriginalFilename();
String extname = originalFilename.substring(originalFilename.lastIndexOf("."));//文件扩展名
String fileName = UUID.randomUUID().toString() + extname;
/**
* 文件上传
*
* @param bytes
* @param objectName
* @return
*/
public String upload(byte[] bytes, String objectName) {
//上传文件到 OSS
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); //从环境变量中获取
OSS ossClient = new OSSClientBuilder().build(aliOssProperties.getEndpoint(), credentialsProvider);
PutObjectRequest putObjectRequest = new PutObjectRequest(aliOssProperties.getBucketName(), fileName, inputStream);
PutObjectResult result = ossClient.putObject(putObjectRequest);
// 创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
// 创建PutObject请求
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
//文件访问路径规则 https://BucketName.Endpoint/ObjectName
StringBuilder stringBuilder = new StringBuilder("https://");
stringBuilder
.append(bucketName)
.append(".")
.append(endpoint)
.append("/")
.append(objectName);
log.info("文件上传到:{}", stringBuilder.toString());
return stringBuilder.toString();
//文件访问路径
String url = aliOssProperties.getEndpoint().split("//")[0] + "//" + aliOssProperties.getBucketName() + "." + aliOssProperties.getEndpoint().split("//")[1] + "/" + fileName;
// 关闭ossClient
ossClient.shutdown();
return url;// 把上传到oss的路径返回
}
}

View File

@ -0,0 +1,18 @@
package com.sky.annotation;
import com.sky.enumeration.OperationType;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 自定义注解用于标识某个方法需要进行功能字段自动填充处理
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
//update insert
OperationType value();
}

View File

@ -0,0 +1,67 @@
package com.sky.aspect;
import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
/**
* 自定义切面实现公共字段自动填充
*/
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
@Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
public void autoFillPointCut(){}
/**
* 前置通知
*/
@Before("autoFillPointCut()")
public void autoFill(JoinPoint joinPoint) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
log.info("开始进行公共字段自动填充...");
//获取数据库操作类型update/insert
MethodSignature signature=(MethodSignature)joinPoint.getSignature();
AutoFill autoFill=signature.getMethod().getAnnotation(AutoFill.class);
OperationType operationType=autoFill.value(); // 获得数据库操作类型
//获取实体对象
Object[] args=joinPoint.getArgs();
if(args==null || args.length==0)
return;
Object entity=args[0]; //规定第一个参数一定是实体对象 employee category...
//准备赋值的数据
LocalDateTime now=LocalDateTime.now();
Long currentId= BaseContext.getCurrentId();
//根据不同的操作类型为对应的属性赋值
if(operationType==OperationType.INSERT){
Method setCreateTime= entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME,LocalDateTime.class);
Method setCreateUser=entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER,Long.class);
Method setUpdateTime=entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
Method setUpdateUser=entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);
setCreateTime.invoke(entity,now);
setCreateUser.invoke(entity,currentId);
setUpdateTime.invoke(entity,now);
setUpdateUser.invoke(entity,currentId);
}else{
Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
//通过反射为对象属性赋值
setUpdateTime.invoke(entity,now);
setUpdateUser.invoke(entity,currentId);
}
log.info(signature.getName());
}
}

View File

@ -23,13 +23,13 @@ public class CategoryController {
* 新增菜品分类
*/
@PostMapping
@ApiOperation("新增菜品分类")
@ApiOperation("新增分类")
public Result addCategory(CategoryDTO categoryDTO){
categoryService.addCategory(categoryDTO);
return Result.success();
}
@GetMapping("/page")
@ApiOperation("菜品分类分页查询")
@ApiOperation("分类分页查询")
public Result<PageResult> list(CategoryPageQueryDTO categoryPageQueryDTO){
PageResult pageResult=categoryService.list(categoryPageQueryDTO);
return Result.success(pageResult);

View File

@ -0,0 +1,32 @@
package com.sky.controller.admin;
import com.aliyuncs.exceptions.ClientException;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {
@Autowired
private AliOssUtil aliOssUtil;
@PostMapping("/upload")
@ApiOperation("文件上传")
public Result<String> upload(MultipartFile file) throws IOException, ClientException {
log.info("文件上传:{}",file);
String url=aliOssUtil.upload(file);
return Result.success(url);
}
}

View File

@ -0,0 +1,54 @@
package com.sky.controller.admin;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {
@Autowired
private DishService dishService;
@PostMapping
@ApiOperation("新增菜品")
public Result addDish(@RequestBody DishDTO dishDTO){
log.info("新增菜品");
System.out.println("dishDTO:"+dishDTO);
dishService.addDish(dishDTO);
return Result.success();
}
@GetMapping("/page")
@ApiOperation("菜品分类查询")
public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
PageResult pageResult=dishService.pageQuery(dishPageQueryDTO);
return Result.success(pageResult);
}
@DeleteMapping
@ApiOperation("删除菜品")
public Result deleteDish(Long[] ids){
log.info("批量删除菜品");
dishService.deleteBatch(ids);
return Result.success();
}
@GetMapping("/{id}")
@ApiOperation("根据id查询菜品")
public Result<DishVO> queryByIdWithFlavor(@PathVariable Long id){
DishVO dishVO=dishService.queryByIdWithFlavor(id);
return Result.success(dishVO);
}
@PutMapping
@ApiOperation("更新菜品")
public Result update(@RequestBody DishDTO dishDTO){
dishService.update(dishDTO);
return Result.success();
}
}

View File

@ -1,8 +1,10 @@
package com.sky.mapper;
import com.sky.annotation.AutoFill;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.enumeration.OperationType;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
@ -15,11 +17,12 @@ public interface CategoryMapper {
@Insert("insert into category (type,name,sort,status,create_time,update_time,create_user,update_user)" +
"values" +
"(#{type},#{name},#{sort},#{status},#{createTime},#{updateTime},#{createUser},#{updateUser})")
@AutoFill(OperationType.INSERT)
void addCategory(Category category);
List<Category> list(CategoryPageQueryDTO categoryPageQueryDTO);
@Delete("delete from category where id=#{id}")
void deleteById(Integer id);
@AutoFill(OperationType.UPDATE)
void update(Category category);
@Select("select * from category where type=#{type}")
List<Category> listByType(Integer type);

View File

@ -0,0 +1,19 @@
package com.sky.mapper;
import com.sky.entity.DishFlavor;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface DishFlavorMapper {
//insert into dish_flavor(xx,xx,xx) values (xx,xx,xx),(xx,xx,xx) 批量添加
void insertBatch(List<DishFlavor> flavorList);
@Delete("delete from dish_flavor where dish_id=#{id}")
void deleteByDishId(Long id);
@Select("select * from dish_flavor where dish_id=#{id}")
List<DishFlavor> queryByDishId(Long id);
}

View File

@ -0,0 +1,26 @@
package com.sky.mapper;
import com.sky.annotation.AutoFill;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface DishMapper {
@AutoFill(OperationType.INSERT)
void insert(Dish dish);
List<DishVO> list(DishPageQueryDTO dishPageQueryDTO);
@Select("select * from dish where id=#{id}")
Dish queryById(Long id);
@Delete("delete from dish where id=#{id}")
void deleteById(Long id);
@AutoFill(OperationType.UPDATE)
void update(Dish dish);
}

View File

@ -1,8 +1,10 @@
package com.sky.mapper;
import com.sky.annotation.AutoFill;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.StartOrStopDTO;
import com.sky.entity.Employee;
import com.sky.enumeration.OperationType;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@ -28,6 +30,7 @@ public interface EmployeeMapper {
@Insert("insert into employee (name, username, password, phone, sex, id_number, create_time, update_time, create_user, update_user,status) " +
"values " +
"(#{name},#{username},#{password},#{phone},#{sex},#{idNumber},#{createTime},#{updateTime},#{createUser},#{updateUser},#{status})")
@AutoFill(OperationType.INSERT)
void save(Employee employee);
/**
@ -37,6 +40,6 @@ public interface EmployeeMapper {
List<Employee> list(EmployeePageQueryDTO employeePageQueryDTO);
@Select("select * from employee where id=#{id};")
Employee queryById(Integer id);
@AutoFill(OperationType.UPDATE)
void update(Employee employee);
}

View File

@ -0,0 +1,11 @@
package com.sky.mapper;
import com.sky.entity.SetmealDish;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface Setmeal_dishMapper {
@Select("select * from setmeal_dish where dish_id=#{id}")
SetmealDish queryByDishId(Long id);
}

View File

@ -0,0 +1,18 @@
package com.sky.service;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.vo.DishVO;
public interface DishService {
void addDish(DishDTO dishDTO);
PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO);
void deleteBatch(Long[] ids);
DishVO queryByIdWithFlavor(Long id);
void update(DishDTO dishDTO);
}

View File

@ -26,10 +26,10 @@ public class CategoryServiceImpl implements CategoryService {
public void addCategory(CategoryDTO categoryDTO) {
Category category=new Category();
BeanUtils.copyProperties(categoryDTO,category);
category.setCreateTime(LocalDateTime.now());
category.setUpdateTime(LocalDateTime.now());
category.setCreateUser(BaseContext.getCurrentId());
category.setUpdateUser(BaseContext.getCurrentId());
// category.setCreateTime(LocalDateTime.now());
// category.setUpdateTime(LocalDateTime.now());
// category.setCreateUser(BaseContext.getCurrentId());
// category.setUpdateUser(BaseContext.getCurrentId());
category.setStatus(StatusConstant.DISABLE);
categoryMapper.addCategory(category);
}
@ -52,8 +52,8 @@ public class CategoryServiceImpl implements CategoryService {
public void update(CategoryDTO categoryDTO) {
Category category=new Category();
BeanUtils.copyProperties(categoryDTO,category);
category.setUpdateTime(LocalDateTime.now());
category.setUpdateUser(BaseContext.getCurrentId());
// category.setUpdateTime(LocalDateTime.now());
// category.setUpdateUser(BaseContext.getCurrentId());
categoryMapper.update(category);
}
@ -62,8 +62,8 @@ public class CategoryServiceImpl implements CategoryService {
Category category=new Category();
category.setStatus(status);
category.setId(id);
category.setUpdateTime(LocalDateTime.now());
category.setUpdateUser(BaseContext.getCurrentId());
// category.setUpdateTime(LocalDateTime.now());
// category.setUpdateUser(BaseContext.getCurrentId());
categoryMapper.update(category);
}

View File

@ -0,0 +1,100 @@
package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.Setmeal_dishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class DishServiceImpl implements DishService {
@Autowired
private DishMapper dishMapper;
@Autowired
private DishFlavorMapper dishFlavorMapper;
@Autowired
private Setmeal_dishMapper setmeal_dishMapper;
@Override
public void addDish(DishDTO dishDTO) {
Dish dish=new Dish();
BeanUtils.copyProperties(dishDTO,dish);
dishMapper.insert(dish);
Long dishId=dish.getId();
List<DishFlavor> flavorList=dishDTO.getFlavors();
for(DishFlavor flavor:flavorList){
flavor.setDishId(dishId);
}
dishFlavorMapper.insertBatch(flavorList);
}
@Override
public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
List<DishVO> dishVOList=dishMapper.list(dishPageQueryDTO);
Page<DishVO> p= (Page<DishVO>) dishVOList;
PageResult pageResult=new PageResult(p.getTotal(),p.getResult());
return pageResult;
}
@Override
@Transactional
public void deleteBatch(Long[] ids) {
for(Long id:ids){
Dish dish=dishMapper.queryById(id);
if(dish.getStatus()== StatusConstant.ENABLE)
throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
else{
SetmealDish setmealDish=setmeal_dishMapper.queryByDishId(id);
if(setmealDish!=null)
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
else {
dishMapper.deleteById(id);
dishFlavorMapper.deleteByDishId(id);
}
}
}
}
@Override
public DishVO queryByIdWithFlavor(Long id) {
Dish dish=dishMapper.queryById(id);
DishVO dishVO=new DishVO();
BeanUtils.copyProperties(dish,dishVO);
List<DishFlavor> dishFlavors=dishFlavorMapper.queryByDishId(id);
dishVO.setFlavors(dishFlavors);
return dishVO;
}
@Override
public void update(DishDTO dishDTO) {
Dish dish=new Dish();
BeanUtils.copyProperties(dishDTO,dish);
dishMapper.update(dish);
dishFlavorMapper.deleteByDishId(dishDTO.getId()); //先删再添加口味不要在原来的上面修改原来可能2种口味现在3种也无法修改
List<DishFlavor> flavors = dishDTO.getFlavors();
if (flavors != null && flavors.size() > 0) {
flavors.forEach(dishFlavor -> {
dishFlavor.setDishId(dishDTO.getId());
});
//向口味表插入n条数据
dishFlavorMapper.insertBatch(flavors);
}
}
}

View File

@ -72,10 +72,10 @@ public class EmployeeServiceImpl implements EmployeeService {
Employee employee = new Employee();
//对象属性拷贝
BeanUtils.copyProperties(employeeDTO, employee);
employee.setCreateTime(LocalDateTime.now());
employee.setUpdateTime(LocalDateTime.now());
employee.setUpdateUser(BaseContext.getCurrentId());
employee.setCreateUser(BaseContext.getCurrentId());
// employee.setCreateTime(LocalDateTime.now());
// employee.setUpdateTime(LocalDateTime.now());
// employee.setUpdateUser(BaseContext.getCurrentId());
// employee.setCreateUser(BaseContext.getCurrentId());
String encodedPassword=bCryptPasswordEncoder.encode(PasswordConstant.DEFAULT_PASSWORD);
employee.setPassword(encodedPassword);
employee.setStatus(StatusConstant.ENABLE);
@ -108,8 +108,8 @@ public class EmployeeServiceImpl implements EmployeeService {
Employee employee = new Employee();
BeanUtils.copyProperties(employeeDTO, employee);
employee.setUpdateTime(LocalDateTime.now());
employee.setUpdateUser(BaseContext.getCurrentId());
// employee.setUpdateTime(LocalDateTime.now());
// employee.setUpdateUser(BaseContext.getCurrentId());
employeeMapper.update(employee);
}

View File

@ -6,3 +6,9 @@ sky:
database: sky_take_out
username: root
password: 123456
alioss:
access-key-id: LTAI5tR43XPATPy2NwTSiTqY
access-key-secret: AJPJSYc5sdwiZoj8RWzsXtjKR3W8f0
endpoint: https://oss-cn-hangzhou.aliyuncs.com
bucket-name: zyjavaweb

View File

@ -37,3 +37,12 @@ sky:
admin-ttl: 7200000
# 设置前端传递过来的令牌名称
admin-token-name: token
alioss:
endpoint: {sky.alioss.endpoint}
bucket-name: {sky.alioss.bucket-name}
access-key-id: {sky.alioss.access-key-id}
access-key-secret: {sky.alioss.access-key-secret}

View File

@ -0,0 +1,11 @@
<?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.sky.mapper.DishFlavorMapper">
<insert id="insertBatch">
insert into dish_flavor (dish_id, name, value) VALUES
<foreach collection="flavorList" item="df" separator=",">
(#{df.dishId},#{df.name},#{df.value})
</foreach>
</insert>
</mapper>

View File

@ -0,0 +1,38 @@
<?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.sky.mapper.DishMapper">
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into dish (name, category_id, price, image, description, create_time, update_time, create_user,update_user, status)
values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser}, #{status})
</insert>
<select id="list" resultType="com.sky.vo.DishVO">
select d.* , c.name as categoryName from dish d left outer join category c on d.category_id = c.id
<where>
<if test="name != null">
and d.name like concat('%',#{name},'%')
</if>
<if test="categoryId != null">
and d.category_id = #{categoryId}
</if>
<if test="status != null">
and d.status = #{status}
</if>
</where>
order by d.create_time desc
</select>
<update id="update">
update dish
<set>
<if test="name !='' and name !=null">name=#{name},</if>
<if test="categoryId !=null">category_id=#{categoryId},</if>
<if test="price !=null">price=#{price},</if>
<if test="image !=null">image=#{image},</if>
<if test="description !=null">description=#{description},</if>
<if test="status != null">status = #{status},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="updateUser != null">update_user = #{updateUser},</if>
</set>
where id=#{id}
</update>
</mapper>