第三次作业:多对一,多对多

This commit is contained in:
LeeMONG 2024-10-13 12:49:54 +08:00
parent 4ee1ace139
commit af2c0278e3
15 changed files with 192 additions and 1 deletions

15
pom.xml
View File

@ -66,11 +66,26 @@
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<!--<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!--<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>-->
</dependencies>
<build>

View File

@ -1,7 +1,9 @@
package com.c202201020242.task1.task1.controller;
import com.c202201020242.task1.task1.model.Books;
import com.c202201020242.task1.task1.model.Reviews;
import com.c202201020242.task1.task1.service.BooksService;
import com.c202201020242.task1.task1.service.ReviewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@ -12,9 +14,12 @@ import org.springframework.web.bind.annotation.RequestMapping;
public class BooksController {
@Autowired
private BooksService booksService;
@Autowired
private ReviewsService reviewsService;
@RequestMapping("/books")
public String books(Model model){
model.addAttribute("BookList",booksService.getAllBooks());
return "book.html";
}
@RequestMapping("/addbooks")

View File

@ -1,7 +1,9 @@
package com.c202201020242.task1.task1.controller;
import com.c202201020242.task1.task1.dao.ReviewsMapper;
import com.c202201020242.task1.task1.model.Users;
import com.c202201020242.task1.task1.service.BooksService;
import com.c202201020242.task1.task1.service.ReviewsService;
import com.c202201020242.task1.task1.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@ -14,6 +16,8 @@ public class UsersController {
private UsersService usersService;
@Autowired
private BooksService booksService;
@Autowired
private ReviewsService reviewsService;
@RequestMapping("/login")
public String login(){

View File

@ -1,6 +1,7 @@
package com.c202201020242.task1.task1.dao;
import com.c202201020242.task1.task1.model.Books;
import com.c202201020242.task1.task1.model.Reviews;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@ -11,5 +12,9 @@ public interface BooksMapper {
public int addBooks(Books books);
public int deleteBooks(int id);
public int updateBooks(Books books);
public List<Books> getAllBooksAndReviews();
//添加评论
public int addReview(Reviews reviews);
public List<Books> findBookByBookid(int bookid);
}

View File

@ -1,12 +1,17 @@
package com.c202201020242.task1.task1.dao;
import com.c202201020242.task1.task1.model.Books;
import com.c202201020242.task1.task1.model.Users;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UsersMapper {
//注册
public int addUsers(Users users);
//查询用户名和密码是否正确
public Users getUsers(String username, String password);
public List<Users> getAllUsers();
}

View File

@ -3,6 +3,7 @@ package com.c202201020242.task1.task1.model;
import lombok.Data;
import java.util.Date;
import java.util.List;
@Data
public class Books {
@ -12,4 +13,5 @@ public class Books {
private String isbn;
private String publisher;
private String published_date;
private List<Reviews> reviewsList;
}

View File

@ -2,9 +2,14 @@ package com.c202201020242.task1.task1.model;
import lombok.Data;
import java.util.List;
@Data
public class Users {
private String username;
private String password;
private int id;
private List<Reviews> reviewsList;
private List<Borrow_records> borrow_recordsList;
}

View File

@ -1,6 +1,7 @@
package com.c202201020242.task1.task1.service;
import com.c202201020242.task1.task1.model.Books;
import com.c202201020242.task1.task1.model.Reviews;
import java.util.List;
@ -9,5 +10,7 @@ public interface BooksService {
public int addBooks(Books books);
public int deleteBooks(int id);
public int updateBooks(Books books);
public List<Books> getAllBooksAndReviews();
public int addReview(Reviews reviews);
}

View File

@ -2,6 +2,7 @@ package com.c202201020242.task1.task1.service.Impl;
import com.c202201020242.task1.task1.dao.BooksMapper;
import com.c202201020242.task1.task1.model.Books;
import com.c202201020242.task1.task1.model.Reviews;
import com.c202201020242.task1.task1.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -31,4 +32,15 @@ public class BooksServiceImpl implements BooksService {
public int updateBooks(Books books) {
return booksMapper.updateBooks(books);
}
@Override
public List<Books> getAllBooksAndReviews() {
return booksMapper.getAllBooksAndReviews();
}
@Override
public int addReview(Reviews reviews) {
return booksMapper.addReview(reviews);
}
}

View File

@ -6,6 +6,8 @@ import com.c202201020242.task1.task1.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UsersServiceImpl implements UsersService {
@Autowired
@ -19,4 +21,10 @@ public class UsersServiceImpl implements UsersService {
public Users getUsers(String username, String password) {
return usersMapper.getUsers(username, password);
}
@Override
public List<Users> getAllUsers() {
return usersMapper.getAllUsers();
}
}

View File

@ -2,8 +2,11 @@ package com.c202201020242.task1.task1.service;
import com.c202201020242.task1.task1.model.Users;
import java.util.List;
public interface UsersService {
public int addUsers(Users users);
public Users getUsers(String username, String password);
public List<Users> getAllUsers();
}

View File

@ -16,4 +16,32 @@
SET title=#{title},author=#{author},isbn=#{isbn},publisher=#{publisher},published_date=#{published_date}
WHERE id=#{id}
</update>
<select id="getAllBooksAndReviews" resultMap="ReviewMap">
SELECT * FROM books202201020242
</select>
<resultMap id="ReviewMap" type="com.c202201020242.task1.task1.model.Books">
<id column="id" property="id"/>
<result property="title" column="title"/>
<result property="author" column="author"/>
<result property="isbn" column="isbn"/>
<result property="publisher" column="publisher"/>
<result property="published_date" column="published_date"/>
<collection property="reviewsList"
ofType="com.c202201020242.task1.task1.model.Reviews"
column="title"
select="com.c202201020242.task1.task1.dao.ReviewsMapper.findReviewByTitle">
</collection>
</resultMap>
<insert id="addReview" parameterType="com.c202201020242.task1.task1.model.Reviews">
INSERT INTO reviews202201020242(bookname,reviews,username) VALUES(#{bookname},#{reviews},#{username})
</insert>
<select id="findBookByBookid" parameterType="String" resultType="com.c202201020242.task1.task1.model.Books">
SELECT * FROM books202201020242 WHERE id=#{bookid}
</select>
</mapper>

View File

@ -4,7 +4,31 @@
<insert id="addUsers" parameterType="com.c202201020242.task1.task1.model.Users">
INSERT INTO users202201020242(username,password) VALUES(#{username},#{password})
</insert>
<select id="getUsers" parameterType="com.c202201020242.task1.task1.model.Users" resultType="com.c202201020242.task1.task1.model.Users">
<select id="getUsers" parameterType="com.c202201020242.task1.task1.model.Users" resultMap="ReviewMap">
SELECT * FROM users202201020242 WHERE username=#{username} AND password=#{password}
</select>
<resultMap id="ReviewMap" type="com.c202201020242.task1.task1.model.Users">
<id column="id" property="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<collection property="reviewsList"
ofType="com.c202201020242.task1.task1.model.Reviews"
column="username"
select="com.c202201020242.task1.task1.dao.ReviewsMapper.findReviewsByUsersId">
</collection>
</resultMap>
<select id="getAllUsers" resultMap="All">
SELECT * FROM users202201020242
</select>
<resultMap id="All" type="com.c202201020242.task1.task1.model.Users">
<id column="id" property="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<collection property="borrow_recordsList"
ofType="com.c202201020242.task1.task1.model.Borrow_records"
column="id"
select="com.c202201020242.task1.task1.dao.Borrow_recordsMapper.findBorrow_recordsByUserId">
</collection>
</resultMap>
</mapper>

View File

@ -30,6 +30,12 @@
</table>
</br>
<Button><a th:href="@{/addbooks}" style="text-decoration: none;color: darkblue;width: 400px">添加书籍</a></Button>
<br>
<br>
<button><a href="/reviews">第三次作业</a></button>
</div>
</body>
</html>

View File

@ -0,0 +1,66 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>书籍评论</title>
</head>
<body>
<h4>书籍评价</h4>
<table border="1">
<tr style="color: cornflowerblue">
<td>id</td>
<td>书名</td>
<td>作者</td>
<td>出版社</td>
<td>评论</td>
<td>操作</td>
</tr>
<tr th:each="reviews:${BookAndReviewList}">
<td th:text="${reviews.id}"></td>
<td th:text="${reviews.title}"></td>
<td th:text="${reviews.author}"></td>
<td th:text="${reviews.publisher}"></td>
<!--判断评论是否为空-->
<td th:if="${reviews.reviewsList}!= null">
<ul>
<li th:each="re:${reviews.reviewsList}">
<span th:text="${re.username}+' : '+${re.reviews}"></span>
</li>
</ul>
</td>
<td><a th:href="@{/addreview}">添加评论</a></td>
</tr>
</table>
</br>
</br>
</br>
<h4>书籍借阅</h4>
<table border="1">
<tr style="color: cornflowerblue">
<td>id</td>
<td>用户名</td>
<td>借阅的书籍</td>
<td colspan="3">操作</td>
</tr>
<tr th:each="borrow:${UserList}">
<td th:text="${borrow.id}"></td>
<td th:text="${borrow.username}"></td>
<td th:if="${borrow.borrow_recordsList}!= null">
<ul>
<li th:each="book:${borrow.borrow_recordsList}">
<span th:text="'编号:'+${book.bookid}"></span><a th:href="@{/updateborrow(x=${book.bookid},y=${borrow.id})}" style="text-decoration: none;color: green">&nbsp&nbsp&nbsp修改此书籍借阅信息</a>
<div th:if="${book.booksList}!=null"
th:each="xxx:${book.booksList}">
<span th:text="'书名: '+${xxx.title}"></span>
</div>
</li>
</ul>
</td>
<td><a th:href="@{/borrow}">借阅书籍</a></td>
<td><a th:href="@{/return}">归还书籍</a></td>
<td></td>
</tr>
</table>
</body>
</html>