在项目中添加时间
工具类接口
public interface Tool {
static String dateTString(Date date){
return new SimpleDateFormat("yyyy-MM-dd").format(date);
}
}
实体类
1. get,set,无参构造,有参构造,tostring省略
2. 在时间的getset方法的基础上再改进一下,再加一个格式化的方法,xxxStr()。
public class Contract {
private Integer cid;
private Integer lid;
private Integer hid;
private Integer ccash;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date ccreatetime;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date cstarttime;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date cendtime;
public Date getCcreatetime() {
return ccreatetime;
}
public String getCcreatetimeStr() {
return Tool.dateTString(ccreatetime);
}
public void setCcreatetime(Date ccreatetime) {
this.ccreatetime = ccreatetime;
}
public Date getCstarttime() {
return cstarttime;
}
public String getCstarttimeStr() {
return Tool.dateTString(cstarttime);
}
public void setCstarttime(Date cstarttime) {
this.cstarttime = cstarttime;
}
public Date getCendtime() {
return cendtime;
}
public String getCendtimeStr() {
return Tool.dateTString(cendtime);
}
public void setCendtime(Date cendtime) {
this.cendtime = cendtime;
}
controller控制层
/**添加*/
@RequestMapping("/contractadd")
public String contractAddMethod(Contract contract){
System.out.println(contract);
int addMethod = contractService.contractAddMethod(contract);
return "redirect:/contract/contractall.action";
}
serviceImpl数据处理层
@Override
public int contractAddMethod(Contract contract) {
contract.setCcreatetime(new Date());
return contractMapper.insert(contract);
}
接口serviceMapper持久层
@Override
public interface ContractMapper {
int insertSelective(Contract record);
}
serviceMapper.xml
<insert id="insertSelective" parameterType="com.zhiyou100.entity.Contract" >
insert into contract
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="cid != null" >
cid,
</if>
<if test="lid != null" >
lid,
</if>
<if test="hid != null" >
hid,
</if>
<if test="ccash != null" >
ccash,
</if>
<if test="ccreatetime != null" >
ccreateTime,
</if>
<if test="cstarttime != null" >
cstartTime,
</if>
<if test="cendtime != null" >
cendTime,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="cid != null" >
#{cid,jdbcType=INTEGER},
</if>
<if test="lid != null" >
#{lid,jdbcType=INTEGER},
</if>
<if test="hid != null" >
#{hid,jdbcType=INTEGER},
</if>
<if test="ccash != null" >
#{ccash,jdbcType=INTEGER},
</if>
<if test="ccreatetime != null" >
#{ccreatetime,jdbcType=TIMESTAMP},
</if>
<if test="cstarttime != null" >
#{cstarttime,jdbcType=TIMESTAMP},
</if>
<if test="cendtime != null" >
#{cendtime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
前台页面一:add.jsp
<%--
Created by IntelliJ IDEA.
User: ASUS
Date: 2020/8/27
Time: 14:59
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>公寓管理系统 - 添加租赁合同</title>
<link rel="stylesheet" href="<c:url value="/css/main.css"/>">
<link rel="stylesheet" href="<c:url value="/font-awesome/css/font-awesome.css"/>">
</head>
<body>
<div class="box">
<h3>添加租赁合同</h3>
<form action="<c:url value="/contract/contractadd.action"/>" method="post">
<table class="form-table">
<tr>
<td>租户编号</td>
<td colspan="3" class="control">
<input type="text" name="lid" placeholder="租户编号">
</td>
</tr>
<tr>
<td>房屋编号</td>
<td colspan="3" class="control">
<input type="text" name="hid" placeholder="房屋编号">
</td>
</tr>
<tr>
<td>押金</td>
<td colspan="3" class="control">
<input type="text" name="ccash" placeholder="押金">
</td>
</tr>
<tr>
<td>开始时间</td>
<td colspan="3" class="control">
<input type="date" name="cstarttime" placeholder="开始时间">
</td>
</tr>
<tr>
<td>结束时间</td>
<td colspan="3" class="control">
<input type="date" name="cendtime" placeholder="结束时间">
</td>
</tr>
<tr>
<td>总金额</td>
<td colspan="3" class="control">
<input type="text" name="ctotalmoney" placeholder="总金额">
</td>
</tr>
<tr>
<td>状态</td>
<td colspan="3" class="control">
<select name="cstatus">
<option value = "1" selected>有效</option>
<option value = "2">失效</option>
</select>
</td>
</tr>
<tr>
<td>付款方式</td>
<td colspan="3" class="control">
<select name="ctype">
<option value = "1">天付</option>
<option value = "2">半月付</option>
<option value = "3"selected>月付</option>
<option value="4">年付</option>
</select>
</td>
</tr>
</table>
<div class="buttons">
<input class="btn btn-primary va-bottom" type="submit" value="保存">
<a class="btn btn-default" href="javascript:history.go(-1)">返回</a>
</div>
</form>
</div>
</body>
</html>
前台页面一:list.jsp
<%--
Created by IntelliJ IDEA.
User: ASUS
Date: 2020/8/27
Time: 14:59
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>公寓管理系统 - 房屋信息管理</title>
<link rel="stylesheet" href="<c:url value="/css/main.css"/> ">
<link rel="stylesheet" href="<c:url value="/font-awesome/css/font-awesome.css"/>">
</head>
</head>
<body>
<div class="box">
<h3>租赁合同管理</h3>
<div class="actions">
<div>
<a class="btn btn-primary" href="<c:url value="/view/contract/add.jsp"/>">添加租赁合同</a>
</div>
</div>
<table class="list">
<tr>
<th>序号</th>
<th>租户姓名</th>
<th>房屋地址</th>
<th>押金</th>
<th>签署时间</th>
<th>开始时间</th>
<th>结束时间</th>
<th>总金额</th>
<th>状态</th>
<th>付款方式</th>
<th>操作</th>
</tr>
<c:forEach items="${list.list}" var="i" >
<tr>
<td>${i.cid}</td>
<td>
<a href="<c:url value="/lessee/getOneByLid.action?lid=${i.lessee.lid}"/>">
${i.lessee.lname}
</a>
</td>
<td>
<a href="<c:url value="/house/getOneByHid.action?hid=${i.house.hid}"/>">
${i.house.haddress}
</a>
</td>
<td>${i.ccash}</td>
<%--ccreatetimeStr也可以--%>
<td>${i.ccreatetimeStr}</td>
<%--str也可以--%>
<td>${i.cstarttime.toLocaleString()}</td>
<td>${i.cendtime.toLocaleString()}</td>
<td>${i.ctotalmoney}</td>
<td>
<c:choose>
<c:when test="${i.cstatus==1 }">
有效
</c:when>
<c:otherwise>
无效
</c:otherwise>
</c:choose>
</td>
<td>${i.ctypeStr}</td>
<td>
<a class="fa fa-university" title="详情" href="<c:url value="/contract/getOneByCid.action?cid=${i.cid}"/>"></a>
<a class="fa fa-paint-brush" title="编辑" href="<c:url value="/contract/updateOneByCid.action?cid=${i.cid}"/>"></a>
<c:if test="${sessionScope.user.wrose>3}">
<a class="fa fa-trash" title="删除" href="<c:url value="/contract/deleteOneByCid.action?cid=${i.cid}"/>" οnclick="confirmDelete(1)"></a>
</c:if>
</td>
</tr>
</c:forEach>
</table>
<div class="pager-info">
<div>共有 ${list.count} 条记录,第 ${list.pageNumber}/${list.pageTotal} 页 </div>
<div>
<ul class="pagination">
<li class="paginate_button previous disabled }">
<a href="<c:url value="/contract/contractall.action?pageNumber=${list.pageNumber - 1}"/>">上一页</a>
</li>
<c:forEach var="i" begin="1" step="1" end="${list.pageTotal}">
<li class="paginate_button active"><a href="<c:url value="/contract/contractall.action?pageNumber=${i}"/>">${i}</a></li>
</c:forEach>
<li class="paginate_button next disabled">
<a href="<c:url value="/contract/contractall.action?pageNumber=${list.pageNumber+1}"/>">下一页</a>
</li>
</ul>
</div>
</div>
</div>
<script src="<c:url value="/bootstrap/js/jquery.js"/>"></script>
<script src="<c:url value="/bootstrap/js/bootstrap.js"/>"></script>
<script>
function confirmDelete(id){
if (confirm("确定要删除码?")) {
alert('发送删除请求,刷新页面(不要异步)');
}
return false;
}
</script>
</body>
</html>