设计一个基于springboot-dubbo的电商网站的收货地址模块的详细代码,包括数据表、service、controller\mapper\mapping\
时间: 2024-05-19 13:14:17 浏览: 89
这里提供一个简单的电商网站收货地址模块的代码示例。
数据表设计:
CREATE TABLE `address` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`user_id` bigint(20) NOT NULL COMMENT '用户ID',
`name` varchar(50) NOT NULL DEFAULT '' COMMENT '收货人姓名',
`phone` varchar(20) NOT NULL DEFAULT '' COMMENT '收货人手机号',
`province` varchar(20) NOT NULL DEFAULT '' COMMENT '省份',
`city` varchar(20) NOT NULL DEFAULT '' COMMENT '城市',
`district` varchar(20) NOT NULL DEFAULT '' COMMENT '县/区',
`address` varchar(100) NOT NULL DEFAULT '' COMMENT '详细地址',
`is_default` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否为默认地址,0-否,1-是',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='收货地址表';
Service层代码:
@Service
public class AddressServiceImpl implements AddressService {
@Autowired
private AddressMapper addressMapper;
@Override
public List<AddressVO> listByUserId(Long userId) {
List<Address> addressList = addressMapper.listByUserId(userId);
return BeanUtil.copyList(addressList, AddressVO.class);
}
@Override
public void save(AddressAddDTO addressAddDTO) {
Address address = BeanUtil.copyProperties(addressAddDTO, Address.class);
address.setCreateTime(new Date());
address.setUpdateTime(address.getCreateTime());
addressMapper.insertSelective(address);
if (addressAddDTO.getIsDefault() == 1) {
addressMapper.updateDefaultByUserId(address.getUserId(), address.getId());
}
}
@Override
public void update(AddressUpdateDTO addressUpdateDTO) {
Address address = BeanUtil.copyProperties(addressUpdateDTO, Address.class);
address.setUpdateTime(new Date());
addressMapper.updateByPrimaryKeySelective(address);
if (addressUpdateDTO.getIsDefault() == 1) {
addressMapper.updateDefaultByUserId(address.getUserId(), address.getId());
}
}
@Override
public void delete(Long id) {
addressMapper.deleteByPrimaryKey(id);
}
@Override
public void setDefault(Long id, Long userId) {
addressMapper.cancelDefaultByUserId(userId);
addressMapper.updateDefaultById(id);
}
}
Controller层代码:
@RestController
@RequestMapping("/api/address")
public class AddressController {
@Autowired
private AddressService addressService;
@GetMapping("/list")
public ResultVO<List<AddressVO>> list(@RequestParam("userId") Long userId) {
return ResultVOUtil.success(addressService.listByUserId(userId));
}
@PostMapping("/save")
public ResultVO save(@RequestBody AddressAddDTO addressAddDTO) {
addressService.save(addressAddDTO);
return ResultVOUtil.success();
}
@PostMapping("/update")
public ResultVO update(@RequestBody AddressUpdateDTO addressUpdateDTO) {
addressService.update(addressUpdateDTO);
return ResultVOUtil.success();
}
@PostMapping("/delete")
public ResultVO delete(@RequestParam("id") Long id) {
addressService.delete(id);
return ResultVOUtil.success();
}
@PostMapping("/setDefault")
public ResultVO setDefault(@RequestParam("id") Long id, @RequestParam("userId") Long userId) {
addressService.setDefault(id, userId);
return ResultVOUtil.success();
}
}
Mapper层代码:
@Mapper
public interface AddressMapper {
List<Address> listByUserId(Long userId);
int insertSelective(Address address);
int updateByPrimaryKeySelective(Address address);
int deleteByPrimaryKey(Long id);
int updateDefaultById(Long id);
int updateDefaultByUserId(@Param("userId") Long userId, @Param("id") Long id);
int cancelDefaultByUserId(Long userId);
}
Mapping层代码:
@Mapper(componentModel = "spring")
public interface AddressMapping {
AddressVO toVO(Address address);
List<AddressVO> toVOList(List<Address> addressList);
Address fromAddDTO(AddressAddDTO addressAddDTO);
Address fromUpdateDTO(AddressUpdateDTO addressUpdateDTO);
}
以上代码示例仅供参考,具体实现可根据实际需求进行修改。
阅读全文