整合struts和hibernate的基本思路是将MVC中M层交给hibernate处理
在eclipse中创建struts_hibernate项目
本项目以Product的增删改查为例,整合Struts+Hibernate
整合步骤分 hibernate部分和struts部分
在eclipse中创建struts_hibernate项目
本项目以Product的增删改查为例,整合Struts+Hibernate
整合步骤分 hibernate部分和struts部分
- 思路图
- HIbernate部分
1.创建数据库how2java
create database how2java
2.pojo
Product有id,name,price3个字段
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; } }
3.Product.hbm.xml
映射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>
4.hibernate.cfg.xml
hibernate基本配置,指定数据库,账号,密码,方言等等
<?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/how2java?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>
5.dao
为ProductDAO准备增,删,修改,查询,获取方法
package com.how2java.dao; import 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; } }
- Struts2 部分
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
分别为增加,删除,获取,修改,查询配置Action
为了便于理解,这里没有使用通配符,可以查看struts章节的通配符匹配简化配置
<?xml version="1.0" encoding="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.Action
分别为增加,删除,修改,查询,获取准备对应的方法.
声明实例化ProductDAO pdao = new ProductDAO();以便于在每一个方法中调用
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.list.jsp
在web目录下,新建一个product目录,接着在product目录下创建list.jsp文件
list.jsp同时提供增加和显示功能
以及删除和修改的超链
<%@ 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.edit.jsp
在product目录下创建edit.jsp文件,用于编辑
<%@ 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
在WebContent目录下创建一个index.jsp,用于跳转到查询product页面
<jsp:forward page="listProduct"/>
- 测试
测试访问地址:
http://127.0.0.1:8080/struts_hibernate/