目录
0、Struts + Hibernate
整合struts和hibernate的基本思路是将MVC中M层交给hibernate处理
在eclipse中创建struts_hibernate项目
本项目以Product的增删改查为例,整合Struts+Hibernate
整合步骤分 hibernate部分和struts部分
1、思路图
基本思路就是把MVC中的Model模型部分使用hibernate来做
2、Hibernate准备工作
这部分准备Hibernate方面的事情
3、创建数据库how2java
create database how2java
4、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;
}
}
5、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>
6、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>
7、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;
}
}