文章目录
Hibernate+Struts框架整合
项目1
本项目的代码来源:how2java:相关的教程
在运行了该代码,依照个人理解阐述一下这两个框架如何有效的整合。
实体(model)
一个简单的Product实体类:包含id、产品名、价格三个属性。
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;
}
}
一个简单的hibernate-Product配置文件:
这个xml文件指示了映射关系,也可以通过注解的方式来实现。
有了这个文件,就可以实现ORM(对象关系映射)的功能。
<?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>
Dao层 以及Hibernate的配置文件
由于我们使用了Hibernate,所以建完MySQL数据库后,不需要我们在面向数据库进行其他的操作。
一个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=UTF-8</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</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>
下面写一个ProductDao层:
将我们需要用到的操作,封装成函数即可。
Dao层的函数实现思路:
- 首先,根据配置文件获取一个sessionfactory。
- sessonfactory 创建 session。
- session 开启 事务。
- 处理相关的Dao层逻辑。
- 提交事务,关闭资源。
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;
}
}
Controllor控制器及struts2配置文件
ProductAction 的控制器。编写思路为:
- 将我们需要用到的资源,都写成 成员属性,然后为其提供get、set方法。
- 编写url 对应的处理函数。
- 通过配置文件或者注解返回给我们前端网页(view视图)
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";
}
}
struts2配置文件:
<?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>
web.xml配置
<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>
View(视图):前端
index.jsp
<jsp:forward page="listProduct"/>
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>
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>
参考
代码来源:how2java