文章目录
- 一、服务拆分(order-service、user-service)
- 1.创建数据库
- 2.创建order-service和user-service模块,引入依赖
- 3、order-service各层代码
- 4、user-service各层代码
一、服务拆分(order-service、user-service)
1.创建数据库
#订单表
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
2.创建order-service和user-service模块,引入依赖
父模块:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR10</spring-cloud.version>
<mysql.version>8.0.25</mysql.version>
<mybatis.version>2.1.3</mybatis.version>
</properties>
<!-- dependencyManagement 只对依赖版本进行管理,不会实际引入 jar -->
<dependencyManagement>
<dependencies>
<!-- springCloud 此处使用 <scope>import</scope>意味着导入了springCloud父模块的jar包依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
order-service模块:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.keqing</groupId>
<artifactId>order-user</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
user-service模块:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3、order-service各层代码
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 {
@Autowired
private OrderMapper orderMapper;
@Autowired
private RestTemplate restTemplate;
/**
* 根据id查找订单
*/
@Override
public 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 {
@Autowired
private OrderService orderService;
@GetMapping("/{id}")
public Order selectOrder(@PathVariable("id") Long id){
return orderService.selectById(id);
}
}
config:
@Component
public class RestTemplateConfig {
/**
* 创建 RestTemplate 并注入 Spring 容器
* @LoadBalanced 注解描述:负载均衡作用
*/
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
4、user-service各层代码
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 {
@Autowired
private UserMapper userMapper;
@Override
public User selectById(Long id) {
return userMapper.getById(id);
}
}
controller:
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User select(@PathVariable("id") Long id){
return userService.selectById(id);
}
}