gpt4 book ai didi

java - 自动将日期存储到数据库中

转载 作者:行者123 更新时间:2023-12-02 01:31:30 24 4
gpt4 key购买 nike

我想将文章存储在我的表中,我希望自动生成发布日期并将其存储在数据库中。每当我添加文章时,我的文章表中的 publishedDate 列都显示 NULL。我想要自动插入日期。我该怎么做?我的实体类和 JSP 文件中有以下代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page import="java.util.Date" %>
<jsp:useBean id="now" class="java.util.Date" />

<c:url value="/admin/article/save" var="url"></c:url>
<form:form class="form-horizontal" action="${url}" modelAttribute="article"
method="POST" enctype="multipart/form-data" style="margin-top:5rem;" >
<fieldset>

<form:hidden path="articleViews" value="${article.articleViews}" />
<form:hidden path="publishedDate" id="publishedDate" value="${now}" />
<form:hidden path="articleId" value="${article.articleId}" />
import java.io.Serializable;
import java.util.Comparator;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
import org.springframework.web.multipart.MultipartFile;

@Entity
@Table
public class Article implements Serializable,Comparator<Article>{


/**
*
*/
private static final long serialVersionUID = -4756552789390263788L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long articleId;
private String articleHeading;
@Column(columnDefinition = "TEXT")

private String articleIntroduction;
@Column(columnDefinition = "TEXT")

private String articleDescription;
@Column(columnDefinition = "TEXT")

private String articleSummary;
@Transient
private MultipartFile aImage;

private String articleCategory;

@Generated(GenerationTime.ALWAYS)
@Column(name="publishedDate" ,columnDefinition = "TIMESTAMP")
private Date publishedDate ;


public long getArticleViews() {
return articleViews;
}
public void setArticleViews(long articleViews) {
this.articleViews = articleViews;
}

@Column( nullable = false, columnDefinition = "bigint(20) default 0")
private long articleViews = 0;

最佳答案

this post 的重复,使用@PrePersist@PreUpdate
或者也可以组合 @CreationTimestamp@UpdateTimestamp

关于java - 自动将日期存储到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57553518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com