中國加油,武漢加油!
篇幅較長,配合目錄觀看
案例準備
- 本案例基於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";
}
}