Compare commits
3 Commits
39640025af
...
2ffc04318f
Author | SHA1 | Date |
---|---|---|
|
2ffc04318f | |
|
d48f9bfe0a | |
|
435736e110 |
|
@ -3,10 +3,12 @@ package com.sky;
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@EnableCaching// 开启缓存注解
|
||||
@SpringBootApplication
|
||||
@EnableTransactionManagement //开启注解方式的事务管理
|
||||
@EnableTransactionManagement // 开启注解方式的事务管理
|
||||
@Slf4j
|
||||
public class SkyApplication {
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -1,12 +1,29 @@
|
|||
package com.sky.config;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
||||
import org.springframework.data.redis.cache.RedisCacheManager;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@Configuration
|
||||
@Slf4j
|
||||
|
@ -25,4 +42,51 @@ public class RedisConfiguration {
|
|||
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解决cache(@Cacheable)把数据缓存到redis中的value是乱码问题
|
||||
*/
|
||||
@Bean
|
||||
@SuppressWarnings("all")
|
||||
public CacheManager cacheManager(RedisConnectionFactory factory) {
|
||||
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
|
||||
// json序列化
|
||||
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = getJackson2JsonRedisSerializer();
|
||||
// 配置序列化
|
||||
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
|
||||
RedisCacheConfiguration redisCacheConfiguration = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
|
||||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));
|
||||
|
||||
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
|
||||
.cacheDefaults(redisCacheConfiguration)
|
||||
.build();
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
private Jackson2JsonRedisSerializer getJackson2JsonRedisSerializer() {
|
||||
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
// 设置ObjectMapper访问权限
|
||||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
// 记录序列化之后的数据类型,方便反序列化
|
||||
om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
|
||||
ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
|
||||
// LocalDatetime序列化,默认不兼容jdk8日期序列化
|
||||
JavaTimeModule timeModule = new JavaTimeModule();
|
||||
timeModule.addDeserializer(LocalDate.class,
|
||||
new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
||||
timeModule.addDeserializer(LocalDateTime.class,
|
||||
new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
||||
timeModule.addSerializer(LocalDate.class,
|
||||
new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
||||
timeModule.addSerializer(LocalDateTime.class,
|
||||
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
||||
// 关闭默认的日期格式化方式,默认UTC日期格式 yyyy-MM-dd’T’HH:mm:ss.SSS
|
||||
om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
om.registerModule(timeModule);
|
||||
|
||||
jackson2JsonRedisSerializer.setObjectMapper(om);
|
||||
return jackson2JsonRedisSerializer;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import com.sky.vo.SetmealVO;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
@ -30,6 +31,7 @@ public class SetmealController {
|
|||
*/
|
||||
@ApiOperation("新增套餐")
|
||||
@PostMapping()
|
||||
@CacheEvict(cacheNames = "setmealCache", key = "@setmealDTO.categoryId")
|
||||
public Result<String> save(@RequestBody SetmealDTO setmealDTO) {
|
||||
setmealService.saveWithDish(setmealDTO);
|
||||
return Result.success();
|
||||
|
@ -70,6 +72,7 @@ public class SetmealController {
|
|||
*/
|
||||
@ApiOperation("批量删除套餐")
|
||||
@DeleteMapping("")
|
||||
@CacheEvict(cacheNames = "setmealCache", allEntries = true)
|
||||
public Result delete(@RequestParam List<Long> ids) {
|
||||
setmealService.delete(ids);
|
||||
return Result.success();
|
||||
|
@ -96,6 +99,7 @@ public class SetmealController {
|
|||
*/
|
||||
@ApiOperation("修改套餐")
|
||||
@PutMapping()
|
||||
@CacheEvict(cacheNames = "setmealCache", allEntries = true)
|
||||
public Result update(@RequestBody SetmealDTO setmealDTO) {
|
||||
setmealService.update(setmealDTO);
|
||||
return Result.success();
|
||||
|
|
|
@ -7,7 +7,7 @@ import com.sky.service.SetmealService;
|
|||
import com.sky.vo.DishItemVO;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
@ -27,10 +27,11 @@ public class SetmealController {
|
|||
* 条件查询
|
||||
*
|
||||
* @param categoryId Long
|
||||
* @return Result<List<Setmeal>>
|
||||
* @return Result<List < Setmeal>>
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
@ApiOperation("根据分类id查询套餐")
|
||||
@Cacheable(cacheNames = "setmealCache", key = "#categoryId")// key值
|
||||
public Result<List<Setmeal>> list(Long categoryId) {
|
||||
Setmeal setmeal = new Setmeal();
|
||||
setmeal.setCategoryId(categoryId);
|
||||
|
@ -43,8 +44,8 @@ public class SetmealController {
|
|||
/**
|
||||
* 根据套餐id查询包含的菜品列表
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
* @param id Long
|
||||
* @return Result<List < DishItemVO>>
|
||||
*/
|
||||
@GetMapping("/dish/{id}")
|
||||
@ApiOperation("根据套餐id查询包含的菜品列表")
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package com.sky.controller.user;
|
||||
|
||||
import com.sky.dto.ShoppingCartDTO;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.ShoppingCartService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user/shopping")
|
||||
@Slf4j
|
||||
@Api(tags = "C端购物车相关接口")
|
||||
public class ShoppingCartController {
|
||||
@Resource
|
||||
private ShoppingCartService shoppingCartService;
|
||||
|
||||
|
||||
/**
|
||||
* 添加购物车
|
||||
*
|
||||
* @param shoppingCartDTO ShoppingCartDTO
|
||||
* @return Result
|
||||
*/
|
||||
@ApiOperation("添加购物车")
|
||||
@PostMapping("add")
|
||||
public Result<String> add(ShoppingCartDTO shoppingCartDTO) {
|
||||
log.info("添加购物车:{}", shoppingCartDTO);
|
||||
shoppingCartService.addShoppingCart(shoppingCartDTO);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.sky.mapper;
|
||||
|
||||
import com.sky.entity.ShoppingCart;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface ShoppingCartMapper {
|
||||
/**
|
||||
* 添加购物车
|
||||
*
|
||||
* @param shoppingCart ShoppingCart
|
||||
* @return List<ShoppingCart>
|
||||
*/
|
||||
List<ShoppingCart> list(ShoppingCart shoppingCart);
|
||||
|
||||
/**
|
||||
* 如果已经存在,只需要将数量+1即可-修改商品数量
|
||||
*
|
||||
* @param shoppingCart ShoppingCart
|
||||
*/
|
||||
void updateNumberById(ShoppingCart shoppingCart);
|
||||
|
||||
/**
|
||||
* 插入购物车数据
|
||||
*
|
||||
* @param shoppingCart ShoppingCart
|
||||
*/
|
||||
void insert(ShoppingCart shoppingCart);
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.ShoppingCartDTO;
|
||||
|
||||
public interface ShoppingCartService {
|
||||
/**
|
||||
* 添加购物车
|
||||
*
|
||||
* @param shoppingCartDTO 购物车参数
|
||||
*/
|
||||
void addShoppingCart(ShoppingCartDTO shoppingCartDTO);
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package com.sky.service.impl;
|
||||
|
||||
import com.sky.context.BaseContext;
|
||||
import com.sky.dto.ShoppingCartDTO;
|
||||
import com.sky.entity.Dish;
|
||||
import com.sky.entity.Setmeal;
|
||||
import com.sky.entity.ShoppingCart;
|
||||
import com.sky.mapper.DishMapper;
|
||||
import com.sky.mapper.SetmealMapper;
|
||||
import com.sky.mapper.ShoppingCartMapper;
|
||||
import com.sky.service.ShoppingCartService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ShoppingCartServiceImplImpl implements ShoppingCartService {
|
||||
@Resource
|
||||
private ShoppingCartMapper shoppingCartMapper;
|
||||
@Resource
|
||||
private DishMapper dishMapper;
|
||||
@Resource
|
||||
private SetmealMapper setmealMapper;
|
||||
|
||||
/**
|
||||
* 添加购物车
|
||||
*
|
||||
* @param shoppingCartDTO 购物车参数
|
||||
*/
|
||||
@Override
|
||||
public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
|
||||
ShoppingCart shoppingCart = new ShoppingCart();
|
||||
BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
|
||||
// 获取当前id
|
||||
Long userId = BaseContext.getCurrentId();
|
||||
shoppingCart.setUserId(userId);
|
||||
// 判断当前插入的购物车商品是否存在
|
||||
List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
|
||||
// 如果已经存在,只需要将数量+1即可
|
||||
if (list != null && !list.isEmpty()) {
|
||||
ShoppingCart cart = list.get(0);
|
||||
cart.setNumber(cart.getNumber() + 1);
|
||||
shoppingCartMapper.updateNumberById(cart);
|
||||
} else {
|
||||
// 如果不存在。需要新建插入购物车数量
|
||||
Long dishId = shoppingCartDTO.getDishId();
|
||||
// 判断本次添加的是菜品还是套餐
|
||||
if (dishId != null) {
|
||||
// 本次添加的到购物车是菜品
|
||||
Dish dish = dishMapper.getById(dishId);
|
||||
shoppingCart.setName(dish.getName());
|
||||
shoppingCart.setImage(dish.getImage());
|
||||
shoppingCart.setAmount(dish.getPrice());
|
||||
} else {
|
||||
// 本次添加的是套餐
|
||||
Long setmealId = shoppingCartDTO.getSetmealId();
|
||||
Setmeal setmeal = setmealMapper.getById(setmealId);
|
||||
shoppingCart.setName(setmeal.getName());
|
||||
shoppingCart.setImage(setmeal.getImage());
|
||||
shoppingCart.setAmount(setmeal.getPrice());
|
||||
}
|
||||
shoppingCart.setNumber(1);
|
||||
shoppingCart.setCreateTime(LocalDateTime.now());
|
||||
shoppingCartMapper.insert(shoppingCart);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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.ShoppingCartMapper">
|
||||
<!-- 插入购物车数据 -->
|
||||
<insert id="insert">
|
||||
insert into shopping_cart (name, image, user_id, dish_id, setmeal_id, dish_flavor, number, amount,
|
||||
create_time)
|
||||
values (#{name}, #{image}, #{userId}, #{dishId}, #{setmealId}, #{dishFlavor}, #{number}, #{amount},
|
||||
#{createTime});
|
||||
</insert>
|
||||
|
||||
<!-- 如果已经存在,只需要将数量+1即可-修改商品数量 -->
|
||||
<update id="updateNumberById">
|
||||
update shopping_cart
|
||||
set number = #{number}
|
||||
where id = #{id};
|
||||
</update>
|
||||
|
||||
<!-- 添加购物车 -->
|
||||
<select id="list" resultType="com.sky.entity.ShoppingCart">
|
||||
select *
|
||||
from shopping_cart
|
||||
<where>
|
||||
<if test="userId != null">
|
||||
and userId = #{userId}
|
||||
</if>
|
||||
<if test="setmealId != null">
|
||||
and setmeal_id = #{setmealId}
|
||||
</if>
|
||||
<if test="dishId != null">
|
||||
and dish_id = #{dishId}
|
||||
</if>
|
||||
<if test="dishFlavor != null">
|
||||
and dish_flavor = #{dishFlavor}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
</mapper>
|
Loading…
Reference in New Issue