parent
580b23885d
commit
2c7e1a97df
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
@ -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;
|
||||
|
||||
}
|
||||
@ -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;
|
||||
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue