俗话说:"好记性不如烂笔头"。本人学习Hibernate也有一个星期了,对Hibernate也有一个初步的了解。下面对Hibernate显示数据做个笔记,使用租房系统的Hibernate+jsp+selvect。

第一步:编写房屋实体类

/*
* 房屋实体类
*/ public class House {
private int id;//房屋id
private String title;//标题
private String description;//描述
private String fdate;//日期
private String price;//价格
private String contact;//面积 //省略get和set方法
}

第二步:配置House.hbm.xml映射

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="entity"> <class name="House" table="House">
<id name="id">
<generator class="increment"/>
</id> <property name="title" />
<property name="description" />
<property name="fdate" />
<property name="price" />
<property name="contact" /> </class> </hibernate-mapping>

第三步:配置hibernate.cfg.xml数据库映射(别忘了导入hibernate必备的架包)

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory name="foo">
<!-- 数据库方言 -->
<property name="dialect">
org.hibernate.dialect.OracleDialect
</property>
<!-- 连接数据库Url -->
<property name="hibernate.connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<!-- 连接驱动 -->
<property name="hibernate.connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<!-- 用户名 -->
<property name="hibernate.connection.username">epet</property>
<!-- 密码 -->
<property name="hibernate.connection.password">123456</property> <!-- 在控制台打印sql信息 -->
<property name="show_sql">true</property>
<!-- 创建表结构 -->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置映射信息 --> <mapping resource="entity/House.hbm.xml" /> </session-factory>
</hibernate-configuration>

第四步:编写dao层和daoImpl层

/*
* 查询所有房屋
*/
public interface HouseDao {
/*
* 查询所有房屋
*/
public List<House> selecthouse(); }
public class HouseDaoImpl implements HouseDao{

/*
* 查询所有房屋
*
* (non-Javadoc)
* @see Dao.HouseDao#selecthouse()
*/
public List<House> selecthouse() {
// TODO Auto-generated method stub
Session session = HibernateUtil.getSession(); //查询房屋实体类
String hql="from House";
Query q=session.createQuery(hql); List<House> list = q.list(); return list; }
}
注意:Hibernate查询的实体,而不是数据库表

第五步:编写Selvect和web.xml配置

