一、搭建service_user模块
在父工程service模块下面创建子模块service_user,也是一个Maven工程,此模块专门用来做用户的CRUD操作实现。
注意:当我们创建service_user模块时,这个节点名称可能不对,记得改成它上一个父工程的节点名称,service才是service_user的父工程。
创建UserController控制器,进行测试
@RestController //返回json数据
@RequestMapping(value = "user")
public class UserController {
@GetMapping(value = "list")
public String index(){
return "hello Spring Cloud!";
}
}
创建Spring Boot启动类
/**
* 启动类
*/
@SpringBootApplication
@ComponentScan(basePackages = {"com.itydf"})
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
编写全局配置文件
因为在service模块中,我们引入了MySQL依赖,所以此时我们需要配置一下MySQL,否则启动会报错,找不到连接。
application.properties
先随便在本地创建一个数据库。
# 服务端口
server.port=9901
# 环境设置:dev、test、prod
spring.profiles.active=dev
# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/你的数据库地址?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
#返回json的全局时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
启动service_user模块,输入地址测试![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3ac4d0b3b4b61d9aa3df3b984245a9f5.png)
成功了,此时基本的结构我们就搭建完成了。
这里说一下整个结构:一定要懂这个依赖关系。
- parent父工程,下面有service模块,service下面有service_user模块。
- service可以使用parent的公共依赖,service_user又可以使用service的依赖。
- 说明了Spring Cloud的依赖可以相互依赖。
后续文章,更新中。。。