java电商项目搭建-------地址模块

努力好了,时间会给你答案。--------magic_guo

对于地址模块的搭建就比较简单了,以下我梳理了地址模块的一些信息;直接看图:
地址模块展示栏:
在这里插入图片描述
从上图中可以看地址模块的一些操作:
功能分析:
在这里插入图片描述
值得注意的是,添加地址是有两种操作:
1、添加的地址是否设为默认地址;
这就意味我们要做一个判断,这里牵涉了两个sql语句:①.插入这条地址信息;②.将该用户的其他默认地址设置为非默认地址;
2、不判断地址是否设置为默认地址,添加进来的都为非默认地址
直接添加地址,不用考虑用户传进来的是否是默认的,都设为非默认地址,然后让用户自己管理操作;只需设置一个将地址改为默认的接口即可;

我参考了淘宝app的地址管理模块,没有说添加的时候就将此地址设置为默认地址,而是由用户来管理自己的地址;就是我们上述的第二种操作;

看代码:
pom依赖:

<dependencies>
        <dependency>
            <groupId>com.guo</groupId>
            <artifactId>shop-common</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

controller控制接口层:

@RequestMapping("/address")
@RestController
public class AddressController {

    @Autowired
    private IAddressService addressService;

    @RequestMapping("/addAddress")
    @LoginUser
    public ResultEntity addAddress(User user, Address address) {

        if (user.getId() == null) {
            return ResultEntity.error("还未登录,请登录");
        }
        address.setUid(user.getId());

        boolean insert = addressService.insert(address);

        return ResultEntity.success(insert);
    }

    @RequestMapping("/updateAddress")
    @LoginUser
    public ResultEntity updateAddress(User user, Integer addressId, Address address) {

        address.setUid(user.getId());
        EntityWrapper<Address> addressEntityWrapper = new EntityWrapper<>();
        addressEntityWrapper.eq("id", addressId);
        addressEntityWrapper.eq("uid", user.getId());

        boolean update = addressService.update(address, addressEntityWrapper);

        return ResultEntity.success(update);
    }

    @RequestMapping("/setDefAddress")
    @LoginUser
    public ResultEntity setDefAddress(User user, Integer addressId) {

        addressService.setDefAddress(user.getId(), addressId);

        return ResultEntity.success();
    }

    @RequestMapping("/delAddress")
    @LoginUser
    public ResultEntity delAddress(User user, Integer addressId) {

        boolean b = addressService.deleteById(addressId);

        return ResultEntity.success(b);
    }

    @RequestMapping("/getAllAddress")
    @LoginUser
    public ResultEntity getAllAddress(User user) {

        EntityWrapper<Address> addressEntityWrapper = new EntityWrapper<>();
        addressEntityWrapper.eq("uid", user.getId());
        List<Address> addresses = addressService.selectList(addressEntityWrapper);

        return ResultEntity.success(addresses);
    }
}

sevice接口层:

public interface IAddressService extends IService<Address> {

    void setDefAddress(Integer id, Integer addressId);
}

servicesImpl实现层:

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Override
    @Transactional
    public void setDefAddress(Integer uid, Integer addressId) {
        // 将所有地址设为非默认地址
        baseMapper.setAllAddressToDef(uid);
        // 将此id的地址位置为默认地址
        baseMapper.setDefAddress(uid, addressId);
    }
}

mapper层:

public interface AddressMapper extends BaseMapper<Address> {

    void setAllAddressToDef(Integer uid);

    void setDefAddress(@Param("uid") Integer uid, @Param("addressId") Integer addressId);
}

mapper.xml:

<?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.guo.mapper.AddressMapper">

    <update id="setAllAddressToDef">
        update
            t_address
        set
            def = 0
        where
            uid = #{uid}
    </update>

    <update id="setDefAddress">
        update
            t_address
        set
            def = 1
        where
            uid = #{uid} and id = #{addressId}
    </update>
    
</mapper>

配置文件:

spring:
  cloud:
    config:
      uri: http://localhost:9999
      profile: shop-address, eureka-client, log, datasource
      name: application
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.guo

config配置文件:

spring:
  application:
    name: shop-address
server:
  port: 8012

启动类:

@SpringBootApplication(scanBasePackages = "com.guo")
@EnableEurekaClient
@MapperScan("com.guo.mapper")
public class ShopAddressApplication {

    public static void main(String[] args) {
        SpringApplication.run(ShopAddressApplication.class, args);
    }

}

完工!


本文章教学视频来自:https://www.bilibili.com/video/BV1tb4y1Q74E?p=3&t=125并做一些自己的更改!


静下心,慢慢来,会很快!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值