近期为自己的项目前端用layui框架翻新一下,改改前后端的代码(之前的页面是用jsp标签动态显示数据的,而且页面数据是modelAndView带的数据,前后端交互没有XHR的,个人觉得不好就换了),在页面显示时间数据的时候突然给我来了一串数字,比如——1585584000000,我记得之前也是在做自己这个项目,做的一个功能获取的时间数据直接是字符串的日期的,我找到了,继续按此法,很快解决了问题。
实体类
user:
注意,要演示的属性是ruzhuTime和outTime,这两个都是java.util.Date类型的!
package com.myhomes.entity;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class User {
private Integer id;
private String acc;
private String password;
private String name;
private Integer userClass;
private String houseId;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date ruzhuTime;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date outTime;
private String ban;
private String isDelete;
private UserClass userClassName;
public UserClass getUserClassName() {
return userClassName;
}
public void setUserClassName(UserClass userClassName) {
this.userClassName = userClassName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getIsDelete() {
return isDelete;
}
public void setIsDelete(String isDelete) {
this.isDelete = isDelete;
}
public String getAcc() {
return acc;
}
public void setAcc(String acc) {
this.acc = acc;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getUserClass() {
return userClass;
}
public void setUserClass(Integer userClass) {
this.userClass = userClass;
}
public String getHouseId() {
return houseId;
}
public void setHouseId(String houseId) {
this.houseId = houseId;
}
public Date getRuzhuTime() {
return ruzhuTime;
}
public void setRuzhuTime(Date ruzhuTime) {
this.ruzhuTime = ruzhuTime;
}
public Date getOutTime() {
return outTime;
}
public void setOutTime(Date outTime) {
this.outTime = outTime;
}
public String getBan() {
return ban;
}
public void setBan(String ban) {
this.ban = ban;
}
}
dao接口方法映射.xml
继续注意看resultMap下的:
<result property="ruzhuTime" column="ruzhu_time" javaType="java.sql.Date"></result>
<result property="outTime" column="out_time" javaType="java.util.Date"></result>
一个是javaType="java.sql.Date";另一个是javaType="java.util.Date"。
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.myhomes.dao.UserDao">
<resultMap id="user" type="User">
<id property="id" column="id" javaType="Integer"></id>
<result property="acc" column="accound" javaType="String"></result>
<result property="password" column="password" javaType="String"></result>
<result property="name" column="name" javaType="String"></result>
<result property="userClass" column="user_class" javaType="Integer"></result>
<result property="houseId" column="house_id" javaType="String"></result>
<result property="ruzhuTime" column="ruzhu_time" javaType="java.sql.Date"></result>
<result property="outTime" column="out_time" javaType="java.util.Date"></result>
<result property="ban" column="ban" javaType="String"></result>
<result property="isDelete" column="is_delete" javaType="String"></result>
<association property="userClassName" column="user_class" javaType="UserClass" select="com.myhomes.dao.UserClassDao.selectById">
</association>
</resultMap>
<select id="selectByUsers" resultMap="user">
select * from users where 1=1
<if test="userName != '' and userName != null and userName != 'null'">
and users.name like concat('%',#{userName},'%')
</if>
<if test="userClass != null ">
and user_class = #{userClass}
</if>
<if test="houseId != '' and houseId != null and houseId != 'null'">
and house_id = #{houseId}
</if>
and is_delete = '0'
</select>
</mapper>
数据库表
两个是datetime类型
控制器层(获取数据并返回)
控制器层没有进行转换类型
package com.myhomes.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myhomes.biz.HouseBiz;
import com.myhomes.biz.UserClassBiz;
import com.myhomes.biz.UserService;
import com.myhomes.entity.House;
import com.myhomes.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller("userController")
@RequestMapping(value = "/user")
public class UserController {
Map<String,Object> result = new HashMap<>();
@Autowired
private UserService userService;
@Autowired
private HouseBiz houseBiz;
@Autowired
private UserClassBiz userClassBiz;
@RequestMapping(value = "/toUsers")
public ModelAndView toUsers(){
ModelAndView modelAndView = new ModelAndView();
List<House> houseList = houseBiz.searchAllHouse();
modelAndView.addObject("houseList",houseList);
modelAndView.setViewName("users_list");
return modelAndView;
}
@RequestMapping(value = "/listAll")
@ResponseBody
public Map<String,Object> listAll(@RequestParam(name = "pageNo",defaultValue = "1") int pageNo,@RequestParam(name = "pageSize",defaultValue = "15") int pageSize,
@RequestParam(name = "userName",defaultValue = "") String userName,@RequestParam(name = "userClass",defaultValue = "") Integer userClass,
@RequestParam(name = "houseId",defaultValue = "") String houseId){
//设置分页参数
PageHelper.startPage(pageNo,pageSize);
//查询数据
List<User> list = userService.searchByUsers(userName,userClass,houseId);
//使用PageInfo封装查询结果
PageInfo<User> pageInfo = new PageInfo<User>(list);
//总数页
Integer pages = pageInfo.getPages();
//当前页
Integer pageNum = pageInfo.getPageNum();
//当前页数据列表
List<User> userList = pageInfo.getList();
result.put("userList",userList);
result.put("pages",pages);
result.put("pageNums",pageNum);
return result;
}
}
页面文件
页面没有进行转换类型
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="/layui/css/layui.css"/>
<script src="/js/jquery-1.8.3.min.js"></script>
<script src="/layui/layui.js"></script>
<script src="/js/users.js"></script>
<title></title>
<style>
.layui-table {
width: 70%;
background-color: #fff;
color: #666;
}
</style>
</head>
<body class="layui-layout-body" style="background-color: white">
<div>
<br/>
<fieldset class="layui-elem-field">
<legend><a>系统用户信息表</a></legend>
<div class="layui-field-box">
<hr class="layui-bg-blue">
<div style="padding-left: 30px;">
<div>
<form class="layui-form">
<div class="layui-form-item">
<label class="layui-form-label" style="font-size: 17px;width: auto;">用户姓名:</label>
<div class="layui-input-inline" style="">
<input id="users_name" type="text" name="name" maxlength="20" class="layui-input" style=""/>
</div>
<label class="layui-form-label" style="font-size: 17px;width: auto;">用户类型:</label>
<div class="layui-input-inline">
<select id="users_class">
<option value="">用户类型</option>
<option value="0">无</option>
<option value="1">超级管理员</option>
<option value="2">房东管理员</option>
<option value="3">现任租客</option>
<option value="4">前任租客</option>
</select>
</div>
<label class="layui-form-label" style="font-size: 17px;width: auto;">房号:</label>
<div class="layui-input-inline">
<select id="users_houseId">
<option value="">选择房号</option>
<c:forEach items="${houseList}" var="hlist">
<option value="${hlist.houseId}"> ${hlist.houseId}</option>
</c:forEach>
</select>
</div>
<input id="search_button" type="button" value="搜索" class="layui-btn" style="margin-left: 10px;" onclick="search()"/>
<input id="insert_button" type="button" value="添加用户" class="layui-btn layui-btn-normal" style="margin-left: 50px;"/>
</div>
</form>
</div>
</div>
<div><a hidden id="usersTable_pageNum"></a>
<table class="layui-table" id="usersTableId">
<colgroup>
<col hidden>
<col width="100">
<col width="100">
<col width="70">
<col width="55">
<col width="75">
<col width="75">
<col width="90">
</colgroup>
<thead>
<tr>
<th hidden>id</th>
<th style="text-align:center">用户姓名</th>
<th style="text-align:center">登录账号</th>
<th style="text-align:center">用户类型</th>
<th style="text-align:center">房间号</th>
<th style="text-align:center">入住时间</th>
<th style="text-align:center">搬出时间</th>
<th style="text-align:center">操作</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div id="pageId"></div>
</div>
</div>
</fieldset>
</div>
</html>
js/JQuery
没有进行数据转换
layui.use('form', function(){
var $ = layui.jquery,
form = layui.form;
});
layui.use('table', function(){
var table = layui.table;
});
/**
* 分页
*/
$(document).ready(function () {
search();
});
function search() {
//获取查找条件
var userName = $("#users_name").val();
var pageNum = $("#usersTable_pageNum").text();
if (pageNum === '' || pageNum === null || pageNum === 'null'){
pageNum = 1;
}
var userClass = $("#users_class").val();
var houseId = $("#users_houseId").val();
$.ajax({
type:"get",
url:"/user/listAll?pageNo="+pageNum+"&userName="+userName+"&userClass="+userClass+"&houseId="+houseId,
success:function (data) {
//设置当前第几页
$("#usersTable_pageNum").text(data.pageNums);
//获取的表数据
var userList = data.userList;
if (userList.length > 0) {
var htmls = "";
for (var i = 0;i < userList.length;i++){
htmls+= "<tr>";
htmls+="<td hidden id='usersId"+i+"'>"+userList[i].id+"</td>";
htmls+="<td style='text-align:center' id='usersName"+i+"'>"+userList[i].name+"</td>";
htmls+="<td style='text-align:center' id='usersAcc"+i+"'>"+userList[i].acc+"</td>";
htmls+="<td style='text-align:center' id='usersClass"+i+"'>"+userList[i].userClassName.className+"</td>";
htmls+="<td style='text-align:center' id='usersHouseId"+i+"'>"+userList[i].houseId+"</td>";
if (userList[i].ruzhuTime){
htmls+="<td style='text-align:center' id='usersRuzhuTime"+i+"'>"+userList[i].ruzhuTime+"</td>";
}else {
htmls+="<td style='text-align:center' id='usersRuzhuTime"+i+"'>-</td>";
}
if (userList[i].outTime){
htmls+="<td style='text-align:center' id='usersOutTime"+i+"'>"+userList[i].outTime+"</td>";
} else {
htmls+="<td style='text-align:center' id='usersOutTime"+i+"'>-</td>";
}
htmls+="<td style='text-align:center'><a id='usersEdit"+i+"' style='color: #1E9FFF'>编辑</a></td>";
htmls+="</tr>";
}
}else {
htmls ="<tr><td colspan='8' style='text-align:center'>暂无数据</td></tr>";
}
$("tbody").html(htmls);
},
error:function () {
layer.alert('系统出错',{
title:"提示"
});
}
});
}
实验开始
运行tomcat启动项目
页面:
目标页面:
一个菜鸟表示很奇怪,因为映射文件的resultMap的javaType设置成java.sql.Date就能直接将数据库字段的类型转成String了。
查看XHR:
我现在把搬出时间的resultMap的那个类型转换下
更新项目数据重启tomcat清除缓存后
没毛病
XHR: