一对一及一对多

This commit is contained in:
饶秋霖 2024-10-15 18:03:54 +08:00
parent 2184054b4a
commit 44980ae4a1
28 changed files with 550 additions and 42 deletions

View File

@ -54,6 +54,7 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
@ -69,12 +70,7 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
<scope>compile</scope>
<scope>runtime</scope>
</dependency>
</dependencies>

View File

@ -2,7 +2,7 @@ package com.c202201020141.task1.work.Controller;
import com.c202201020141.task1.work.model.Book;
import com.c202201020141.task1.work.sevice.Impl.BookService;
import com.c202201020141.task1.work.sevice.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

View File

@ -0,0 +1,43 @@
package com.c202201020141.task1.work.Controller;
import com.c202201020141.task1.work.model.Borrow_records;
import com.c202201020141.task1.work.sevice.Borrow_recordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Borrow_recordsController {
@Autowired
private Borrow_recordsService borrow_recordsService;
@RequestMapping("/getborrow_records")
public String getBorrow_records(Model model){
model.addAttribute("borrow_recordsList",borrow_recordsService.getBorrow_records());
return "borrow_records.html";
}
@RequestMapping("/addborrow_recordspage")
public String addBorrow_recordsPage(){
return "addborrow_records.html";
}
@RequestMapping("/addborrow_recordscommit")
public String addBorrow_recordsCommit(Borrow_records borrow_records){
borrow_recordsService.addBorrow_records(borrow_records);
return "redirect:/getborrow_records";
}
@RequestMapping("/deleteborrow_records")
public String deleteBorrow_records(int id){
borrow_recordsService.deleteBorrow_records(id);
return "redirect:/getborrow_records";
}
@RequestMapping("/updateborrow_recordspage")
public String updateBorrow_records(Borrow_records borrow_records,Model model){
model.addAttribute("borrow_records",borrow_records);
return "updateborrow_records.html";
}
@RequestMapping("/updateborrow_recordscommit")
public String updateBorrow_recordsCommit(Borrow_records borrow_records){
borrow_recordsService.updateBorrow_records(borrow_records);
return "redirect:/getborrow_records";
}
}

View File

@ -2,7 +2,7 @@ package com.c202201020141.task1.work.Controller;
import com.c202201020141.task1.work.model.User;
import com.c202201020141.task1.work.sevice.Impl.UserService;
import com.c202201020141.task1.work.sevice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

View File

@ -1,6 +1,6 @@
package com.c202201020141.task1.work.dao;
import com.c202201021041.task1.work.model.Book;
import com.c202201020141.task1.work.model.Book;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;

View File

@ -0,0 +1,14 @@
package com.c202201020141.task1.work.dao;
import com.c202201020141.task1.work.model.Borrow_records;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface Borrow_recordsMapper {
public List<Borrow_records> getBorrow_records();
public int addBorrow_records(Borrow_records borrow_records);
public int deleteBorrow_records(int id);
public int updateBorrow_records(Borrow_records borrow_records);
}

View File

@ -0,0 +1,13 @@
package com.c202201020141.task1.work.dao;
import com.c202201020141.task1.work.model.Review;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface ReviewMapper {
public List<Review> findReviewByBooksid(int booksid);
public int addReview(Review review);
}

View File

@ -1,6 +1,6 @@
package com.c202201020141.task1.work.dao;
import com.c202201021041.task1.work.model.User;
import com.c202201020141.task1.work.model.User;
import org.apache.ibatis.annotations.Mapper;

View File

@ -0,0 +1,10 @@
package com.c202201020141.task1.work.model;
import lombok.Data;
@Data
public class Borrow_records {
private int id;
private int booksid;
private int usersid;
}

View File

@ -0,0 +1,11 @@
package com.c202201020141.task1.work.model;
import lombok.Data;
@Data
public class Review {
private int id;
private String evaluation;
private int usersid;
private int booksid;
}

View File

@ -1,6 +1,6 @@
package com.c202201020141.task1.work.sevice.Impl;
package com.c202201020141.task1.work.sevice;
import com.c202201021041.task1.work.model.Book;
import com.c202201020141.task1.work.model.Book;
import java.util.List;

View File

