微服务Mybatis连接Mysql数据库
在class-common模块中,添加实体类,对应mysql中的表:
java
//使用lombok生成getset
@Data
public class User {
private Integer id;
private String name;
private String pwd;
private String headImg;
private String phone;
private Date createTime;
private String wechat;
}
@Data
public class Video {
private Integer id;
private String title;
private String summary;
private String coverImg;
private Integer price;
private Date createTime;
private Double point;
}
@Data
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;
}
Mybatis依赖导入 + 数据库配置
在父项目的POM中添加Mybatis依赖:
xml
<!-- 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驱动依赖:
xml
//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个模块配置数据库连接(记得修改端口、应用名称、数据库名称)
yaml
//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为例,添加启动类:
java
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 开发:
VideoController.java
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
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
java
import com.study.pojo.Video;
public interface VideoService {
//根据ID查询视频
Video findById(int videoId);
}
VideoServiceImpl.java
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);
}
}
启动后访问测试
java
http://127.0.0.1:9000/api/v1/video/find_by_id?videoId=