Java_Web: Hibernate + jsp + selvect + HQL data query, java_webselvect
As the saying goes: "A good memory is worse than a bad pen ". I have been studying Hibernate for a week and have a preliminary understanding of Hibernate. Next, take a note of the Hibernate data and use the Hibernate + jsp + selvect of the rental system.
Step 1: Compile the Housing Entity
/** House entity class */public class House {private int id; // House id private String title; // The title private String description; // The description private String fdate; // Date private String price; // price private String contact; // area // The get and set methods are omitted}
Step 2: Configure House. hbm. xml ing
<?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">
Step 3: configure the hibernate. cfg. xml database ing (do not forget to import the required rack package for hibernate)
<! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
Step 4: Write the dao layer and daoImpl Layer
/** Query all houses */public interface HouseDao {/** query all houses */public List <House> selecthouse ();}
Public class HouseDaoImpl implements HouseDao {/** query all houses ** (non-Javadoc) * @ see Dao. houseDao # selecthouse () */public List <House> selecthouse () {// TODO Auto-generated method stub Session session = HibernateUtil. getSession (); // Query the House object class String hql = "from House"; Query q = session. createQuery (hql); List <House> list = q. list (); return list;} note: the entity queried by Hibernate, instead of the database table
Step 5: Compile Selvect and web. xml configuration
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 The servlet. <br> */public void destroy () {System. out. println ("Destroy 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 I F 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 ("initialize 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"> <! -- Query houses --> <servlet-name> SelectAllServlet </servlet-name> <servlet-class> selvect. SelectAllServlet </servlet-class> </servlet> <! -- Servlet ing 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>
Display on jsp page
<LI class = bold> House Information </LI> <c: forEach var = "mind" items = "$ {sessionScope. li} "> <TR> <TD class = house-thumb> <span> <A href =" details.htm "target =" _ blank "> </a> </span> </TD> <DL> <! -- Title, price --> <DT> <A href = "houseid? Id =$ {mind. id} "target =" _ blank ">$ {mind. title }</A> </DT> <TD class = house-price> <SPAN >$ {mind. price} </SPAN> RMB/month </TD> </TR> </c: forEach>