@ -0,0 +1,13 @@
package com.c202201020141.task1.work.sevice;
import com.c202201020141.task1.work.model.Borrow_records;
import java.util.List;
public interface Borrow_recordsService {
public List<Borrow_records> getBorrow_records();
public int addBorrow_records(Borrow_records borrow_records);
public int deleteBorrow_records(int id);
public int updateBorrow_records(Borrow_records borrow_records);
}

View File

@ -1,8 +1,9 @@
package com.c202201020141.task1.work.sevice.Impl;
import com.c202201020141.task1.work.dao.BookMapper;
import com.c202201020141.task1.work.model.Book;
import com.c202201020141.task1.work.sevice.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

View File

@ -0,0 +1,31 @@
package com.c202201020141.task1.work.sevice.Impl;
import com.c202201020141.task1.work.dao.Borrow_recordsMapper;
import com.c202201020141.task1.work.model.Borrow_records;
import com.c202201020141.task1.work.sevice.Borrow_recordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class Borrow_recordsServiceImpl implements Borrow_recordsService {
@Autowired
Borrow_recordsMapper borrow_recordsMapper;
@Override
public List<Borrow_records> getBorrow_records() {
return borrow_recordsMapper.getBorrow_records();
}
@Override
public int addBorrow_records(Borrow_records borrow_records) {
return borrow_recordsMapper.addBorrow_records(borrow_records);
}
@Override
public int deleteBorrow_records(int id) {
return borrow_recordsMapper.deleteBorrow_records(id);
}
@Override
public int updateBorrow_records(Borrow_records borrow_records) {
return borrow_recordsMapper.updateBorrow_records(borrow_records);
}
}

View File

@ -0,0 +1,19 @@
package com.c202201020141.task1.work.sevice.Impl;
import com.c202201020141.task1.work.dao.ReviewMapper;
import com.c202201020141.task1.work.model.Review;
import com.c202201020141.task1.work.sevice.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ReviewServiceImpl implements ReviewService {
@Autowired
ReviewMapper reviewMapper;
@Override
public int addReview(Review review){
return reviewMapper.addReview(review);
}
}

View File

@ -3,9 +3,9 @@ package com.c202201020141.task1.work.sevice.Impl;
import com.c202201020141.task1.work.dao.UserMapper;
import com.c202201020141.task1.work.model.User;
import org.springframework.stereotype.Service;
import com.c202201020141.task1.work.sevice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@ -17,6 +17,7 @@ public class UserServiceImpl implements UserService {
this.userMapper = userMapper;
}
@Override
public int addUser(User user) {
return userMapper.addUser(user);
}

View File

@ -0,0 +1,8 @@
package com.c202201020141.task1.work.sevice;
import com.c202201020141.task1.work.model.Review;
public interface ReviewService {
public int addReview(Review review);
}

View File

