1.创建聚合工程 xdclass-cloud,修改pom文件
注意:记得删除聚合工程src目录
<packaging>pompackaging>
<properties>
<java.version>1.8java.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.3.3.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Hoxton.SR8version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.2.1.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<fork>truefork>
<addResources>trueaddResources>
configuration>
plugin>
plugins>
build>
2.创建4个子项目 xdclass-common、xdclass-video-service、xdclass-user-service、xdclass-order-service
3.添加子项目依赖
修改子项目xdclass-video-service、xdclass-user-service、xdclass-order-service下的pom文件
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>net.xdclassgroupId>
<artifactId>xdclass-commonartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
4.Mybatis依赖导入+数据库配,创建common包实体类
public class User {
private Integer id;
private String name;
private String pwd;
private String headImg;
private String phone;
private Date createTime;
private String wechat;
}
public class Video {
private Integer id;
private String title;
private String summary;
private String coverImg;
private Integer price;
private Date createTime;
private Double point;
}
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;
}
5.聚合工程pom.xml修改
<properties>
<java.version>1.8java.version>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.2version>
<type>pomtype>
<scope>importscope>
dependency>
6.添加mybatis依赖和数据库驱动
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
7.在xdclass-cloud\xdclass-video-service\src\main\resources创建application.yml
xdclass-user-service、xdclass-order-service项目下的端口及数据库名称要改成对应的
server:
port: 9000
spring:
application:
name: xdclass-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&serverTimezone=UTC
username: root
password: 123456
# 控制台输出sql、下划线转驼峰
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
8.controller->service->mapper 开发
@RestController
@RequestMapping("api/v1/video")
public class VideoController {
@Autowired
private VideoService videoService;
@RequestMapping("find_by_id")
public Object findVideoById(int videoId){
return videoService.findById(videoId);
}
}
@Service
public class VideoServiceImpl implements VideoService {
@Autowired
private VideoMapper videoMapper;
@Override
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);
}
9.启动类配置
@SpringBootApplication
@MapperScan("net.xdclass.dao")
public class VideoApplication {
public static void main(String[] args) {
SpringApplication.run(VideoApplication.class,args);
}
}