中国加油,武汉加油!
篇幅较长,配合目录观看
案例准备
- 本案例基于springboot篇】二十一. 基于springboot电商项目 十 购物车模块和自定义注解校验登录状况
1. 地址服务环境搭建
1.1 shop-entity编写Address类
package com.wpj.entity;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("t_address")
public class Address implements Serializable{
@TableId(type = IdType.AUTO)
private Integer id;
private String person;
private String phone;
private String address;
@TableField(value = "is_def")
private Integer isDef;
@TableField(value = "user_id")
private Integer uid;
}
1.2 shop-mapper编写mapper接口
package com.wpj.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.wpj.entity.Address;
public interface IAddressMapper extends BaseMapper<Address> {
}
1.3 shop-service-api编写Service接口
package com.wpj.service;
import com.wpj.entity.Address;
public interface IAddressService extends IBaseService<Address> {
}
1.4 shop-service-iml新建address-service(module-springboot)
1.4.1 address-service导包
<dependency>
<groupId>com.wpj</groupId>
<artifactId>shop-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.wpj</groupId>
<artifactId>shop-mapper</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.wpj</groupId>
<artifactId>shop-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
1.4.2 编写ServiceImpl
package com.wpj.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.wpj.entity.Address;
import com.wpj.mapper.IAddressMapper;
import com.wpj.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class AddressServiceImpl extends BaseServiceImpl<Address> implements IAddressService {
@Autowired
private IAddressMapper addressMapper;
@Override
public BaseMapper<Address> getMapper() {
return addressMapper;
}
}
1.4.3 配置yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/nz1904-springboot-shop
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
dubbo:
application:
name: address-service
registry:
address: zookeeper:192.168.59.100:2181
protocol:
port: -1
mybatis-plus:
type-aliases-package: com.wpj.entity
mapper-locations: classpath:/mapper/*.xml
1.4.4 修改程序入口
package com.wpj;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@DubboComponentScan(basePackages = "com.wpj.service")
@MapperScan(basePackages = "com.wpj.mapper")
public class AddressServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AddressServiceApplication.class, args);
}
}
1.5 shop-web新建shop-order(module-springboot)
1.5.1 shop-order导包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.wpj</groupId>
<artifactId>shop-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.wpj</groupId>
<artifactId>shop-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
1.5.2 配置yml
server:
port: 8086
spring:
thymeleaf:
cache: false
prefix: classpath:/templates/
redis:
host: 192.168.59.100
password: admin
dubbo:
application:
name: shop-order
registry:
address: zookeeper://192.168.59.100:2181
consumer:
check: false
retries: 3
1.5.3 修改程序入口
package com.wpj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class ShopOrderApplication {
public static void main(String[] args) {
SpringApplication.run(ShopOrderApplication.class, args);
}
}
1.5.4 启动AddressService并测试
package com.wpj;
import com.alibaba.dubbo.config.annotation.Reference;
import com.wpj.entity.Address;
import com.wpj.service.IAddressService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
class ShopOrderApplicationTests {
@Reference
private IAddressService addressService;
@Test
public void contextLoads() {
List<Address> list = addressService.getList();
for (Address address : list) {
System.out.println(address);
}
}
}
2. 地址服务模块-确认订单页面
2.1 service-api定义方法
package com.wpj.service;
import com.wpj.entity.Address;
import java.util.List;
public interface IAddressService extends IBaseService<Address> {
List<Address> getAddressListByUid(Integer id);
}
2.2 address-service重写方法
@Override
public List<Address> getAddressListByUid(Integer id) {
EntityWrapper wrapper = new EntityWrapper();
wrapper.eq("user_id", id);
return addressMapper.selectList(wrapper);
}
2.3 shop-order编写Controller
package com.wpj.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.wpj.common.aop.IsLogin;
import com.wpj.entity.Address;
import com.wpj.entity.Cart;
import com.wpj.entity.User;
import com.wpj.service.IAddressService;
import com.wpj.service.ICartService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import java.math.BigDecimal;
import java.util.List;
@Controller
@RequestMapping(value = "/order")
public class OrderController {
@Reference
private IAddressService addressService;
@Reference
private ICartService cartService;
@RequestMapping(value = "/toAffirmOrder")
@IsLogin(mustUser = true)
public String toAffirmOrder(User user, ModelMap map){
List<Address> addressList = addressService.getAddressListByUid(user.getId());
List<Cart> cartList = cartService.getUserCartList(user, null);
BigDecimal totalPrice = new BigDecimal(0);
for(Cart cart:cartList){
totalPrice= totalPrice.add(cart.getSubTotal());
}
map.put("addressList",addressList);
map.put("cartList",cartList);
map.put("totalPrice",totalPrice);
return "affirmOrder";
}
}
2.4 导入静态资源和编写页面
2.5 开启包扫描
package com.wpj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(scanBasePackages = "com.wpj",exclude = DataSourceAutoConfiguration.class)
public class ShopOrderApplication {
public static void main(String[] args) {
SpringApplication.run(ShopOrderApplication.class, args);
}
}
2.6 启动程序入口测试
3. 地址服务模块-添加新地址
3.1 address-service重写insert方法
@Override
public int insert(Address address) {
return addressMapper.addAdderss(address);
}
3.2 shop-mapper的Mapper接口定义方法
package com.wpj.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.wpj.entity.Address;
public interface IAddressMapper extends BaseMapper<Address> {
int addAddress(Address address);
}
3.3 定义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.wpj.mapper.IAddressMapper">
<insert id="addAddress" statementType="CALLABLE">
{
call add_address(#{person},#{phone},#{address},#{isDef},#{uid})
}
</insert>
</mapper>
3.4 数据库定义存储过程
3.5 shop-order编写Controller
package com.wpj.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.wpj.common.aop.IsLogin;
import com.wpj.entity.Address;
import com.wpj.entity.User;
import com.wpj.service.IAddressService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/address")
public class AddressController {
@Reference
private IAddressService addressService;
@RequestMapping(value = "/addAddress")
@IsLogin(mustUser = true)
@ResponseBody
public String addAddress(Address address, User user){
address.setUid(user.getId());
addressService.insert(address);
return "ok";
}
}