
#订单表
CREATE TABLE `tb_order` (`id` int NOT NULL AUTO_INCREMENT COMMENT 'id',`ordername` varchar(255) DEFAULT NULL COMMENT '订单名称',`price` int DEFAULT NULL COMMENT '价格',`num` int DEFAULT NULL COMMENT '数量',`userId` int DEFAULT NULL COMMENT '用户id',PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb3#用户表
CREATE TABLE `tb_user` (`id` int NOT NULL AUTO_INCREMENT COMMENT 'id',`username` varchar(255) DEFAULT NULL COMMENT '用户名',`address` varchar(255) DEFAULT NULL COMMENT '地址',PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb3
父模块:
org.springframework.boot spring-boot-starter-parent 2.3.9.RELEASE
UTF-8 UTF-8 1.8 Hoxton.SR10 8.0.25 2.1.3
org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import mysql mysql-connector-java ${mysql.version} org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis.version}
org.projectlombok lombok
order-service模块:
org.springframework.boot spring-boot-starter-web mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter com.keqing order-user 1.0-SNAPSHOT compile
org.springframework.boot spring-boot-maven-plugin
user-service模块:
org.springframework.boot spring-boot-starter-web mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter
org.springframework.boot spring-boot-maven-plugin
pojo:
@Data
public class Order {private Integer id;private String ordername;private Integer price;private Integer num;private Integer UserId;private User user;
}
mapper:
@Mapper
public interface OrderMapper {/*** 根据id查询订单*/@Select("select * from tb_order where id = #{id}")Order getById(@Param("id") Long id);
}
service:
public interface OrderService {/*** 根据id查找订单*/Order selectById(Long id);
}
service.impl:
@Service
public class OrderServiceImpl implements OrderService {@Autowiredprivate OrderMapper orderMapper;@Autowiredprivate RestTemplate restTemplate;/*** 根据id查找订单*/@Overridepublic Order selectById(Long id) {//1.查询订单Order order = orderMapper.getById(id);//2.利用RestTemplate发起http请求,查询用户//2.1 url路径String url = "http://userservice/user/" + order.getUserId();//2.2 发起http请求,将JSON反序列化为User对象User user = restTemplate.getForObject(url,User.class);order.setUser(user);return order;}
}
controller:
@RestController
@RequestMapping("/order")
public class OrderController {@Autowiredprivate OrderService orderService;@GetMapping("/{id}")public Order selectOrder(@PathVariable("id") Long id){return orderService.selectById(id);}
}
config:
@Component
public class RestTemplateConfig {/*** 创建 RestTemplate 并注入 Spring 容器* @LoadBalanced 注解描述:负载均衡作用*/@LoadBalanced@Beanpublic RestTemplate restTemplate(){return new RestTemplate();}
}
pojo:
@Data
public class User {private Integer id;private String username;private String address;
}
mapper:
@Mapper
public interface UserMapper {@Select("select * from tb_user where id = #{id}")User getById(@Param("id") Long id);
}
service:
public interface UserService {User selectById(Long id);
}
service.impl:
@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic User selectById(Long id) {return userMapper.getById(id);}
}
controller:
@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/{id}")public User select(@PathVariable("id") Long id){return userService.selectById(id);}
}
下一篇:装腔作势是什么意思