故事紧接上回,在上一篇中,我们完成以下部分:
这个模块是用来存放系统上面用到的 bean 对象,方便与不同的微服务模块引用。这里我们创建几个 bean 对象:
User
public class User {
private Integer id;
private String name;
private String pwd;
private String headImg;
private String phone;
private Date createTime;
private String wechat;
// 省略 getter 与 setter
}
Video
public class Video {
private Integer id;
private String title;
private String summary;
private String coverImg;
private Integer price;
private Date createTime;
private Double point;
// 省略 getter 与 setter
}
VideoOrder
public class VideoOrder {
private Integer id;
private String outTradeNo;
private Integer state;
private Date createTime;
private Integer totalFee;
private Integer videoId;
private String videoTitle;
private String videoImg;
private Integer userId;
private String serverInfo;
// 省略 getter 与 setter
}
对应 order 模块,我们主要是引入对应的依赖包,以及增加对应的数据库里连接相关的配置
主要是增加 mybatis 的依赖
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
server:
port: 9000
spring:
application:
name: demo-order
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.102:3306/cloud_video?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: admin
password: 123456
# 控制台输出sql、下划线转驼峰
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
对应 video 模块,和上面的 order 模块一样,我们主要是引入对应的依赖包,以及增加对应的数据库里连接相关的配置
主要是增加 mybatis 的依赖
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
server:
port: 8000
spring:
application:
name: demo-video
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.152.129:3306/cloud_video?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: admin
password: 123456
# 控制台输出sql、下划线转驼峰
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
主要是增加对数据库的操作,以及用于接口访问的逻辑
@RestController
@RequestMapping("api/v1/video")
public class VideoController {
@Autowired
private VideoService videoService;
// http://localhost:9000/api/v1/video/find_by_id?videoId=30
@RequestMapping("find_by_id")
public Object findById(int videoId, HttpServletRequest httpRequest){
Video video = videoService.findById(videoId);
String serverInfo = httpRequest.getServerName() + ":"+ httpRequest.getServerPort();
video.setServerInfo(serverInfo);
return video;
}
/**
* 这里是接收传入过来的参数,使用的注解可以使用 @RequestMapping("saveByFeign") 或者 @PostMapping("saveByFeign")
* @param video Video
* @return 返回值
*/
@PostMapping("saveByFeign")
public int saveByFeign(@RequestBody Video video){
System.out.println("========>"+video.getTitle());
return 1;
}
}
@Service
public class VideoService {
@Autowired
private VideoMapper videoMapper;
public Video findById(int videoId) {
return videoMapper.findById(videoId);
}
}
@Repository
public interface VideoMapper {
@Select("select * from video where id=#{videoId}")
Video findById(@Param("videoId") int videoId);
}
@SpringBootApplication
@MapperScan("com.demo.mapper")
public class VideoApplication {
public static void main(String[] args) {
SpringApplication.run(VideoApplication.class, args);
}
}
http://localhost:8000/api/v1/video/find_by_id?videoId=30
最后,到这里为止,整个工程项目就搭建完成了,后面开始集成 nacos,作为注册中心来使用。