- demo 项目,增加 redis 库
This commit is contained in:
parent
580b23885d
commit
2c7e1a97df
|
@ -1,7 +1,7 @@
|
|||
package cn.iocoder.mall.demo.application.controller;
|
||||
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.demo.application.convert.DemOrderConvert;
|
||||
import cn.iocoder.mall.demo.application.convert.DemoOrderConvert;
|
||||
import cn.iocoder.mall.demo.application.dto.DemoOrderAddDTO;
|
||||
import cn.iocoder.mall.demo.business.api.DemoOrderService;
|
||||
import cn.iocoder.mall.demo.business.bo.order.DemoOrderAddBO;
|
||||
|
@ -19,7 +19,7 @@ public class DemoOrderController {
|
|||
|
||||
@PostMapping("/add")
|
||||
public CommonResult<Integer> add(DemoOrderAddDTO addDTO) {
|
||||
DemoOrderAddBO addBO = DemOrderConvert.INSTANCE.convert(addDTO);
|
||||
DemoOrderAddBO addBO = DemoOrderConvert.INSTANCE.convert(addDTO);
|
||||
addBO.setUserId(10); // TODO 10 用户编号。
|
||||
Integer orderId = demoOrderService.add(addBO);
|
||||
return CommonResult.success(orderId);
|
||||
|
|
|
@ -5,8 +5,6 @@ import cn.iocoder.mall.demo.application.convert.DemoProductConvert;
|
|||
import cn.iocoder.mall.demo.application.vo.DemoProductVO;
|
||||
import cn.iocoder.mall.demo.business.api.DemoProductService;
|
||||
import cn.iocoder.mall.demo.business.bo.product.DemoProductBO;
|
||||
import cn.iocoder.mall.demo.rpc.api.DemoProductRpcService;
|
||||
import org.apache.dubbo.config.annotation.Reference;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
@ -20,20 +18,10 @@ public class DemoProductController {
|
|||
@Autowired
|
||||
private DemoProductService productService;
|
||||
|
||||
@Reference(validation = "true", version = "${dubbo.consumer.DemoProductRpcService.version}")
|
||||
private DemoProductRpcService productRpcService;
|
||||
|
||||
@GetMapping("/get")
|
||||
public CommonResult<DemoProductVO> get(@RequestParam("id") Integer id) {
|
||||
DemoProductBO product = productService.get(id);
|
||||
return CommonResult.success(DemoProductConvert.INSTANCE.convert(product));
|
||||
}
|
||||
|
||||
// TODO 芋艿,这里只是做一个 demo 。实际一般不会这么玩,更多是内嵌的,像 {@link #get(Integer id)} 的情况。
|
||||
@GetMapping("/get2")
|
||||
public CommonResult<DemoProductVO> get2(@RequestParam("id") Integer id) {
|
||||
cn.iocoder.mall.demo.rpc.vo.DemoProductVO product = productRpcService.get(id);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package cn.iocoder.mall.demo.application.controller;
|
||||
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.demo.application.convert.DemoUserConvert;
|
||||
import cn.iocoder.mall.demo.application.vo.DemoUserVO;
|
||||
import cn.iocoder.mall.demo.rpc.api.DemoUserRpcService;
|
||||
import org.apache.dubbo.config.annotation.Reference;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class DemoUserController {
|
||||
|
||||
@Reference(validation = "true", version = "${dubbo.consumer.DemoUserRpcService.version}")
|
||||
private DemoUserRpcService userRpcService;
|
||||
|
||||
// TODO 芋艿,这里只是做一个 demo 。实际一般不会这么玩,更多是内嵌的,像 {@link #get(Integer id)} 的情况。
|
||||
@GetMapping("/get")
|
||||
public CommonResult<DemoUserVO> get(@RequestParam("id") Integer id) {
|
||||
cn.iocoder.mall.demo.rpc.vo.DemoUserVO user = userRpcService.get(id);
|
||||
return CommonResult.success(DemoUserConvert.INSTANCE.convert(user));
|
||||
}
|
||||
|
||||
}
|
|
@ -7,9 +7,9 @@ import org.mapstruct.Mappings;
|
|||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface DemOrderConvert {
|
||||
public interface DemoOrderConvert {
|
||||
|
||||
DemOrderConvert INSTANCE = Mappers.getMapper(DemOrderConvert.class);
|
||||
DemoOrderConvert INSTANCE = Mappers.getMapper(DemoOrderConvert.class);
|
||||
|
||||
@Mappings({})
|
||||
DemoOrderAddBO convert(DemoOrderAddDTO addDTO);
|
|
@ -0,0 +1,16 @@
|
|||
package cn.iocoder.mall.demo.application.convert;
|
||||
|
||||
import cn.iocoder.mall.demo.application.vo.DemoUserVO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface DemoUserConvert {
|
||||
|
||||
DemoUserConvert INSTANCE = Mappers.getMapper(DemoUserConvert.class);
|
||||
|
||||
@Mappings({})
|
||||
DemoUserVO convert(cn.iocoder.mall.demo.rpc.vo.DemoUserVO vo);
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package cn.iocoder.mall.demo.application.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class DemoUserVO {
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 性别
|
||||
*/
|
||||
private Integer gender;
|
||||
|
||||
}
|
|
@ -11,3 +11,5 @@ dubbo:
|
|||
consumer:
|
||||
DemoProductRpcService:
|
||||
version: 1.0.0
|
||||
DemoUserRpcService:
|
||||
version: 1.0.0
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package cn.iocoder.mall.demo.business.api;
|
||||
|
||||
import cn.iocoder.mall.demo.business.bo.user.DemoUserBO;
|
||||
|
||||
public interface DemoUserService {
|
||||
|
||||
DemoUserBO get(Integer id);
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package cn.iocoder.mall.demo.business.bo.user;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* Demo 用户 BO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class DemoUserBO {
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 性别
|
||||
*/
|
||||
private Integer gender;
|
||||
|
||||
}
|
|
@ -43,6 +43,15 @@
|
|||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 工具类相关 -->
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
package cn.iocoder.mall.demo.business.cacheobject;
|
|
@ -0,0 +1,26 @@
|
|||
package cn.iocoder.mall.demo.business.cacheobject.user;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* 用户缓存对象
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class DemoUserCacheObject {
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 性别
|
||||
*/
|
||||
private Integer gender;
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package cn.iocoder.mall.demo.business.convert;
|
||||
|
||||
import cn.iocoder.mall.demo.business.bo.user.DemoUserBO;
|
||||
import cn.iocoder.mall.demo.business.cacheobject.user.DemoUserCacheObject;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface DemoUserConvert {
|
||||
|
||||
DemoUserConvert INSTANCE = Mappers.getMapper(DemoUserConvert.class);
|
||||
|
||||
@Mappings({})
|
||||
DemoUserBO convert(DemoUserCacheObject object);
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package cn.iocoder.mall.demo.business.dao;
|
||||
package cn.iocoder.mall.demo.business.dao.mysql;
|
||||
|
||||
import cn.iocoder.mall.demo.business.dataobject.order.DemoOrderDO;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
@ -1,4 +1,4 @@
|
|||
package cn.iocoder.mall.demo.business.dao;
|
||||
package cn.iocoder.mall.demo.business.dao.mysql;
|
||||
|
||||
import cn.iocoder.mall.demo.business.dataobject.product.DemoProductDO;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
@ -0,0 +1,31 @@
|
|||
package cn.iocoder.mall.demo.business.dao.redis;
|
||||
|
||||
import cn.iocoder.mall.demo.business.cacheobject.user.DemoUserCacheObject;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Repository
|
||||
public class DemoUserCacheDao {
|
||||
|
||||
private static final String KEY_PREFIX = "user_";
|
||||
|
||||
@Resource(name = "redisTemplate")
|
||||
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
|
||||
private ValueOperations<String, String> operations;
|
||||
|
||||
private static String buildKey(Integer id) {
|
||||
return KEY_PREFIX + id;
|
||||
}
|
||||
|
||||
public DemoUserCacheObject get(Integer id) {
|
||||
return JSON.parseObject(operations.get(buildKey(id)), DemoUserCacheObject.class);
|
||||
}
|
||||
|
||||
public void set(Integer id, DemoUserCacheObject value) {
|
||||
operations.set(buildKey(id), JSON.toJSONString(value));
|
||||
}
|
||||
|
||||
}
|
|
@ -10,7 +10,7 @@ import cn.iocoder.mall.demo.business.bo.product.DemoProductBO;
|
|||
import cn.iocoder.mall.demo.business.bo.product.DemoProductQuantityReduceBO;
|
||||
import cn.iocoder.mall.demo.business.constant.OrderStatusEnum;
|
||||
import cn.iocoder.mall.demo.business.convert.DemoOrderConvert;
|
||||
import cn.iocoder.mall.demo.business.dao.DemoOrderMapper;
|
||||
import cn.iocoder.mall.demo.business.dao.mysql.DemoOrderMapper;
|
||||
import cn.iocoder.mall.demo.business.dataobject.order.DemoOrderDO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
|
|
@ -5,7 +5,7 @@ import cn.iocoder.common.framework.vo.PageResult;
|
|||
import cn.iocoder.mall.demo.business.api.DemoProductService;
|
||||
import cn.iocoder.mall.demo.business.bo.product.*;
|
||||
import cn.iocoder.mall.demo.business.convert.DemoProductConvert;
|
||||
import cn.iocoder.mall.demo.business.dao.DemoProductMapper;
|
||||
import cn.iocoder.mall.demo.business.dao.mysql.DemoProductMapper;
|
||||
import cn.iocoder.mall.demo.business.dataobject.product.DemoProductDO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package cn.iocoder.mall.demo.business.service;
|
||||
|
||||
import cn.iocoder.mall.demo.business.api.DemoUserService;
|
||||
import cn.iocoder.mall.demo.business.bo.user.DemoUserBO;
|
||||
import cn.iocoder.mall.demo.business.cacheobject.user.DemoUserCacheObject;
|
||||
import cn.iocoder.mall.demo.business.convert.DemoUserConvert;
|
||||
import cn.iocoder.mall.demo.business.dao.redis.DemoUserCacheDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class DemoUserServiceImpl implements DemoUserService {
|
||||
|
||||
@Autowired
|
||||
private DemoUserCacheDao userCacheDao;
|
||||
|
||||
@Override
|
||||
public DemoUserBO get(Integer id) {
|
||||
DemoUserCacheObject userCacheObject = userCacheDao.get(id);
|
||||
if (userCacheObject == null) { // TODO 芋艿,临时
|
||||
userCacheDao.set(id, new DemoUserCacheObject().setId(id)
|
||||
.setName("芋艿").setGender(1));
|
||||
}
|
||||
return DemoUserConvert.INSTANCE.convert(userCacheObject);
|
||||
}
|
||||
|
||||
}
|
|
@ -5,6 +5,8 @@ spring:
|
|||
driver-class-name: com.mysql.jdbc.Driver
|
||||
username: testb5f4
|
||||
password: F4df4db0ed86@11
|
||||
# redis
|
||||
redis:
|
||||
|
||||
# mybatis-plus
|
||||
mybatis-plus:
|
||||
|
@ -17,3 +19,4 @@ mybatis-plus:
|
|||
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
|
||||
mapperLocations: classpath*:mapper/*.xml
|
||||
typeAliasesPackage: cn.iocoder.mall.demo.business.dataobject
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?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="cn.iocoder.mall.demo.business.dao.DemoProductMapper">
|
||||
<mapper namespace="cn.iocoder.mall.demo.business.dao.mysql.DemoProductMapper">
|
||||
|
||||
<update id="updateQuantityReduce">
|
||||
UPDATE product
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package cn.iocoder.mall.demo.rpc.api;
|
||||
|
||||
import cn.iocoder.mall.demo.rpc.vo.DemoUserVO;
|
||||
|
||||
public interface DemoUserRpcService {
|
||||
|
||||
DemoUserVO get(Integer id);
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package cn.iocoder.mall.demo.rpc.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class DemoUserVO {
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 性别
|
||||
*/
|
||||
private Integer gender;
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package cn.iocoder.mall.demo.rpc.convert;
|
||||
|
||||
import cn.iocoder.mall.demo.business.bo.user.DemoUserBO;
|
||||
import cn.iocoder.mall.demo.rpc.vo.DemoUserVO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface DemoUserConvert {
|
||||
|
||||
DemoUserConvert INSTANCE = Mappers.getMapper(DemoUserConvert.class);
|
||||
|
||||
@Mappings({})
|
||||
DemoUserVO convert(DemoUserBO object);
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package cn.iocoder.mall.demo.rpc.service;
|
||||
|
||||
import cn.iocoder.mall.demo.business.api.DemoUserService;
|
||||
import cn.iocoder.mall.demo.business.bo.user.DemoUserBO;
|
||||
import cn.iocoder.mall.demo.rpc.api.DemoUserRpcService;
|
||||
import cn.iocoder.mall.demo.rpc.convert.DemoUserConvert;
|
||||
import cn.iocoder.mall.demo.rpc.vo.DemoUserVO;
|
||||
import org.apache.dubbo.config.annotation.Service;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
@Service(validation = "true", version = "${dubbo.provider.DemoUserRpcService.version}")
|
||||
public class DemoUserRpcServiceImpl implements DemoUserRpcService {
|
||||
|
||||
@Autowired
|
||||
private DemoUserService demoUserService;
|
||||
|
||||
@Override
|
||||
public DemoUserVO get(Integer id) {
|
||||
DemoUserBO userBO = demoUserService.get(id);
|
||||
return DemoUserConvert.INSTANCE.convert(userBO);
|
||||
}
|
||||
|
||||
}
|
|
@ -16,3 +16,5 @@ dubbo:
|
|||
# filter: -exception
|
||||
DemoProductRpcService:
|
||||
version: 1.0.0
|
||||
DemoUserRpcService:
|
||||
version: 1.0.0
|
||||
|
|
Loading…
Reference in New Issue