@ -1,8 +1,7 @@
package com.c202201020141.task1.work.sevice.Impl;
package com.c202201020141.task1.work.sevice;
import com.c202201021041.task1.work.model.User;
import java.util.List;
import com.c202201020141.task1.work.model.User;
public interface UserService {
public int addUser(User user);

View File

@ -8,7 +8,7 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# ?????
spring.datasource.name=defaultDataSource
# ???????
spring.datasource.url=jdbc:mysql://106.53.194.250:63306/mybatis202201020141?serverTimezone=UTC
spring.datasource.url=jdbc:mysql://106.53.194.250:63306/task202201020141?serverTimezone=UTC
# ??????&???
spring.datasource.username=202201020141
spring.datasource.password=@hnucm1254

View File

@ -1,26 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.c202201021041.task1.work.dao.BookMapper">
<select id="getAllBook" resultType="com.c202201021041.task1.work.model.Book">
<mapper namespace="com.c202201020141.task1.work.dao.BookMapper">
<select id="getAllBook" resultMap="bookMap">
select * from books202201020131
</select>
<insert id="addBook" parameterType="com.c202201021041.task1.work.model.Book">
insert into books202201020131(title,author,isbn,publisher,published_date) values(#{title},#{author},#{isbn},#{publisher},#{published_date})
<resultMap id="bookMap" type="com.c202201020141.task1.work.model.Book">
<id property="id" column="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="reviewList"
ofType="com.c202201020141.task1.work.model.Review"
column="id"
select="com.c202201021031.task1.springboot.homework2th.dao.ReviewMapper.findReviewByBooksid"
/>
<collection property="userList"
ofType="com.c202201020141.task1.work.model.User"
column="id"
select="com.c202201021031.task1.springboot.homework2th.dao.UserMapper.getUserByBookId"
/>
</resultMap>
<insert id="addBook" parameterType="com.c202201020141.task1.work.model.Book">
insert into books202201020131(title, author, isbn, publisher, published_date) values(#{title}, #{author}, #{isbn}, #{publisher}, #{published_date})
</insert>
<delete id="deleteBook" parameterType="int">
delete from books202201020131 where id=#{id}
</delete>
<!-- <update id="updateBooks" parameterType="com.c202201021031.task1.springboot.homework2th.model.Book">-->
<!-- update books202201020131 set title=#{title},author=#{author},isbn=#{isbn},publisher=#{publisher},published_date=#{published_date}} where id=#{id}-->
<!-- </update>-->
<update id="updateBook" parameterType="com.c202201021041.task1.work.model.Book">
update books202201020131 set title=#{title},author=#{author},isbn=#{isbn},publisher=#{publisher},published_date=#{published_date} where id=#{id}
<update id="updateBook" parameterType="com.c202201020141.task1.work.model.Book">
update books202201020131 set title=#{title}, author=#{author}, isbn=#{isbn}, publisher=#{publisher}, published_date=#{published_date} where id=#{id}
</update>
</mapper>
</mapper>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.c202201020141.task1.work.dao.Borrow_recordsMapper">
<select id="getBorrow_records" resultType="com.c202201020141.task1.work.model.Borrow_records">
select * from borrow_records202201020131
</select>
<insert id="addBorrow_records" parameterType="com.c202201020141.task1.work.model.Borrow_records">
insert into borrow_records202201020131(booksid,usersid) values(#{booksid},#{usersid})
</insert>
<delete id="deleteBorrow_records" parameterType="int">
delete from borrow_records202201020131 where id=#{id}
</delete>
<update id="updateBorrow_records" parameterType="com.c202201020141.task1.work.model.Borrow_records">
update borrow_records202201020131 set booksid=#{booksid},usersid=#{usersid} where id=#{id}
</update>
</mapper>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.c202201020141.task1.work.dao.ReviewMapper">
<insert id="addReview" parameterType="com.c202201020141.task1.work.model.Review">
insert into reviews202201020131(evaluation,usersid,booksid) values(#{evaluation},#{usersid},#{booksid})
</insert>
<select id="findReviewByBooksid" resultType="com.c202201020141.task1.work.model.Review">
select * from reviews202201020131 where booksid = #{booksid}
</select>
</mapper>

View File

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.c202201021041.task1.work.dao.UserMapper">
<insert id="addUser" parameterType="com.c202201021041.task1.work.model.User">
<mapper namespace="com.c202201020141.task1.work.dao.UserMapper">
<insert id="addUser" parameterType="com.c202201020141.task1.work.model.User">
insert into users202201020131(username,password) values(#{username},#{password})
</insert>
<select id="getUser" resultType="com.c202201021041.task1.work.model.User">
<select id="getUser" resultType="com.c202201020141.task1.work.model.User">
select * from users202201020131 where username=#{username} and password=#{password}
</select>

View File

@ -8,28 +8,28 @@
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
height: 120vh;
margin: 0;
background-color: #ffffff;
font-family: Arial, sans-serif;
}
.container {
width: 300px;
padding: 20px;
padding: 25px;
background-color: #ffffff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
border-radius: 8px;
text-align: center;
}
form {
margin-top: 20px;
margin-top: 25px;
}
input[type="text"], input[type="submit"] {
width: calc(100% - 22px);
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 15px;
margin-bottom: 15px;
border: 2px solid #ccc;
border-radius: 5px;
}
input[type="submit"] {
background-color: #00b374;

View File

@ -0,0 +1,68 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 120vh;
margin: 3;
}
.container {
text-align: center;
max-width: 300px;
margin: auto;
padding: 25px;
border: 2px solid #ccc;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
}
label {
display: block;
margin-bottom: 10px;
}
input[type="number"],
input[type="text"] {
width: 100%;
padding: 15px;
margin-bottom: 20px;
border: 2px solid #ccc;
border-radius: 3px;
box-sizing: border-box;
}
input[type="submit"] {
width: 100%;
padding: 10px;
background-color: #00b374;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #bfff00;
}
</style>
</head>
<body>
<div class="container">
<h1>添加借阅记录</h1>
<form action="/addborrow_recordscommit" method="post">
<label for="usersid">用户Id:</label>
<input type="number" name="usersid" id="usersid" placeholder="请输入用户id"/><br>
<label for="booksid">书籍Id:</label>
<input type="number" name="booksid" id="booksid" placeholder="请输入书籍id"/><br>
<input type="submit" value="添加借阅记录"/>
</form>
</div>
</body>
</html>

View File

@ -0,0 +1,68 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 120vh;
margin: 3;
}
.container {
text-align: center;
max-width: 300px;
margin: auto;
padding: 25px;
border: 2px solid #ccc;
border-radius: 15px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
}
label {
display: block;
margin-bottom: 5px;
}
input[type="number"],
input[type="text"] {
width: 100%;
padding: 15px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 3px;
box-sizing: border-box;
}
input[type="submit"] {
width: 100%;
padding: 10px;
background-color: #00b374;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #bfff00;
}
</style>
</head>
<body>
<div class="container">
<h1>添加评价</h1>
<form action="/addevaluationcommit" method="post">
<label for="booksid">书籍ID:</label>
<input type="number" name="booksid" id="booksid" placeholder="请输入书籍id"/><br>
<label for="evaluation">评价内容:</label>
<input type="text" name="evaluation" id="evaluation" placeholder="请输入评价"/><br>
<input type="submit" value="添加评价"/>
</form>
</div>
</body>
</html>

View File

@ -0,0 +1,95 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.btn {
display: inline-block;
padding: 15px 25px;
font-size: 10px;
color: #fff;
background-color: #a2a2a2;
border: none;
border-radius: 5px;
text-decoration: none;
cursor: pointer;
margin-top: 25px;
}
.btn:hover {
background-color: #b30000;
}
body {
display: flex;
justify-content: center;
align-items: center;
height: 120vh;
margin: 0;
background-color: #ffffff;
font-family: Arial, sans-serif;
}
.container {
width: 80%;
max-width: 1200px;
background-color: #fff;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0.2, 0.3);
border-radius: 10px;
text-align: left;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #ccc;
padding: 15px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
a {
text-decoration: none;
color: #007bff;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h1>借阅记录页面</h1>
<table border="1">
<tr>
<td>id</td>
<td>用户id</td>
<td>书籍id</td>
<td>删除</td>
<td>更新</td>
</tr>
<tr th:each="borrow_records:${borrow_recordsList}">
<td th:text="${borrow_records.id}"></td>
<td th:text="${borrow_records.usersid}"></td>
<td th:text="${borrow_records.booksid}"></td>
<td>
<a th:href="@{'/deleteborrow_records?id=' + ${borrow_records.id}}">删除</a>
</td>
<td>
<a class="update-link" th:href="@{/updateborrow_recordspage(id=${borrow_records.id},
usersid=${borrow_records.usersid},
booksid=${borrow_records.booksid})}">更新</a>
</td>
</tr>
</table>
<a href="/addborrow_recordspage">添加借阅记录</a>
<a href="/book">返回书籍页面</a>
</div>
</body>
</html>

View File

@ -0,0 +1,71 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 120vh;
margin: 0;
}
.container {
text-align: center;
max-width: 300px;
margin: auto;
padding: 25px;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0.2, 0.3);
}
label {
display: block;
margin-bottom: 10px;
}
input[type="number"],
input[type="text"] {
width: 100%;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
}
input[type="submit"] {
width: 100%;
padding: 10px;
background-color: #00b374;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #bfff00;
}
</style>
</head>
<body>
<div class="container">
<h1>更新借阅记录</h1>
<form action="/updateborrow_recordscommit" method="post">
<input type="hidden" name="id" th:value="${borrow_records.id}" />
<label for="usersid">用户Id:</label>
<input type="number" name="usersid" id="usersid" placeholder="请输入用户id" th:value="${borrow_records.usersid}"/><br>
<label for="booksid">书籍Id:</label>
<input type="number" name="booksid" id="booksid" placeholder="请输入书籍id" th:value="${borrow_records.booksid}"/><br>
<input type="submit" value="更新"/>
</br>
</br>
</form>
</div>
</body>
</html>