在class-common服务中,添加实体类,对应mysql中的表:
image.png

  1. //自行生成get和set方法,此处省略
  2. public class User {
  3. private Integer id;
  4. private String name;
  5. private String pwd;
  6. private String headImg;
  7. private String phone;
  8. private Date createTime;
  9. private String wechat;
  10. }
  11. public class Video {
  12. private Integer id;
  13. private String title;
  14. private String summary;
  15. private String coverImg;
  16. private Integer price;
  17. private Date createTime;
  18. private Double point;
  19. }
  20. public class VideoOrder {
  21. private Integer id;
  22. private String outTradeNo;
  23. private Integer state;
  24. private Date createTime;
  25. private Integer totalFee;
  26. private Integer videoId;
  27. private String videoTitle;
  28. private String videoImg;
  29. private Integer userId;
  30. }

Mybatis依赖导入+数据库配置

  • class-video-service
  • class-user-service
  • class-order-service

在父POM中添加Mybatis依赖,添加后其他子项目即可使用:
image.png

<!-- Mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.2</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

在video、user、order服务中的POM添加Mybatis和Mysql驱动依赖:

//3个服务的POM都需要添加

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

3个模块配置数据库连接(记得修改端口、应用名称、数据库名称)

//3个服务分别添加,在resources目录下新建application.yml文件,添加如下内容,每个服务连接的数据库记得更改

server:
  port: 9000  # 服务端口,每个服务记得更改,确保未被占用
spring:
  application:
    name: class-video-service   # 服务名称,每个服务名记得更改,不能一样
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/cloud_video?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
# 控制台输出sql、下划线转驼峰
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true

测试数据库连接

以视频服务video为例,添加启动类:
image.png

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class VideoApplication {
    public static void main(String[] args) {
        SpringApplication.run(VideoApplication.class, args);
    }
}

controller->service->mapper 开发:
image.png

VideoController.java**

import com.study.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/video")
public class VideoController {
    @Autowired
    private VideoService videoService;

    //根据ID查询视频
    @GetMapping("/find_by_id")
    public Object findById(int videoId){
        return videoService.findById(videoId);
    }
}

VideoMapper.java

import com.study.pojo.Video;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

//此处注意@Repository和@Mapper的区别,使用@Mapper注解,不需要在启动类上加入@MapperScan注解,如果使用@Repository注解,则需要在启动类上加入@MapperScan注解
@Mapper
public interface VideoMapper {
    //根据ID查询视频
    @Select("select * from video where id = #{videoId}")
    Video findById(int videoId);
}

VideoService.java

import com.study.pojo.Video;

public interface VideoService {
    //根据ID查询视频
    Video findById(int videoId);
}

VideoServiceImpl.java

import com.study.mapper.VideoMapper;
import com.study.pojo.Video;
import com.study.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;

    //根据ID查询视频
    @Override
    public Video findById(int videoId) {
        return videoMapper.findById(videoId);
    }
}

启动后访问测试:

http://127.0.0.1:9000/api/v1/video/find_by_id?videoId=