第二次作业

This commit is contained in:
adan 2024-10-15 19:23:09 +08:00
commit 879eb9a12a
39 changed files with 776 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package com.c202201020417.task1;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Task1Application.class);
}
}

View File

@ -0,0 +1,13 @@
package com.c202201020417.task1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Task1Application {
public static void main(String[] args) {
SpringApplication.run(Task1Application.class, args);
}
}

View File

@ -0,0 +1,32 @@
package com.c202201020417.task1;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.nio.file.Paths;
public class Test {
public static void main(String[] args) {
FastAutoGenerator.create("jdbc:mysql://106.53.194.250:63306/task202201020417?serverTimezone=UTC ",
"202201020417", "@hnucm1254")
.globalConfig(builder -> builder
.author("张三")
.outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")
.commentDate("yyyy-MM-dd")
)
.packageConfig(builder -> builder
.parent("com.baomidou.mybatisplus")
.entity("entity")
.mapper("mapper")
.service("service")
.serviceImpl("service.impl")
.xml("mapper.xml")
)
.strategyConfig(builder -> builder
.entityBuilder()
.enableLombok()
)
.templateEngine(new FreemarkerTemplateEngine())
.execute();
}
}

View File

@ -0,0 +1,45 @@
package com.c202201020417.task1.controller;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Reviews;
import com.c202201020417.task1.service.BooksService;
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 BooksController {
@Autowired
private BooksService booksService;
@RequestMapping("/bookpage")
public String bookpage(Model model){
model.addAttribute("booksList",booksService.getAllBook());
return "bookpage.html";
}
@RequestMapping("/addbook")
public String addbook(Books books){
return "addbookpage.html";
}
@RequestMapping("/addbookcommit")
public String addbookcommit(Books books){
booksService.addBook(books);
return "redirect:/bookpage";
}
@RequestMapping("/deletebook")
public String deletebook(int id){
booksService.deleteBook(id);
return "redirect:/bookpage";
}
@RequestMapping("/updatebook")
public String updatebook(Books books,Model model){
model.addAttribute("books",books);
return "updatebook.html";
}
@RequestMapping("/updatebookcommit")
public String updatebookcommit(Books books){
booksService.updateBook(books);
return "redirect:/bookpage";
}
}

View File

@ -0,0 +1,52 @@
package com.c202201020417.task1.controller;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Borrow;
import com.c202201020417.task1.service.BooksService;
import com.c202201020417.task1.service.BorrowService;
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 BorrowController {
@Autowired
private BorrowService borrowService;
@RequestMapping("/addborrow")
public String addborrow(Borrow borrow)
{
return "addborrow.html";
}
@RequestMapping("/addborrowcommit")
public String addborrowcommit(Borrow borrow)
{
borrowService.addBorrow(borrow);
return "redirect:/bookpage";
}
@RequestMapping("/updateborrow")
public String updateborrow(Borrow borrow,Model model)
{
model.addAttribute("borrow",borrow);
return "updateborrow.html";
}
@RequestMapping("/updateborrowcommit")
public String updateborrowcommit(Borrow borrow)
{
borrowService.updateBorrow(borrow);
return "redirect:/bookpage";
}
@RequestMapping("/deleteborrow")
public String deleteborrow(Integer id)
{
return "deleteborrow.html";
}
@RequestMapping("/deleteborrowcommit")
public String deleteborrowcommit(Integer id)
{
borrowService.deleteBorrow(id);
return "redirect:/bookpage";
}
}

View File

@ -0,0 +1,35 @@
package com.c202201020417.task1.controller;
import com.c202201020417.task1.model.User;
import com.c202201020417.task1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LoginController {
@Autowired
private UserService userService;
@RequestMapping("/login")
public String login() {
return "login.html";
}
@RequestMapping("/logincommit")
public String logincommit(String username, String password) {
User user = userService.findUserByName(username);
if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
return "redirect:/bookpage";
}
return "fail.html";
}
@RequestMapping("/adduser")
public String adduser(User user){
return "adduserpage.html";
}
@RequestMapping("/addusercommit")
public String addusercommit(User user){
userService.addUser(user);
return "redirect:/login";
}
}

View File

@ -0,0 +1,27 @@
package com.c202201020417.task1.controller;
import com.c202201020417.task1.dao.ReviewsMapper;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Reviews;
import com.c202201020417.task1.service.BooksService;
import com.c202201020417.task1.service.ReviewsService;
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 ReviewsController {
@Autowired
private ReviewsService reviewsService;
@RequestMapping("/addreview")
public String addreviews(Reviews reviews){
return "addreview.html";
}
@RequestMapping("/addreviewcommit")
public String addreviewcommit(Reviews reviews){
reviewsService.addReviews(reviews);
return "redirect:/bookpage";
}
}

