完成学习事务
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package cn.mayiming;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
@@ -10,6 +11,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
@MapperScan("cn.mayiming.Mapper")
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
|
||||
@@ -2,11 +2,13 @@ package cn.mayiming.Controller;
|
||||
|
||||
|
||||
import cn.mayiming.Service.OrderService;
|
||||
import cn.mayiming.entity.Order;
|
||||
import cn.mayiming.entity.User;
|
||||
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class OrderController {
|
||||
@@ -15,7 +17,13 @@ public class OrderController {
|
||||
private OrderService orderService;
|
||||
|
||||
@PostMapping("/order")
|
||||
public User getOrder(@RequestBody User user){
|
||||
public User getUserOrder(@RequestBody User user){
|
||||
//throw new RuntimeException("Cuowu ");
|
||||
return orderService.SearchUserbyname(user);
|
||||
}
|
||||
|
||||
@GetMapping("/order")
|
||||
public List<Order> getUserOrder(@RequestParam Integer id){
|
||||
return orderService.OrderListById(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package cn.mayiming.Mapper;
|
||||
|
||||
|
||||
import cn.mayiming.entity.Order;
|
||||
import feign.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface OrderMapper {
|
||||
|
||||
/**
|
||||
* 根据用户ID查询所有订单
|
||||
* @param userId 用户ID(@Param 注解解决参数名绑定问题)
|
||||
* @return 该用户的所有订单列表(按创建时间倒序)
|
||||
*/
|
||||
@Select("SELECT id, " +
|
||||
"order_no AS orderNo, " +
|
||||
"user_id AS userId, " +
|
||||
"product_name AS productName, " +
|
||||
"product_price AS productPrice, " +
|
||||
"count, " +
|
||||
"total_amount AS totalAmount, " +
|
||||
"status, " +
|
||||
"create_time AS createTime, " +
|
||||
"update_time AS updateTime " +
|
||||
"FROM t_order " +
|
||||
"WHERE user_id = #{userId} " +
|
||||
"ORDER BY create_time DESC")
|
||||
List<Order> selectByUserId(@Param("userId") Integer userId);
|
||||
}
|
||||
@@ -1,17 +1,34 @@
|
||||
package cn.mayiming.Service;
|
||||
|
||||
import cn.mayiming.Mapper.OrderMapper;
|
||||
import cn.mayiming.entity.Order;
|
||||
import cn.mayiming.entity.User;
|
||||
import cn.mayiming.feign.UserFeignClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class OrderService {
|
||||
@Autowired
|
||||
private UserFeignClient userFeignClient;
|
||||
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
|
||||
public User SearchUserbyname (User user) {
|
||||
return userFeignClient.selectByUsername(user);
|
||||
}
|
||||
|
||||
public List<Order> OrderListById(Integer id) {
|
||||
User user = userFeignClient.GetUserByid(id);
|
||||
|
||||
if(user == null) {
|
||||
throw new RuntimeException("错误!");
|
||||
}
|
||||
return orderMapper.selectByUserId(id);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
54
order-service/src/main/java/cn/mayiming/entity/Order.java
Normal file
54
order-service/src/main/java/cn/mayiming/entity/Order.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package cn.mayiming.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
@Data // Lombok 注解,自动生成get/set/toString
|
||||
public class Order {
|
||||
/** 订单ID */
|
||||
private Long id;
|
||||
/** 订单编号 */
|
||||
private String orderNo;
|
||||
/** 关联用户ID */
|
||||
private Integer userId;
|
||||
/** 商品名称 */
|
||||
private String productName;
|
||||
/** 商品单价 */
|
||||
private BigDecimal productPrice;
|
||||
/** 购买数量 */
|
||||
private Integer count;
|
||||
/** 订单总金额 */
|
||||
private BigDecimal totalAmount;
|
||||
/** 订单状态:0-待支付 1-已支付 2-已取消 3-已完成 */
|
||||
private Integer status;
|
||||
/** 创建时间 */
|
||||
private Date createTime;
|
||||
/** 更新时间 */
|
||||
private Date updateTime;
|
||||
|
||||
// 可选:添加状态枚举,避免硬编码数字
|
||||
public enum OrderStatus {
|
||||
PENDING_PAYMENT(0, "待支付"),
|
||||
PAID(1, "已支付"),
|
||||
CANCELLED(2, "已取消"),
|
||||
COMPLETED(3, "已完成");
|
||||
|
||||
private final int code;
|
||||
private final String desc;
|
||||
|
||||
OrderStatus(int code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,16 @@ package cn.mayiming.feign;
|
||||
import cn.mayiming.entity.User;
|
||||
import feign.Param;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@FeignClient(name = "user-service")
|
||||
public interface UserFeignClient {
|
||||
@PostMapping("/user")
|
||||
User selectByUsername(User user);
|
||||
|
||||
@GetMapping("/user")
|
||||
User GetUserByid(@RequestParam("id") Integer id);
|
||||
|
||||
}
|
||||
|
||||
@@ -18,4 +18,24 @@ spring:
|
||||
namespace: public
|
||||
group: DEFAULT_GROUP
|
||||
config:
|
||||
import: nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension}?server-addr=${spring.cloud.nacos.config.server-addr}
|
||||
import: nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension}?server-addr=${spring.cloud.nacos.config.server-addr}
|
||||
|
||||
datasource:
|
||||
# 数据库驱动类(MySQL 8.x 用 com.mysql.cj.jdbc.Driver,5.x 用 com.mysql.jdbc.Driver)
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
# 数据库连接 URL(替换为你的数据库地址、端口、库名,如 user_db)
|
||||
url: jdbc:mysql://rm-f8z6oc5a03331500p8o.mysql.rds.aliyuncs.com:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
||||
# 数据库用户名(默认 root,根据实际情况修改)
|
||||
username: root
|
||||
# 数据库密码(替换为你的 MySQL 密码)
|
||||
password: Root123456
|
||||
# 可选:连接池配置(推荐使用 HikariCP,Spring Boot 2.x 默认)
|
||||
hikari:
|
||||
# 连接池最大连接数
|
||||
maximum-pool-size: 10
|
||||
# 连接池最小空闲连接数
|
||||
minimum-idle: 2
|
||||
# 连接超时时间(毫秒)
|
||||
connection-timeout: 30000
|
||||
# 连接最大存活时间(毫秒)
|
||||
max-lifetime: 1800000
|
||||
Reference in New Issue
Block a user