基础版本完成

This commit is contained in:
2023-08-14 22:45:38 +08:00
commit 6c3b4925cf
28 changed files with 689 additions and 0 deletions

41
borrow-service/pom.xml Normal file
View File

@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>SpringCloudStudy</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>org.example</groupId>
<artifactId>borrow-service</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,18 @@
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* ClassName: BorrowApplication
* Package: com.test
*
* @author yovinchen
* @Create 2023/8/14 16:25
*/
@SpringBootApplication
public class BorrowApplication {
public static void main(String[] args) {
SpringApplication.run(BorrowApplication.class, args);
}
}

View File

@@ -0,0 +1,29 @@
package com.test.controller;
import com.test.entity.UserBorrowDetail;
import com.test.service.BorrowService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* ClassName: BorrowController
* Package: com.test.controller
*
* @author yovinchen
* @Create 2023/8/14 21:30
*/
@RestController
public class BorrowController {
@Resource
BorrowService service;
@RequestMapping("/borrow/{uid}")
UserBorrowDetail findUserBorrows(@PathVariable("uid") int uid){
return service.getUserBorrowDetailByUid(uid);
}
}

View File

@@ -0,0 +1,20 @@
package com.test.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.List;
/**
* ClassName: com.test.entity.UserBorrowDetail
* Package: PACKAGE_NAME
*
* @author yovinchen
* @Create 2023/8/14 21:22
*/
@Data
@AllArgsConstructor
public class UserBorrowDetail {
User user;
List<Book> bookList;
}

View File

@@ -0,0 +1,26 @@
package com.test.mapper;
import com.test.entity.Borrow;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* ClassName: BorrowMapper
* Package: com.test.mapper
*
* @author yovinchen
* @Create 2023/8/14 17:04
*/
@Mapper
public interface BorrowMapper {
@Select("select * from DB_BORROW where uid = #{uid}")
List<Borrow> getBorrowsByUid(int uid);
@Select("select * from DB_BORROW where bid = #{bid}")
List<Borrow> getBorrowsByBid(int bid);
@Select("select * from DB_BORROW where bid = #{bid} and uid = #{uid}")
Borrow getBorrow(int uid, int bid);
}

View File

@@ -0,0 +1,16 @@
package com.test.service;
import com.test.entity.User;
import com.test.entity.UserBorrowDetail;
/**
* ClassName: BorrowService
* Package: com.test.service
*
* @author yovinchen
* @Create 2023/8/14 17:04
*/
public interface BorrowService {
UserBorrowDetail getUserBorrowDetailByUid(int uid);
}

View File

@@ -0,0 +1,43 @@
package com.test.service.impl;
import com.test.entity.Book;
import com.test.entity.Borrow;
import com.test.entity.User;
import com.test.entity.UserBorrowDetail;
import com.test.mapper.BorrowMapper;
import com.test.service.BorrowService;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/**
* ClassName: BorrowServiceImpl
* Package: com.test.service.impl
*
* @author yovinchen
* @Create 2023/8/14 21:25
*/
@Service
public class BorrowServiceImpl implements BorrowService {
@Resource
BorrowMapper mapper;
@Override
public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
List<Borrow> borrow = mapper.getBorrowsByUid(uid);
//RestTemplate支持多种方式的远程调用
RestTemplate template = new RestTemplate();
//这里通过调用getForObject来请求其他服务并将结果自动进行封装
//获取User信息
User user = template.getForObject("http://localhost:8101/user/" + uid, User.class);
//获取每一本书的详细信息
List<Book> bookList = borrow.stream().map(b -> template.getForObject("http://localhost:8201/book/" + b.getBid(), Book.class)).collect(Collectors.toList());
return new UserBorrowDetail(user, bookList);
}
}

View File

@@ -0,0 +1,8 @@
server:
port: 8301
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://43.143.164.194:3306/mac
username: mac
password: mactest