View File

@ -0,0 +1,16 @@
package com.c202201020417.task1.dao;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Reviews;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface BooksMapper {
public List<Books> getAllBook();
//int 表示增加的条数
public int addBook(Books book);
public int deleteBook(int id);
public int updateBook(Books book);
}

View File

@ -0,0 +1,14 @@
package com.c202201020417.task1.dao;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Borrow;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface BorrowMapper {
public int addBorrow(Borrow borrow);
public int updateBorrow(Borrow borrow);
public int deleteBorrow(int id);
}

View File

@ -0,0 +1,13 @@
package com.c202201020417.task1.dao;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Reviews;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface ReviewsMapper {
public List<Reviews> findReviewsByBooksId(int booksid);
public int addReviews(Reviews reviews);
}

View File

@ -0,0 +1,14 @@
package com.c202201020417.task1.dao;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
public int addUser(User user);
public User findUserByName(String username);
public List<User> findUsersByBooksId( int booksid);
}

View File

@ -0,0 +1,20 @@
package com.c202201020417.task1.model;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
import java.util.List;
@Data
public class Books {
private int id;
private String title;
private String author;
private String publisher;
private String isbn;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date published_date;
private List<Reviews> reviewsList;
private List<User> usersList;
}

View File

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

View File

@ -0,0 +1,10 @@
package com.c202201020417.task1.model;
import lombok.Data;
@Data
public class Reviews {
private int id;
private String content;
private int booksid;
}

View File

@ -0,0 +1,12 @@
package com.c202201020417.task1.model;
import lombok.Data;
import java.util.List;
@Data
public class User {
private int id;
private String username;
private String password;
}

View File

@ -0,0 +1,13 @@
package com.c202201020417.task1.service;
import com.c202201020417.task1.model.Books;
import java.util.List;
public interface BooksService {
public List<Books> getAllBook();
//int 表示增加的条数
public int addBook(Books book);
public int deleteBook(int id);
public int updateBook(Books book);
}

View File

@ -0,0 +1,10 @@
package com.c202201020417.task1.service;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Borrow;
public interface BorrowService {
public int addBorrow(Borrow borrow);
public int updateBorrow(Borrow borrow);
public int deleteBorrow(int id);
}

View File

@ -0,0 +1,10 @@
package com.c202201020417.task1.service;
import com.c202201020417.task1.model.Reviews;
import java.util.List;
public interface ReviewsService {
public List<Reviews> findReviewsByBooksId(int booksid);
public int addReviews(Reviews reviews);
}

View File

@ -0,0 +1,8 @@
package com.c202201020417.task1.service;
import com.c202201020417.task1.model.User;
public interface UserService {
public User findUserByName(String username);
public int addUser(User user);
}

View File

@ -0,0 +1,39 @@
package com.c202201020417.task1.service.impl;
import com.c202201020417.task1.dao.BooksMapper;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Reviews;
import com.c202201020417.task1.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BooksServiceImpl implements BooksService {
@Autowired
private BooksMapper booksMapper;
@Override
public List<Books> getAllBook() {
return booksMapper.getAllBook();
}
@Override
public int addBook(Books book) {
return booksMapper.addBook(book);
}
@Override
public int deleteBook(int id) {
return booksMapper.deleteBook(id);
}
@Override
public int updateBook(Books book) {
return booksMapper.updateBook(book);
}
}

View File

@ -0,0 +1,29 @@
package com.c202201020417.task1.service.impl;
import com.c202201020417.task1.dao.BooksMapper;
import com.c202201020417.task1.dao.BorrowMapper;
import com.c202201020417.task1.model.Books;
import com.c202201020417.task1.model.Borrow;
import com.c202201020417.task1.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BorrowServiceImpl implements BorrowService {
@Autowired
private BorrowMapper borrowMapper;
@Override
public int addBorrow(Borrow borrow) {
return borrowMapper.addBorrow(borrow);
}
@Override
public int updateBorrow(Borrow borrow) {
return borrowMapper.updateBorrow(borrow);
}
@Override
public int deleteBorrow(int id) {
return borrowMapper.deleteBorrow(id);
}
}

View File

