zoukankan      html  css  js  c++  java
  • Java_Web三大框架之Hibernate+jsp+selvect+HQL查询数据

    俗话说:"好记性不如烂笔头"。本人学习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="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>
  • 相关阅读:
    DEV GridView显示行号
    winfrom Log4Net 代码(二) 记录格式log_info.txt和log_error.txt,只产生两个文本,里面分别记录提示信息和报错信息
    VB.NET使用Log4Net
    Log4Net使用中loginfo.IsInfoEnabled=false问题解决方法
    VB.NET 根据当前日期获取星期几
    VB.NET 发送outLook邮件body基于Html样式
    VB.NET NPOI快速导入导出Excel
    python使用cx_oracle连接oracle数据库
    物理STANDBY库创建还原点(打开为READ WRITE后再变回STANDBY库)
    oracle RAC和RACOneNode之间的转换
  • 原文地址:https://www.cnblogs.com/wlx520/p/4692944.html
Copyright © 2011-2022 走看看