将查询结果打印到jsp表java spring mvc jsp

yptwkmov  于 2021-06-19  发布在  Mysql
关注(0)|答案(2)|浏览(278)

我需要打印到表的人谁超过55岁,但它打印每个人从数据库。我不知道怎么了。

@Override
    @SuppressWarnings("unchecked")
    public List<Book> old1() {
        Session session = this.sessionFactory.getCurrentSession();
//        List<Book> bookListt = session.createQuery("SELECT BOOK_TITLE, BOOK_AUTHOR, birthday FROM bookmanager.books WHERE birthday <= DATE_SUB(CURDATE(), INTERVAL 15 year)").list();
        List<Book> bookList1 = session.createSQLQuery("SELECT ID, BOOK_TITLE, BOOK_AUTHOR, born FROM bookmanager.book  WHERE born <= DATE_SUB(CURDATE(), INTERVAL 10 year))").list();

        return bookList1;
    }

控制器:

@RequestMapping(value = "books", method = RequestMethod.GET)
    public String old(Model model){
        model.addAttribute("book", new Book());
        model.addAttribute("old", this.bookService.listBooks());

        return "books";
    }

我试着把那个人印出来,但这是错的。从db打印所有peson
我该怎么修?

<c:if test="${!empty old1}">
    <table class="tg">
        <tr>
            <th width="80">ID</th>
            <th width="120">Title</th>
            <th width="120">Author</th>
            <th width="120">Price</th>
            <th width="120">born</th>
            <th width="60">Edit</th>
            <th width="60">Delete</th>
        </tr>
        <c:forEach items="${old1}" var="book1">
            <tr>
                <td>${book1.id}</td>
                <td><a href="/bookdata/${book1.id}" target="_blank">${book.bookTitle}</a></td>
                <td>${book1.bookAuthor}</td>
                <td>${book1.price}</td>
                <td>${book1.born}</td>
                <td><a href="<c:url value='/edit/${book1.id}'/>">Edit</a></td>
                <td><a href="<c:url value='/remove/${book1.id}'/>">Delete</a></td>
            </tr>
        </c:forEach>
    </table>
</c:if>
yfwxisqw

yfwxisqw1#

我不确定您的hibernate/java代码是否完全正确,但是您的mysql查询看起来不正确。如果你想把所有 born 字段从当前日期起超过55年,则使用此字段:

SELECT id, bookTitle, bookAuthor, birthday
FROM Book
WHERE born <= DATE_SUB(CURDATE(), INTERVAL 55 year)
ORDER BY born;
6jygbczu

6jygbczu2#

将查询更改为条件。本机sql查询未工作。

public List<Book> listBooks() {
        Session session = this.sessionFactory.getCurrentSession();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 55);

        List<Book> bookList = session.createCriteria(Book.class).add(Restrictions.le("born", calendar.getTime())).list();

        return bookList;
    }

相关问题