package selvect;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import entity.House; import Biz.HouseBiz;
import Biz.Impl.HouseBizImpl; public class SelectAllServlet extends HttpServlet { /**
* Destruction of the servlet. <br>
*/
public void destroy() {
System.out.println("销毁select");
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
// response.setContentType("text/html");
// PrintWriter out = response.getWriter();
// out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
// out.println("<HTML>");
// out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
// out.println(" <BODY>");
// out.print(" This is ");
// out.print(this.getClass());
// out.println(", using the GET method");
// out.println(" </BODY>");
// out.println("</HTML>");
// out.flush();
// out.close();
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { HouseBiz mb=new HouseBizImpl(); List<House> li=mb.selecthouse();
request.getSession().setAttribute("li", li);
response.sendRedirect("list.jsp"); //request.getRequestDispatcher("index.jsp").forward(request, response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
System.out.println("初始化servlet");
} }
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!--查询房屋-->
<servlet>
<servlet-name>SelectAllServlet</servlet-name>
<servlet-class>selvect.SelectAllServlet</servlet-class>
</servlet> <!-- 映射servlet -->
<servlet-mapping>
<servlet-name>SelectAllServlet</servlet-name>
<url-pattern>/SelectAllServlet</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

在jsp页面显示

        <LI class=bold>房屋信息</LI>
<c:forEach var="mind" items="${sessionScope.li}">
<TR>
<TD class=house-thumb><span><A href="details.htm" target="_blank"><img src="data:images/thumb_house.gif" width="100" height="75" alt=""></a></span></TD>
<TD> <DL>
<!--标题,价格-->
<DT><A href="houseid?id=${mind.id}" target="_blank">${mind.title}</A></DT> <TD class=house-price><SPAN>${mind.price}</SPAN>元/月</TD></TR> </c:forEach>

Java_Web三大框架之Hibernate+jsp+selvect+HQL查询数据的更多相关文章

  1. Java_Web三大框架之Hibernate+jsp+selvect+HQL注册用户

    Hibernate比SQL语句简单多了,代码冗余少,切方便简洁明了.下面用Hibernate+jsp+selvect+HQL来实现注册用户. 第一步:编写用户实体类和Users2.hbm.xml映射. ...

  2. Java_Web三大框架之Hibernate+jsp+selvect+HQL登入验证

    刚开始接触Hibernate有些举手无措,觉得配置信息太多.经过一个星期的适应,Hibernate比sql简单方便多了.下面做一下Hibernate+jsp+selvect+HQL登入验证. 第一步: ...

  3. Java_Web三大框架之Hibernate+jsp+HQL分页查询

    分页查询无处不在.使用Hibernate+jsp+HQL进行分页查询. 第一步:编写房屋实体类和House.hbm.xml映射. /* * 房屋实体类 */ public class House { ...

  4. Java_Web三大框架之Hibernate+HQL语言基础

    12.1 HQL语言基础Hibernate查询语言为HQL(Hibernate Query Language),可以直接使用实体类名及属性.HQL语法类似于SQL,有SQL的关键词如select.fr ...

  5. Java_Web三大框架之Hibernate增删改查

    下面介绍一下Hibernate的增删改查. 第一步:编写用户实体类以及User.hbm.xml映射 package com.msl.entity; public class User { privat ...

  6. Java_Web三大框架之Hibernate 入门(一)

    一.Hibernate简介: Hibernate作者——Gavin King Hibernate创始人 < Hibernate in action >作者 EJB 3.0的Entity b ...

  7. Java_Web三大框架之Hibernate操作数据库(三)

    使用Hibernate操作数据库需要七个步骤: (1)读取并解析配置文件 Configuration conf = newConfiguration().configure(); (2)读取并解析映射 ...

  8. Java_Web三大框架之Hibernate配置文件(二)

    下面介绍一下编写Hibernate的配置文件,使用Hibernate操作数据库. 开始部署:下载需要的jar包               下载Hibernate           Hibernat ...

  9. Java三大框架之——Hibernate中的三种数据持久状态和缓存机制

    Hibernate中的三种状态   瞬时状态:刚创建的对象还没有被Session持久化.缓存中不存在这个对象的数据并且数据库中没有这个对象对应的数据为瞬时状态这个时候是没有OID. 持久状态:对象经过 ...

随机推荐

  1. 理解 LARGE_INTEGER的定义

    http://bbs.csdn.net/topics/310239341 #if defined(MIDL_PASS) typedef struct _LARGE_INTEGER { #else // ...

  2. 5-46 新浪微博热门话题 (30分)——unfinished HASH

    5-46 新浪微博热门话题   (30分) 新浪微博可以在发言中嵌入“话题”,即将发言中的话题文字写在一对“#”之间,就可以生成话题链接,点击链接可以看到有多少人在跟自己讨论相同或者相似的话题.新浪微 ...

  3. [poj2505]A multiplication game_博弈论

    A mutiplication game poj-2505 题目大意:给定一个数n和p,两个选手每次可以将p乘上[2,9].最先使得p大于n的选手胜利. 注释:$1\le n\le 429496729 ...

  4. Ubuntu 16.04安装Adobe AIR

    安装: wget -O adobe-air.sh http://drive.noobslab.com/data/apps/AdobeAir/adobe-air.sh chmod +x adobe-ai ...

  5. 15、Java并发性和多线程-线程通讯

    以下内容转自http://ifeve.com/thread-signaling/: 线程通信的目标是使线程间能够互相发送信号.另一方面,线程通信使线程能够等待其他线程的信号. 例如,线程B可以等待线程 ...

  6. MySQL语句给字段值加1

    update tbl_moment_like set like_count = like_count + #{addLikes} where mid = #{mid}

  7. zookeeper+kafka配置

    ZooKeeper 安装 #将ZooKeeper解压到/usr/local中 tar –zxvf zookeeper-3.4.6.tar.gz –C /usr/local cd /usr/local ...

  8. C++学习之多重继承与虚继承

    一.多重继承 我们知道,在单继承中,派生类的对象中包含了基类部分 和 派生类自定义部分.同样的,在多重继承(multiple inheritance)关系中,派生类的对象包含了每个基类的子对象和自定义 ...

  9. 负载均衡算法,轮询方式 大话设计模式之工厂模式 C#

    负载均衡算法,轮询方式 2018-04-13 17:37 by 天才卧龙, 13 阅读, 0 评论, 收藏, 编辑 学无止境,精益求精 十年河东,十年河西,莫欺少年穷 学历代表你的过去,能力代表你的现 ...

  10. 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取

    装饰者模式的学习(c#) 案例转自https://www.cnblogs.com/stonefeng/p/5679638.html //主体基类 using System;using System.C ...