企业标准 DTO 传参 + Controller + Service + 拷贝工具类完整版
1先写两个 DTO新增、修改ShopSaveDTO.java新增用import lombok.Data; import java.math.BigDecimal; Data public class ShopSaveDTO { private String name; private String address; private Long typeId; private String phone; private BigDecimal avgPrice; }ShopUpdateDTO.java修改用import lombok.Data; import java.math.BigDecimal; Data public class ShopUpdateDTO { private Long id; // 修改必须传id private String name; private String address; private Long typeId; private String phone; private BigDecimal avgPrice; }2Controller完整接收 DTOimport org.springframework.web.bind.annotation.*; import javax.annotation.Resource; RestController RequestMapping(/shop) public class ShopController { Resource private ShopService shopService; // 查询 GetMapping(/{id}) public Result queryById(PathVariable Long id) { return shopService.queryById(id); } // 新增接收 ShopSaveDTO PostMapping public Result saveShop(RequestBody ShopSaveDTO dto) { return shopService.saveShop(dto); } // 修改接收 ShopUpdateDTO PutMapping public Result updateShop(RequestBody ShopUpdateDTO dto) { return shopService.updateShop(dto); } // 删除 DeleteMapping(/{id}) public Result deleteShop(PathVariable Long id) { return shopService.deleteShop(id); } }3Service 接口import com.baomidou.mybatisplus.extension.service.IService; public interface ShopService extends IServiceShop { Result queryById(Long id); Result saveShop(ShopSaveDTO dto); Result updateShop(ShopUpdateDTO dto); Result deleteShop(Long id); }4ServiceImpl完整 BeanUtils.copyPropertiesimport org.springframework.beans.BeanUtils; import org.springframework.transaction.annotation.Transactional; import org.springframework.data.redis.core.StringRedisTemplate; Service public class ShopServiceImpl extends ServiceImplShopMapper, Shop implements ShopService { Resource private StringRedisTemplate stringRedisTemplate; Resource private CacheClient cacheClient; private static final String CACHE_SHOP_KEY cache:shop:; // 查询你原来的逻辑 Override public Result queryById(Long id) { Shop shop cacheClient.queryWithPassThrough( CACHE_SHOP_KEY, id, Shop.class, this::getById, 30L, java.util.concurrent.TimeUnit.MINUTES ); if (shop null) { return Result.fail(店铺不存在); } return Result.ok(shop); } // 新增 Override Transactional public Result saveShop(ShopSaveDTO dto) { Shop shop new Shop(); BeanUtils.copyProperties(dto, shop); // DTO → 实体 save(shop); return Result.ok(); } // 修改 Override Transactional public Result updateShop(ShopUpdateDTO dto) { Shop shop new Shop(); BeanUtils.copyProperties(dto, shop); // DTO → 实体 updateById(shop); // 删除缓存 stringRedisTemplate.delete(CACHE_SHOP_KEY dto.getId()); return Result.ok(); } // 删除 Override Transactional public Result deleteShop(Long id) { removeById(id); stringRedisTemplate.delete(CACHE_SHOP_KEY id); return Result.ok(); } }