ubuntu服务器 struts_hibernate搭配
hibernate为数据库部分。struts是控制部分。
hibernate数据库部分
1、本程序的执行顺序。

2、数据库的创建和修改参考我的经验分享。 数据库名:db_hero表名:produt_字段:如图


3、新建类,Product,对应数据库的三个字段。 package com.how2java.pojo;public class Product { private int id; private String name; private float price; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } }

4、新建product_表。对应Product类。 <?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.how2java.pojo"> <class name="Product" table="product_"> <id name="id" column="id"> <generator class="native"> </generator> </id> <property name="name" /> <property name="price" /> </class> </hibernate-mapping>

5、新建hibernate.cfj.xml, 指定链接信息,数据库,账号,密码。 <?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/db_hero?characterEncoding=GBK</property> <property name="connection.username">root</property> <property name="connection.password">admin</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="current_session_context_class">thread</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="com/how2java/pojo/Product.hbm.xml" /> </session-factory></hibernate-configuration>

6、新建ProductDAO类,类的增献皲咝犴删查改工作。package com.how2java.dao;i罪焐芡拂mport java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.how2java.pojo.Product;public class ProductDAO { public void add(Product p) { List<Product> result = new ArrayList(); SessionFactory sf = new Configuration().configure() .buildSessionFactory(); Session s = sf.openSession(); s.beginTransaction(); s.save(p); s.getTransaction().commit(); s.close(); sf.close(); } public Product get(int id) { Product result = null; SessionFactory sf = new Configuration().configure() .buildSessionFactory(); Session s = sf.openSession(); result = (Product) s.get(Product.class, id); s.close(); sf.close(); return result; } public void delete(int id) { List<Product> result = new ArrayList(); SessionFactory sf = new Configuration().configure() .buildSessionFactory(); Session s = sf.openSession(); s.beginTransaction(); Product p = (Product) s.get(Product.class, id); s.delete(p); s.getTransaction().commit(); s.close(); sf.close(); } public void update(Product p) { List<Product> result = new ArrayList(); SessionFactory sf = new Configuration().configure() .buildSessionFactory(); Session s = sf.openSession(); s.beginTransaction(); s.update(p); s.getTransaction().commit(); s.close(); sf.close(); } public List<Product> listProduct() { List<Product> result = new ArrayList(); SessionFactory sf = new Configuration().configure() .buildSessionFactory(); Session s = sf.openSession(); Query q = s.createQuery("from Product p"); result = q.list(); s.close(); sf.close(); return result; }}

struts控制部分
1、首先执行web.xml,设置struts; <web-app> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <dispatcher>FORWARD</dispatcher> <dispatcher>REQUEST</dispatcher> <url-pattern>/*</url-pattern> </filter-mapping></web-app>

2、设置struts.xml的映射。 <?xml version="1.0" e荏鱿胫协ncoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <package name="basicstruts" extends="struts-default"> <action name="addProduct" class="com.how2java.action.ProductAction" method="add"> <result name="list" type="redirect">listProduct</result> </action> <action name="deleteProduct" class="com.how2java.action.ProductAction" method="delete"> <result name="list" type="redirect">listProduct</result> </action> <action name="editProduct" class="com.how2java.action.ProductAction" method="edit"> <result name="edit">/product/edit.jsp</result> </action> <action name="updateProduct" class="com.how2java.action.ProductAction" method="update"> <result name="list" type="redirect">listProduct</result> </action> <action name="listProduct" class="com.how2java.action.ProductAction" method="list"> <result name="listJsp">/product/list.jsp</result> </action> </package></struts>

3、编写类,对应struts的方法。 package com.how2java.action;import java.util.List;import com.how2java.dao.ProductDAO;import com.how2java.pojo.Product;public class ProductAction { ProductDAO pdao = new ProductDAO(); Product product; List<Product> products; public List<Product> getProducts() { return products; } public void setProducts(List<Product> products) { this.products = products; } public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } public String add() { pdao.add(product); return "list"; } public String edit() { product =pdao.get(product.getId()); return "edit"; } public String delete() { pdao.delete(product.getId()); return "list"; } public String update() { pdao.update(product); return "list"; } public String list() { products = pdao.listProduct(); return "listJsp"; }}

4、新建product/list.jsp,对应struts的返回页面<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8" isELIgnored="false"%><%@ taglib prefix="s" uri="/struts-tags"%><html><body><table align="center" border="1" cellspacing="0" width="500px"> <tr> <td>id</td> <td>name</td> <td>price</td> <td>edit</td> <td>delete</td> </tr> <s:iterator value="products" var="p"> <tr> <td>${p.id}</td> <td>${p.name}</td> <td>${p.price}</td> <td><a href="editProduct?product.id=${p.id}">edit</a></td> <td><a href="deleteProduct?product.id=${p.id}">delete</a></td> </tr> </s:iterator></table><br/><form action="addProduct" method="post"><table align="center" border="1" cellspacing="0"><tr> <td> name: </td> <td> <input type="text" name="product.name" value=""> </td></tr><tr> <td> price: </td> <td> <input type="text" name="product.price" value="0"> </td></tr><tr> <td colspan="2" align="center"> <input type="submit" value="submit"> </td></tr></table></form></body></html>

5、新建product/edit.jsp,对应struts的返回页面<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8" isELIgnored="false"%><%@ taglib prefix="s" uri="/struts-tags"%><html><body><form action="updateProduct" method="post"><table align="center" border="1" cellspacing="0"><tr> <td> name: </td> <td> <input type="text" name="product.name" value="${product.name}"> </td></tr><tr> <td> price: </td> <td> <input type="text" name="product.price" value="${product.price}"> </td></tr><tr> <td colspan="2" align="center"> <input type="hidden" name="product.id" value="${product.id}"> <input type="submit" value="submit"> </td></tr></table></form></body></html>

6、新建index.jsp,跳转查询页面

7、最后上传到网站,可以看到效果。 有增加,修改,删除,显示的功能。

