国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

Java_Web三大框架之Hibernate+jsp+selvect+HQL查詢數(shù)據(jù)

2019-11-15 01:00:43
字體:
來源:轉載
供稿:網(wǎng)友
java_Web三大框架之Hibernate+jsp+selvect+HQL查詢數(shù)據(jù)

俗話說:"好記性不如爛筆頭"。本人學習Hibernate也有一個星期了,對Hibernate也有一個初步的了解。下面對Hibernate顯示數(shù)據(jù)做個筆記,使用租房系統(tǒng)的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數(shù)據(jù)庫映射(別忘了導入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">        <!-- 數(shù)據(jù)庫方言 -->        <property name="dialect">            org.hibernate.dialect.OracleDialect        </property>        <!-- 連接數(shù)據(jù)庫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>        <!-- 創(chuàng)建表結構 -->        <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查詢的實體,而不是數(shù)據(jù)庫表

第五步:編寫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>


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 剑河县| 平湖市| 许昌市| 商丘市| 萍乡市| 汾阳市| 仁化县| 永仁县| 乌苏市| 乌拉特后旗| 金寨县| 宜春市| 西和县| 江津市| 辽宁省| 武山县| 田东县| 新竹市| 保康县| 洛浦县| 五家渠市| 嘉兴市| 大英县| 夏河县| 连山| 成安县| 南平市| 来安县| 荥阳市| 洞头县| 连南| 且末县| 吴忠市| 县级市| 高台县| 华容县| 福贡县| 囊谦县| 响水县| 余姚市| 扎兰屯市|