本案例实现了一个书籍续借的功能,使用了Spring框架、MyBatis框架和Thymeleaf模板解析器。

首先创建一个Book类,包含书籍的id、名称和借阅状态:

public class Book {
    private int id;
    private String name;
    private boolean borrowed;
    
    // getters and setters
}

接着,创建一个BookMapper接口,定义查询所有书籍和更新书籍借阅状态的方法:

public interface BookMapper {
    List<Book> getAllBooks();
    void updateBookBorrowStatus(int id, boolean borrowed);
}

然后,在resources目录下创建一个mybatis-config.xml文件,配置MyBatis的数据源和mapper文件:

<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/library" />
                <property name="username" value="root" />
                <property name="password" value="password" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="BookMapper.xml" />
    </mappers>
</configuration>

在同级目录下创建一个BookMapper.xml文件,定义SQL语句:

<mapper namespace="com.example.mapper.BookMapper">
    <select id="getAllBooks" resultType="com.example.model.Book">
        SELECT * FROM books
    </select>
    <update id="updateBookBorrowStatus">
        UPDATE books SET borrowed=#{borrowed} WHERE id=#{id}
    </update>
</mapper>

接下来,创建一个BookService类,用于与数据库交互:

@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;
    
    public List<Book> getAllBooks() {
        return bookMapper.getAllBooks();
    }
    
    public void updateBookBorrowStatus(int id, boolean borrowed) {
        bookMapper.updateBookBorrowStatus(id, borrowed);
    }
}

最后,创建一个Controller类,处理网页请求:

@Controller
public class BookController {
    @Autowired
    private BookService bookService;
    
    @RequestMapping("/")
    public String index(Model model) {
        List<Book> books = bookService.getAllBooks();
        model.addAttribute("books", books);
        return "index";
    }
    
    @RequestMapping(value = "/borrow", method = RequestMethod.POST)
    public String borrowBook(@RequestParam("id") int id, Model model) {
        bookService.updateBookBorrowStatus(id, true);
        return "redirect:/";
    }
    
    @RequestMapping(value = "/return", method = RequestMethod.POST)
    public String returnBook(@RequestParam("id") int id, Model model) {
        bookService.updateBookBorrowStatus(id, false);
        return "redirect:/";
    }
}

在resources/templates目录下创建一个index.html文件,使用Thymeleaf模板解析器渲染网页:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Library</title>
</head>
<body>
    <h1>Library</h1>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="book : ${books}">
                <td th:text="${book.id}"></td>
                <td th:text="${book.name}"></td>
                <td th:text="${book.borrowed ? 'Borrowed' : 'Available'}"></td>
                <td>
                    <form th:action="@{/borrow}" method="post">
                        <input type="hidden" name="id" th:value="${book.id}" />
                        <button type="submit" th:disabled="${book.borrowed}" th:text="${book.borrowed ? 'Borrowed' : 'Borrow'}"></button>
                    </form>
                    <form th:action="@{/return}" method="post">
                        <input type="hidden" name="id" th:value="${book.id}" />
                        <button type="submit" th:disabled="${!book.borrowed}" th:text="${!book.borrowed ? 'Available' : 'Return'}"></button>
                    </form>
                </td>
            </tr>
        </tbody>
    </table>
</body>
</html>

这个网页包含一个表格,显示所有书籍的id、名称、借阅状态和操作按钮。当用户点击借阅按钮或归还按钮时,会提交一个表单,调用Controller的borrowBook或returnBook方法更新数据库。

至此,书籍续借功能的实现就完成了

使用Spring mybatis thymeleaf模板解析实现书籍续借的案例

原文地址: https://www.cveoy.top/t/topic/gHTZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录