第三次作业:多对一,多对多
This commit is contained in:
parent
4ee1ace139
commit
af2c0278e3
15
pom.xml
15
pom.xml
|
@ -66,11 +66,26 @@
|
||||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
<version>3.0.3</version>
|
<version>3.0.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!--<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
||||||
|
<version>3.5.7</version>
|
||||||
|
</dependency>-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>mysql</groupId>
|
<groupId>mysql</groupId>
|
||||||
<artifactId>mysql-connector-java</artifactId>
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
<version>8.0.25</version>
|
<version>8.0.25</version>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package com.c202201020242.task1.task1.controller;
|
package com.c202201020242.task1.task1.controller;
|
||||||
|
|
||||||
import com.c202201020242.task1.task1.model.Books;
|
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.BooksService;
|
||||||
|
import com.c202201020242.task1.task1.service.ReviewsService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
|
||||||
|
@ -12,9 +14,12 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
public class BooksController {
|
public class BooksController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private BooksService booksService;
|
private BooksService booksService;
|
||||||
|
@Autowired
|
||||||
|
private ReviewsService reviewsService;
|
||||||
@RequestMapping("/books")
|
@RequestMapping("/books")
|
||||||
public String books(Model model){
|
public String books(Model model){
|
||||||
model.addAttribute("BookList",booksService.getAllBooks());
|
model.addAttribute("BookList",booksService.getAllBooks());
|
||||||
|
|
||||||
return "book.html";
|
return "book.html";
|
||||||
}
|
}
|
||||||
@RequestMapping("/addbooks")
|
@RequestMapping("/addbooks")
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package com.c202201020242.task1.task1.controller;
|
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.model.Users;
|
||||||
import com.c202201020242.task1.task1.service.BooksService;
|
import com.c202201020242.task1.task1.service.BooksService;
|
||||||
|
import com.c202201020242.task1.task1.service.ReviewsService;
|
||||||
import com.c202201020242.task1.task1.service.UsersService;
|
import com.c202201020242.task1.task1.service.UsersService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
@ -14,6 +16,8 @@ public class UsersController {
|
||||||
private UsersService usersService;
|
private UsersService usersService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private BooksService booksService;
|
private BooksService booksService;
|
||||||
|
@Autowired
|
||||||
|
private ReviewsService reviewsService;
|
||||||
|
|
||||||
@RequestMapping("/login")
|
@RequestMapping("/login")
|
||||||
public String login(){
|
public String login(){
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.c202201020242.task1.task1.dao;
|
package com.c202201020242.task1.task1.dao;
|
||||||
|
|
||||||
import com.c202201020242.task1.task1.model.Books;
|
import com.c202201020242.task1.task1.model.Books;
|
||||||
|
import com.c202201020242.task1.task1.model.Reviews;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -11,5 +12,9 @@ public interface BooksMapper {
|
||||||
public int addBooks(Books books);
|
public int addBooks(Books books);
|
||||||
public int deleteBooks(int id);
|
public int deleteBooks(int id);
|
||||||
public int updateBooks(Books books);
|
public int updateBooks(Books books);
|
||||||
|
public List<Books> getAllBooksAndReviews();
|
||||||
|
//添加评论
|
||||||
|
public int addReview(Reviews reviews);
|
||||||
|
public List<Books> findBookByBookid(int bookid);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,17 @@
|
||||||
package com.c202201020242.task1.task1.dao;
|
package com.c202201020242.task1.task1.dao;
|
||||||
|
|
||||||
|
import com.c202201020242.task1.task1.model.Books;
|
||||||
import com.c202201020242.task1.task1.model.Users;
|
import com.c202201020242.task1.task1.model.Users;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface UsersMapper {
|
public interface UsersMapper {
|
||||||
//注册
|
//注册
|
||||||
public int addUsers(Users users);
|
public int addUsers(Users users);
|
||||||
//查询用户名和密码是否正确
|
//查询用户名和密码是否正确
|
||||||
public Users getUsers(String username, String password);
|
public Users getUsers(String username, String password);
|
||||||
|
|
||||||
|
public List<Users> getAllUsers();
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.c202201020242.task1.task1.model;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class Books {
|
public class Books {
|
||||||
|
@ -12,4 +13,5 @@ public class Books {
|
||||||
private String isbn;
|
private String isbn;
|
||||||
private String publisher;
|
private String publisher;
|
||||||
private String published_date;
|
private String published_date;
|
||||||
|
private List<Reviews> reviewsList;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,14 @@ package com.c202201020242.task1.task1.model;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class Users {
|
public class Users {
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
|
private List<Reviews> reviewsList;
|
||||||
|
private List<Borrow_records> borrow_recordsList;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.c202201020242.task1.task1.service;
|
package com.c202201020242.task1.task1.service;
|
||||||
|
|
||||||
import com.c202201020242.task1.task1.model.Books;
|
import com.c202201020242.task1.task1.model.Books;
|
||||||
|
import com.c202201020242.task1.task1.model.Reviews;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -9,5 +10,7 @@ public interface BooksService {
|
||||||
public int addBooks(Books books);
|
public int addBooks(Books books);
|
||||||
public int deleteBooks(int id);
|
public int deleteBooks(int id);
|
||||||
public int updateBooks(Books books);
|
public int updateBooks(Books books);
|
||||||
|
public List<Books> getAllBooksAndReviews();
|
||||||
|
public int addReview(Reviews reviews);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.c202201020242.task1.task1.service.Impl;
|
||||||
|
|
||||||
import com.c202201020242.task1.task1.dao.BooksMapper;
|
import com.c202201020242.task1.task1.dao.BooksMapper;
|
||||||
import com.c202201020242.task1.task1.model.Books;
|
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.BooksService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
@ -31,4 +32,15 @@ public class BooksServiceImpl implements BooksService {
|
||||||
public int updateBooks(Books books) {
|
public int updateBooks(Books books) {
|
||||||
return booksMapper.updateBooks(books);
|
return booksMapper.updateBooks(books);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Books> getAllBooksAndReviews() {
|
||||||
|
return booksMapper.getAllBooksAndReviews();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int addReview(Reviews reviews) {
|
||||||
|
return booksMapper.addReview(reviews);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ import com.c202201020242.task1.task1.service.UsersService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UsersServiceImpl implements UsersService {
|
public class UsersServiceImpl implements UsersService {
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@ -19,4 +21,10 @@ public class UsersServiceImpl implements UsersService {
|
||||||
public Users getUsers(String username, String password) {
|
public Users getUsers(String username, String password) {
|
||||||
return usersMapper.getUsers(username, password);
|
return usersMapper.getUsers(username, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Users> getAllUsers() {
|
||||||
|
return usersMapper.getAllUsers();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,11 @@ package com.c202201020242.task1.task1.service;
|
||||||
|
|
||||||
import com.c202201020242.task1.task1.model.Users;
|
import com.c202201020242.task1.task1.model.Users;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface UsersService {
|
public interface UsersService {
|
||||||
public int addUsers(Users users);
|
public int addUsers(Users users);
|
||||||
public Users getUsers(String username, String password);
|
public Users getUsers(String username, String password);
|
||||||
|
public List<Users> getAllUsers();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,4 +16,32 @@
|
||||||
SET title=#{title},author=#{author},isbn=#{isbn},publisher=#{publisher},published_date=#{published_date}
|
SET title=#{title},author=#{author},isbn=#{isbn},publisher=#{publisher},published_date=#{published_date}
|
||||||
WHERE id=#{id}
|
WHERE id=#{id}
|
||||||
</update>
|
</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>
|
</mapper>
|
||||||
|
|
|
@ -4,7 +4,31 @@
|
||||||
<insert id="addUsers" parameterType="com.c202201020242.task1.task1.model.Users">
|
<insert id="addUsers" parameterType="com.c202201020242.task1.task1.model.Users">
|
||||||
INSERT INTO users202201020242(username,password) VALUES(#{username},#{password})
|
INSERT INTO users202201020242(username,password) VALUES(#{username},#{password})
|
||||||
</insert>
|
</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 * FROM users202201020242 WHERE username=#{username} AND password=#{password}
|
||||||
</select>
|
</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>
|
</mapper>
|
|
@ -30,6 +30,12 @@
|
||||||
</table>
|
</table>
|
||||||
</br>
|
</br>
|
||||||
<Button><a th:href="@{/addbooks}" style="text-decoration: none;color: darkblue;width: 400px">添加书籍</a></Button>
|
<Button><a th:href="@{/addbooks}" style="text-decoration: none;color: darkblue;width: 400px">添加书籍</a></Button>
|
||||||
|
|
||||||
|
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
|
<button><a href="/reviews">第三次作业</a></button>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -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">   修改此书籍借阅信息</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>
|
Loading…
Reference in New Issue