数据库是Oracle 11g2 Express。
我将nls_date_format更改为DD/MM/RRRR
:
alter system set nls_date_format = "DD/MM/RRRR" scope = spfile;
然后我重新启动了数据库。
现在在我的spring-mvc项目中我创建了一个Bean:
@Entity
@Table(name = "HR.EMPLOYEES")
public class User {
@Id
@SequenceGenerator(name="EMPLOYEES_SEQ", sequenceName="EMPLOYEES_SEQ", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="EMPLOYEES_SEQ")
@Column(name = "EMPLOYEE_ID")
private int id;
@Formula(value="FIRST_NAME || ' ' || LAST_NAME")
private String username;
@Column(name = "FIRST_NAME")
private String firstname;
@Column(name = "LAST_NAME")
private String lastname;
private String email;
@Column(name="HIRE_DATE")
@Temporal(TemporalType.DATE)
private Date hireDate; // java.util.Date
private String job_id;
@Column(name = "SALARY")
private double salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
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;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public String getJob_id() {
return job_id;
}
public void setJob_id(String job_id) {
this.job_id = job_id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
这是DAO实施:
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);
}
}
这是控制器:
@Controller
public class HomeController {
@Autowired
private UserDAO userDao;
@RequestMapping("/")
public ModelAndView handleRequest() throws Exception { // to list employees
List<User> listUsers = userDao.list();
ModelAndView model = new ModelAndView("UserList");
model.addObject("userList", listUsers);
return model;
}
@RequestMapping(value = "/new", method = RequestMethod.GET)
public ModelAndView newUser() { // to add a new employee
ModelAndView model = new ModelAndView("UserForm");
model.addObject("user_details", new User());
model.addObject("titre", "Ajout");
return model;
}
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public ModelAndView editUser(HttpServletRequest request) { // to edit an existing employee
int userId = Integer.parseInt(request.getParameter("id"));
User user = userDao.get(userId);
ModelAndView model = new ModelAndView("UserForm");
model.addObject("user_details", user);
model.addObject("titre", "Modif");
return model;
}
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public ModelAndView deleteUser(HttpServletRequest request) { // to delete an existing employee
int userId = Integer.parseInt(request.getParameter("id"));
userDao.delete(userId);
return new ModelAndView("redirect:/");
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveUser(@ModelAttribute User user) { // to execute the dml of insert of update about an employee's data
userDao.saveOrUpdate(user);
return new ModelAndView("redirect:/");
}
}
以下是编辑员工的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 uri="http://www.springframework.org/tags/form" prefix="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_details">
<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> <%-- this is the date field --%>
</tr>
<tr>
<td>Job_id:</td>
<td><form:input path="job_id"/></td>
</tr>
<tr>
<td>Salary:</td>
<td><form:input path="salary"/></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>
在运行时,hire_date文本字段中显示的值具有格式RRRR-MM-DD
,并且唯一可以插入/更新到数据库中的格式为MM/DD/RRRR
!虽然spfile nls_date_format
已设置为DD/MM/RRRR
且数据库已重新启动!那么为什么spfile参数不起作用呢!
答案 0 :(得分:0)
我认为这不是数据库配置问题,而是Spring问题。 Spring使用Jackson序列化JSON,Jackson默认使用GMT时间戳格式来处理日期的序列化和反序列化,除非另有配置。
如果使用@Temporal
注释日期字段,Spring将以此格式显示日期: YYYY-MM-DD 。
您需要为日期字段创建自定义序列化程序以解决问题,并使用@JsonSerialize(using=JsonDateSerializer.class)
注释注释日期字段的get方法。
JsonDateSerializer 将是您的自定义序列化程序类。
在这个问题上看到我对所有程序的回答:
答案 1 :(得分:0)
我找到了一个更简单的解决方案here