@ -0,0 +1,26 @@
package com.c202201020417.task1.service.impl;
import com.c202201020417.task1.dao.ReviewsMapper;
import com.c202201020417.task1.model.Reviews;
import com.c202201020417.task1.service.ReviewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ReviewsServiceImpl implements ReviewsService {
@Autowired
private ReviewsMapper reviewsMapper;
@Override
public List<Reviews> findReviewsByBooksId(int booksid) {
return List.of();
}
@Override
public int addReviews(Reviews reviews)
{
return reviewsMapper.addReviews(reviews);
}
}

View File

@ -0,0 +1,22 @@
package com.c202201020417.task1.service.impl;
import com.c202201020417.task1.dao.UserMapper;
import com.c202201020417.task1.model.User;
import com.c202201020417.task1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findUserByName(String username) {
return userMapper.findUserByName(username);
}
@Override
public int addUser(User user) {
return userMapper.addUser(user);
}
}

View File

@ -0,0 +1,18 @@
spring.application.name=task1
# ??????
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# ?????
spring.datasource.name=defaultDataSource
# ???????
spring.datasource.url=jdbc:mysql://106.53.194.250:63306/task202201020417?serverTimezone=UTC
# ??????&???
spring.datasource.username=202201020417
spring.datasource.password=@hnucm1254
#??????????MyBatis??
#??Mybatis?Mapper??
mybatis-plus.mapper-locations=classpath:mapper/*.xml
#??Mybatis?????
mybatis-plus.type-aliases-package=com.c202201020417.task1.model
logging.level.com.c202201020417.task1 = debug

View File

@ -0,0 +1,35 @@
<?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.c202201020417.task1.dao.BooksMapper">
<select id="getAllBook" resultMap="BooksMap">
select * from books202201020417
</select>
<insert id="addBook" parameterType="com.c202201020417.task1.model.Books">
insert into books202201020417(title,author,isbn,publisher,published_date) values(#{title},#{author},#{isbn},#{publisher},#{published_date})
</insert>
<delete id="deleteBook" parameterType="Integer">
delete from books202201020417 where id=#{id}
</delete>
<update id="updateBook" parameterType="com.c202201020417.task1.model.Books">
update books202201020417 set title=#{title},author=#{author},isbn=#{isbn},publisher=#{publisher},published_date=#{published_date} where id=#{id}
</update>
<resultMap id="BooksMap" type="com.c202201020417.task1.model.Books">
<id column="id" property="id"/>
<result column="title" property="title"/>
<result column="author" property="author"/>
<result column="publisher" property="publisher"/>
<result column="isbn" property="isbn"/>
<result column="published_date" property="published_date"/>
<collection property="reviewsList"
column="id"
select="com.c202201020417.task1.dao.ReviewsMapper.findReviewsByBooksId"
ofType="com.c202201020417.task1.model.Reviews">
</collection>
<collection property="usersList"
column="id"
select="com.c202201020417.task1.dao.UserMapper.findUsersByBooksId"
ofType="com.c202201020417.task1.model.User">
</collection>
</resultMap>
</mapper>

View File

@ -0,0 +1,14 @@
<?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.c202201020417.task1.dao.BorrowMapper">
<insert id="addBorrow" parameterType="com.c202201020417.task1.model.Borrow">
insert into borrow_records202201020417(usersid,booksid) values(#{usersid},#{booksid})
</insert>
<update id="updateBorrow" parameterType="com.c202201020417.task1.model.Borrow">
update borrow_records202201020417 set usersid=#{usersid} ,booksid=#{booksid} where id=#{id}
</update>
<delete id="deleteBorrow" parameterType="Integer">
delete from borrow_records202201020417 where id=#{id}
</delete>
</mapper>

View File

@ -0,0 +1,10 @@
<?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.c202201020417.task1.dao.ReviewsMapper">
<select id="findReviewsByBooksId" resultType="com.c202201020417.task1.model.Reviews">
select * from reviews202201020417 where booksid=#{booksid}
</select>
<insert id="addReviews" parameterType="com.c202201020417.task1.model.Reviews">
insert into reviews202201020417(content,booksid) values(#{content},#{booksid})
</insert>
</mapper>

View File

@ -0,0 +1,15 @@
<?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.c202201020417.task1.dao.UserMapper">
<insert id="addUser" parameterType="com.c202201020417.task1.model.User">
insert into users202201020417(username,password) values(#{username},#{password})
</insert>
<select id="findUserByName" resultType="com.c202201020417.task1.model.User">
SELECT * FROM users202201020417 WHERE username = #{username}
</select>
<select id="findUsersByBooksId" resultType="com.c202201020417.task1.model.User">
select * from users202201020417 where id in (select usersid
from borrow_records202201020417
where booksid = #{booksid})
</select>
</mapper>

View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/addbookcommit}" method="post">
<input type="text" name="title" placeholder="标题"><br>
<input type="text" name="author" placeholder="作者"><br>
<input type="text" name="isbn" placeholder="isbn"><br>
<input type="text" name="publisher" placeholder="出版商"><br>
<input type="date" name="published_date" placeholder="出版日期"><br>
<input type="submit" value="提交"></input>
</form>
</body>
</html>

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/addborrowcommit}" method="post">
<input type="text" name="usersid" placeholder="用户id">
<input type="text" name="booksid" placeholder="书籍id"></input>
<input type="submit" value="提交"></input>
</form>
</body>
</html>

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/addreviewcommit}" method="post">
<input type="text" name="content" placeholder="评论内容">
<input type="text" name="booksid" th:value="${id}" placeholder="书籍id"> </input>
<input type="submit" value="提交"></input>
</form>
</body>
</html>

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/addusercommit}" method="post">
<input type="text" name="username" placeholder="用户名"><br>
<input type="password" name="password" placeholder="密码"><br>
<input type="submit" value="提交"></input>
</form>
</body>
</html>

View File

@ -0,0 +1,56 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>书籍列表</h1>
<a href="/addbook">增加书籍</a>
<a href="/addreview">增加评论</a>
<a href="/addborrow">增加借阅</a>
<a href="/updateborrow">修改借阅</a>
<a href="/deleteborrow">删除借阅</a>
<table border="1">
<tr>
<td>id</td>
<td>名称</td>
<td>作者</td>
<td>编号</td>
<td>出版社</td>
<td>出版日期</td>
<td>删除</td>
<td>修改</td>
<td>评论列表</td>
<td>借阅列表</td>
</tr>
<tr th:each="books:${booksList}">
<td th:text="${books.id}">id</td>
<td th:text="${books.title}">title</td>
<td th:text="${books.author}">author</td>
<td th:text="${books.isbn}">isbn</td>
<td th:text="${books.publisher}">pulisher</td>
<td th:text="${books.published_date}">published_date</td>
<td><a th:href="@{/deletebook(id=${books.id})}" >删除</a></td>
<td><a th:href="@{/updatebook(id=${books.id},title=${books.title},author=${books.author},isbn=${books.isbn},publisher=${books.publisher})}" >修改</a></td>
<td>
<ul>
<li th:each="reviews202201020417:${books.reviewsList}">
<span th:text="${reviews202201020417.content}"></span>
<span th:text="${reviews202201020417.booksid}"></span>
</li>
</ul>
</td>
<td>
<ul>
<li th:each="users202201020417:${books.usersList}">
<span th:text="${users202201020417.username}"></span>
</li>
</ul>
</td>
</tr>
</table>
</body>
</html>

View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/deleteborrowcommit}" method="post">
<input type="text" name="id" th:value="${id}" placeholder="借阅记录id"></input>
<input type="submit" value="提交"></input>
</form>
</body>
</html>

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>登陆失败</h1>
</body>
</html>

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>登录页面</h1>
<form th:action="@{/logincommit}" method="post">
<input type="text" name="username" placeholder="用户名"/><br/>
<input type="password" name="password" placeholder="密码"/><br/>
<input type="submit" value="登录"/></input><br>
<a href="/adduser">注册用户</a>
</form>
</body>
</html>

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>更新页面</h1>
<form th:action="@{/updatebookcommit}" method="post">
<input type="text" name="id" hidden th:value="${books.id}" placeholder="id">
<input type="text" name="title" th:value="${books.title}" placeholder="书名"/><br/>
<input type="text" name="author" th:value="${books.author}" placeholder="作者"/><br/>
<input type="text" name="isbn" th:value="${books.isbn}" placeholder="isbn"/><br/>
<input type="text" name="publisher" th:value="${books.publisher}" placeholder="出版商"/><br/>
<input type="text" name="published_date" th:value="${books.published_date}" placeholder="出版日期"/><br/>
<input type="submit" value="更新书籍信息"></input>
</form>
</body>
</html>

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/updateborrowcommit}" method="post">
<input type="text" name="id" th:value="${id}" placeholder="id"></input>
<input type="text" name="usersid" th:value="${usersid}" placeholder="用户id">
<input type="text" name="booksid" th:value="${booksid}" placeholder="书籍id"></input>
<input type="submit" value="提交"></input>
</form>
</body>
</html>

View File

@ -0,0 +1,13 @@
package com.c202201020417.task1;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Task1ApplicationTests {
@Test
void contextLoads() {
}
}