启程网络(新简约软件开发工作室)
email hbqcwl@sina.cn

第10课 从数据库读取并显示留言 2021年10月28日 17:33     阅读(184)

本课主要讲解使用 select 语法查询数据,并显示到前面页面中。

第1步,了解select 语法


我们先打开一个查询界面




然后输入以下语句:
Select id,title,content,author,add_time from liuyanban
如下图所示:



在“结果1“中能看到数据库中的数据。

Select  字段1、字段2、字段3 …  from   表名

这是查询表中所有数据,显示指定的字段,如果要显示所有字段,可以把所有字段都写上,也可以用 * 代替,例如:select * from 表名,这个就是查询所有数据,显示所有字段信息。

理解 select 语法后,我们开始配置mybatis 中的查询

第2步,在 LiuyanbanMapper.java添加一个查询方法

要查询数据,需要有一个查询的接口方法,先在 LiuyanbanMapper.java 文件中添加一个方法,返回留言数据集合,如下图所示:


添加方法代码如下:

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();
}

第3步,在 LiuyanbanMapper.xml 中配置查询的 xml

打开 liuyanbanMapper.xml ,添加查询用的xml配置,如下图所示:



具体代码如下:

	<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>


resultMap 是把数据表的字段和 type 的这个类(cn.qcwl.model.Liuyanban)进行绑定,比如第一个 <result column="id" property="id" jdbcType="INTEGER"/> 是把 id 字段和 Liuyanban 类的 id 属性绑定上,数据表中查询的id字段值赋给Liuyanban这个类对像的id属性。

Select 这个是 mybatis 查询的配置,其中的 resultMap 是查询出来的数据如何返回绑定。


第4步,在 controller 中进行查询数据


Mybatis 配置完成后,需要查询数据,添加代码,如下图所示:



1. 添加一个 model 参数
2. 定义接收从数据库中查询的数据变量
3. 通过 model 把数据传到 jsp 页面中

具体代码如下:

	@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";
	}

第5步,修改 jsp 页面,显示数据

1. 先添加标签库引用
由于我们使用的是 jstl 标签库,这个是对jsp方便操作的一个库。添加后如下图所示:



具体代码如下:

<%--  添加标签库  --%>
<%@ 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" %>

2. 添加forEach标签
有了标签库后,需要用 forEach 标签循环这些数据进行显示。如下图所示:



具体代码如下:

	 <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>


看一下 forEeach 的说明:
<c:forEach  items=”后台通过model传递过来的数据“ var=”取出来的每一个对像”>
而使用 ${item.title} 显示取出来的对像的title 值,同样对于其他值也是这样进行获取

Fmt:formatDate 这个是对日期进行格式化,可以指定把日期按什么格式输入,上面是根据 
yyyy  是4位年,例如:2021 年 
MM   是2位月,例如:02 月
dd     是2位日,例如:03 日
HH:mm:ss   24小时进制的小时、分、秒

第6步,运行程序查看效果

运行程序后,如下图所示:



再来和数据库对比一下,如下图所示:



数据完全一样。本节课已完成从数据库读取并显示了,下节课将学习如何修改留言内容


代码下载地址

百度网盘地址:
链接:https://pan.baidu.com/s/1NBMDt_22u6wJSytguZkQ-Q 
提取码:d1fp 


原创文章,转载请注明本文链接地址(违者必究): 第10课 从数据库读取并显示留言

打赏作者很喜欢这篇文章,打赏犒劳下作者,以此激励作者创作更多
微信打赏
支付宝打赏