第三次作业第一次提交
This commit is contained in:
parent
d769142fdc
commit
50593e852d
|
@ -2,6 +2,7 @@ package com.lilingui.task1.controller;
|
|||
|
||||
import com.lilingui.task1.model.Book;
|
||||
import com.lilingui.task1.service.BookService;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
|
@ -11,14 +12,21 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||
public class BookConctroller {
|
||||
|
||||
@Autowired
|
||||
BookService bookService;
|
||||
private BookService bookService;
|
||||
|
||||
@RequestMapping("/books")
|
||||
public String getBookPage(Model model) {
|
||||
public String getBookPage(Model model, HttpSession session) {
|
||||
model.addAttribute("userid", session.getAttribute("userid"));
|
||||
model.addAttribute("username", session.getAttribute("username"));
|
||||
model.addAttribute("bookList", bookService.getAllBooks());
|
||||
return "books.html";
|
||||
}
|
||||
|
||||
@RequestMapping("/logout")
|
||||
public String login() {
|
||||
return "redirect:/login";
|
||||
}
|
||||
|
||||
@RequestMapping("/addbook")
|
||||
public String addBook() {
|
||||
return "addbook.html";
|
||||
|
|
|
@ -2,9 +2,9 @@ package com.lilingui.task1.controller;
|
|||
|
||||
import com.lilingui.task1.model.User;
|
||||
import com.lilingui.task1.service.UserService;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
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
|
||||
|
@ -23,16 +23,14 @@ public class UserConctroller {
|
|||
}
|
||||
|
||||
@RequestMapping("/logincommit")
|
||||
public String loginCommit(User user) {
|
||||
if(userService.login(user) != null)
|
||||
public String loginCommit(User user, HttpSession session) {
|
||||
if(userService.login(user) != null) {
|
||||
user = userService.login(user);
|
||||
session.setAttribute("userid", user.getId());
|
||||
session.setAttribute("username", user.getUsername());
|
||||
return "redirect:/books";
|
||||
}
|
||||
else
|
||||
return "redirect:/login";
|
||||
}
|
||||
|
||||
@RequestMapping("/registercommit")
|
||||
public String registerCommit(User user) {
|
||||
userService.register(user);
|
||||
return "redirect:/login";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.lilingui.task1.model;
|
|||
import lombok.Data;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class Book {
|
||||
|
@ -12,4 +13,6 @@ public class Book {
|
|||
private String isbn;
|
||||
private String publisher;
|
||||
private Date published_date;
|
||||
private List<Review> reviewsList;
|
||||
private List<BorrowRecord> borrowRecordsList;
|
||||
}
|
||||
|
|
|
@ -2,9 +2,29 @@
|
|||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
|
||||
<mapper namespace="com.lilingui.task1.dao.BookMapper">
|
||||
<select id="getAllBooks" resultType="com.lilingui.task1.model.Book">
|
||||
<select id="getAllBooks" resultMap="bookMap">
|
||||
select * from books
|
||||
</select>
|
||||
|
||||
<resultMap id="bookMap" type="com.lilingui.task1.model.Book">
|
||||
<id column="id" property="id"/>
|
||||
<result column="title" property="title"/>
|
||||
<result column="author" property="author"/>
|
||||
<result column="isbn" property="isbn"/>
|
||||
<result column="publisher" property="publisher"/>
|
||||
<result column="published_date" property="published_date"/>
|
||||
<collection property="reviewsList"
|
||||
ofType="com.lilingui.task1.model.Review"
|
||||
column="id"
|
||||
select="com.lilingui.task1.dao.ReviewMapper.getReviewsByBookId">
|
||||
</collection>
|
||||
<collection property="borrowRecordsList"
|
||||
ofType="com.lilingui.task1.model.BorrowRecord"
|
||||
column="id"
|
||||
select="com.lilingui.task1.dao.BorrowRecordMapper.getBorrowRecordsByBookId">
|
||||
</collection>
|
||||
</resultMap>
|
||||
|
||||
<insert id="addBook" parameterType="com.lilingui.task1.model.Book">
|
||||
insert into books(title,author,isbn,publisher,published_date) values(#{title},#{author},#{isbn},#{publisher},#{published_date})
|
||||
</insert>
|
||||
|
|
|
@ -5,6 +5,9 @@
|
|||
<select id="login" parameterType="com.lilingui.task1.model.User" resultType="com.lilingui.task1.model.User">
|
||||
SELECT * FROM users WHERE username = #{username} AND password = #{password}
|
||||
</select>
|
||||
<select id="getUsernameById" resultType="String">
|
||||
SELECT username FROM users WHERE id = #{id}
|
||||
</select>
|
||||
<insert id="register" parameterType="com.lilingui.task1.model.User">
|
||||
INSERT INTO users(username,password) VALUES(#{username},#{password})
|
||||
</insert>
|
||||
|
|
|
@ -6,7 +6,8 @@
|
|||
</head>
|
||||
<body>
|
||||
<h1>书籍目录</h1>
|
||||
<a href="/addbook">添加书籍</a>
|
||||
<h3 th:text="'欢迎您,'+${username}"></h3>
|
||||
<a href="/logout">注销</a>
|
||||
<table border="1">
|
||||
<tr>
|
||||
<td>id</td>
|
||||
|
@ -17,6 +18,8 @@
|
|||
<td>出版日期</td>
|
||||
<td>更新</td>
|
||||
<td>删除</td>
|
||||
<td>评论</td>
|
||||
<td>增加评论</td>
|
||||
</tr>
|
||||
<tr th:each="book:${bookList}">
|
||||
<td th:text="${book.id}"></td>
|
||||
|
@ -31,7 +34,19 @@
|
|||
<td>
|
||||
<a th:href="@{/deletebook(id=${book.id})}">删除</a>
|
||||
</td>
|
||||
<td th:if="${book.reviewsList}!=null">
|
||||
<ul>
|
||||
<li th:each="review:${book.reviewsList}">
|
||||
<span th:text="${review.commentator}+':'+${review.comment}"></span>
|
||||
</li>
|
||||
</ul>
|
||||
</td>
|
||||
<td>
|
||||
<a th:href="@{/addreview(userid=${userid}, bookid=${book.id}, title=${book.title})}">增加评论</a>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a href="/addbook">添加书籍</a>
|
||||
<a th:href="@{/borrowrecords(userid=${userid})}">借阅书籍</a>
|
||||
</body>
|
||||
</html>
|
|
@ -7,7 +7,7 @@
|
|||
<body>
|
||||
<h1>更新书籍</h1>
|
||||
<a th:href="@{/books}">返回</a>
|
||||
<div th:text="${book.id}"></div>
|
||||
<h3 th:text="${book.id}"></h3>
|
||||
<form th:action="@{/updatebookcommit}" method="post">
|
||||
<input type="text" name="id" hidden th:value="${book.id}" placeholder="请输入id"/>
|
||||
<input type="text" name="title" th:value="${book.title}" placeholder="请输入书名"/><br>
|
||||
|
|
Loading…
Reference in New Issue