数据库是Oracle 11g2 Express,默认模式为hr。
班级模型的代码:
@Entity
@Table(name = "HR.EMPLOYEES")
public class User {
private int id;
private String username;
private String firstname;
private String lastname;
private String email;
private Date hireDate; // java.util.Date
private String job_id;
@Id
@SequenceGenerator(name="EMPLOYEES_SEQ", sequenceName="EMPLOYEES_SEQ", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="EMPLOYEES_SEQ")
@Column(name = "EMPLOYEE_ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Formula(value="FIRST_NAME || ' ' || LAST_NAME")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "FIRST_NAME")
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
@Column(name = "LAST_NAME")
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name="HIRE_DATE")
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
@Column(name="JOB_ID")
public String getJob_id() {
return job_id;
}
public void setJob_id(String job_id) {
this.job_id = job_id;
}
}
类模型的实现:
public class UserDAOImpl implements UserDAO {
@Autowired
private SessionFactory sessionFactory;
public UserDAOImpl() {
}
public UserDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
@Transactional
public List<User> list() {
@SuppressWarnings("unchecked")
List<User> listUser = (List<User>) sessionFactory.getCurrentSession()
.createCriteria(User.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
return listUser;
}
@Override
@Transactional
public User get(int id) {
String hql = "from User where id=" + id;
Query query = sessionFactory.getCurrentSession().createQuery(hql);
@SuppressWarnings("unchecked")
List<User> listUser = (List<User>) query.list();
if (listUser != null && !listUser.isEmpty()) {
return listUser.get(0);
}
return null;
}
@Override
@Transactional
public void saveOrUpdate(User user) {
sessionFactory.getCurrentSession().saveOrUpdate(user);
}
@Override
@Transactional
public void delete(int id) {
User userToDelete = new User();
userToDelete.setId(id);
sessionFactory.getCurrentSession().delete(userToDelete);
}
}
JSP页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${titre}</title>
<style type="text/css">
a {
text-decoration: none;
}
</style>
</head>
<body>
<div align="center">
<h1>${titre}</h1>
<table>
<form:form action="save" method="post" modelAttribute="user">
<form:hidden path="id"/>
<tr>
<td>Firstname:</td>
<td><form:input path="firstname"/></td>
</tr>
<tr>
<td>Lastname:</td>
<td><form:input path="lastname"/></td>
</tr>
<tr>
<td>Email:</td>
<td><form:input path="email"/></td>
</tr>
<tr>
<td>Hiredate:</td>
<td><form:input path="hireDate"/></td>
</tr>
<tr>
<td>Job_id:</td>
<td><form:input path="job_id"/></td>
</tr>
<tr>
<td>
<input type="submit" value="Save">
</td>
<td><a href="<c:url value='/' />"><input type="button" value="Annuler" /></a></td>
</tr>
</form:form>
</table>
</div>
</body>
</html>
单击JSP页面中的“提交”按钮时,数据将插入到表中。但是日期栏从18/08/16更改为08/06/17!那有什么不对?
答案 0 :(得分:0)
将HTML表单传递到数据库时,日期始终难以使用。 在JDBC中,您应该尽可能多地使用java.util.Date对象,并检查数据库上的语言环境是否与服务器上的语言环境相同(从String转换为Date,反之亦然)以及配置的时区也是一样。
我不认为使用LocalDate可以帮助您进行数据库记录,因为我不确定数据库驱动程序(或数据库文件 - 因为您使用JPA,您应该使用Hibernate或类似的东西)将支持它。 / p>
有时,使用字符串来表示生日更简单......
此外,在您的代码中,您没有告诉我们您将字符串从输入表单转换为日期的位置,也许问题就在这里......
此致
卢瓦克
答案 1 :(得分:0)
尝试为@Entity类中的Date变量设置@DateTimeFormat(pattern =“dd.MM.yyyy”)(具有适合您的模式)。
实施例。
@DateTimeFormat(pattern="dd.MM.yyyy")
@Column(name = "auctionDate")
private LocalDate auctionDate;