本课主要讲解使用 select 语法查询数据,并显示到前面页面中。
Select id,title,content,author,add_time from liuyanban
package cn.qcwl.dao;
import java.util.List;
import cn.qcwl.model.Liuyanban;
public interface LiuyanbanMapper {
public int insertLiuyanban(Liuyanban model);
/**
* 查询所有留言信息
* @return 保存留言的一个列表
*/
public List<Liuyanban> selectLiuyanban();
}
<resultMap id="lybResult" type="cn.qcwl.model.Liuyanban" >
<result column="id" property="id" jdbcType="INTEGER"/>
<result column="title" property="title" jdbcType="VARCHAR"/>
<result column="content" property="content" jdbcType="VARCHAR"/>
<result column="author" property="author" jdbcType="VARCHAR"/>
<result column="add_time" property="addTime" jdbcType="TIMESTAMP"/>
</resultMap>
<select id="selectLiuyanban" resultMap="lybResult">
select id,title,content,author,add_time from liuyanban
</select>
@RequestMapping("")
public String index(Model model) {
/*
// 1. 创建一个对像
Liuyanban model = new Liuyanban();
// 2. 给对像的属性赋值
model.setTitle("我想学编程");
model.setContent("听说编程是很高深的,好学吗?");
model.setAddTime(new Date()); // 以当前日期赋值
//id是自动增长类型 ,这里就不赋值了。
// 调用留言板页面会,mapper会自动给数据表 Liuyanban 添加一条记录
liuyanbanMapper.insertLiuyanban(model);
*/
List<Liuyanban> liuyanList = liuyanbanMapper.selectLiuyanban();
model.addAttribute("liuyanList", liuyanList);
return "index";
}
<%-- 添加标签库 --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<div class="row bg-primary">
<div class="col-sm-2"><p >标题</p></div>
<div class="col-sm-6"><p>内容</p></div>
<div class="col-sm-2"><p>留言人</p></div>
<div class="col-sm-2"><p>留言时间</p></div>
</div>
<c:forEach items="${liuyanList }" var="item">
<div class="row">
<div class="col-sm-2"><p>${item.title }</p></div>
<div class="col-sm-6"><p>${item.content }</p></div>
<div class="col-sm-2"><p>${item.author }</p></div>
<div class="col-sm-2">
<p>
<fmt:formatDate value="${item.addTime }" pattern="yyyy-MM-dd HH:mm:ss" />
</p>
</div>
</div>
</c:forEach>