您的位置:首页 > 编程语言 > Java开发

1、Struts2和Hibernate的简单整合(带Session的管理方式)

2015-10-15 20:16 211 查看
1、关于数据库:是部门和员工的关系

关于entity和xx.hbm.xml的实现

Dept.class

package cn.itcast.entity;

import java.util.HashSet;
import java.util.Set;

public class Dept {

private int deptId;
private String deptName;
// 【一对多】 部门对应的多个员工
private Set<Employee> emps = new HashSet<Employee>();

public Dept(int deptId, String deptName) {
super();
this.deptId = deptId;
this.deptName = deptName;
}
public Dept() {
super();
}
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Set<Employee> getEmps() {
return emps;
}
public void setEmps(Set<Employee> emps) {
this.emps = emps;
}
@Override
public String toString() {
return "Dept [deptId=" + deptId + ", deptName=" + deptName + "]";
}

}


Dept.hbm.xml

<?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="cn.itcast.entity">

<class name="Dept" table="t_dept" >
<id name="deptId">
<generator class="native"></generator>
</id>
<property name="deptName" length="20"></property>

<set name="emps">
<key column="dept_id"></key>
<one-to-many class="Employee"/>
</set>
</class>

</hibernate-mapping>


Employee.class

package cn.itcast.entity;

public class Employee {

private int empId;
private String empName;
private double salary;
// 【多对一】员工与部门
private Dept dept;;

public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}

}


Employee.hbm.xml

<?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="cn.itcast.entity">

<class name="Employee" table="t_employee">
<id name="empId">
<generator class="native"></generator>
</id>
<property name="empName" length="20"></property>
<property name="salary" type="double"></property>

<many-to-one name="dept" column="dept_id" class="Dept"></many-to-one>

</class>

</hibernate-mapping>


2、关于dao的实现

DeptDao.java

package cn.itcast.dao;

import cn.itcast.entity.Dept;
import cn.itcast.utils.HibernateUtils;

public class DeptDao {

/**
* 主键查询
*/
public Dept findById(int id){
// 获取session, 执行操作
return (Dept) HibernateUtils.getSession().get(Dept.class, id);
}
}


3、关于serrvice的实现

DeptService.java

package cn.itcast.service;

import cn.itcast.dao.DeptDao;
import cn.itcast.entity.Dept;
import cn.itcast.utils.HibernateUtils;

public class DeptService {

// 调用的dao
private DeptDao deptDao  = new DeptDao();

public Dept findById(int id){
return deptDao.findById(id);
}
}


4、关于Action的实现

DeptAction.java

package cn.itcast.action;

import cn.itcast.entity.Dept;
import cn.itcast.service.DeptService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DeptAction extends ActionSupport{

// Service
private DeptService deptService = new DeptService();

// 默认处理方法
public String execute() {
// 主键查询(模拟数据)
Dept dept = deptService.findById(12);
// 保存
ActionContext.getContext().getContextMap().put("dept", dept);
return SUCCESS;
}
}


4、session拦截器的实现

SessionInterceptor.java

package cn.itcast.interceptor;

import org.hibernate.Session;

import cn.itcast.utils.HibernateUtils;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
* Session管理拦截器:
*       当访问action的时候,创建session;
*      action ---> service  --> dao 【获取的是这里创建的session】
* @author Jie.Yuan
*
*/
public class SessionInterceptor extends AbstractInterceptor {

@Override
public String intercept(ActionInvocation invocation) throws Exception {

try {
// 1. 先创建Session
Session session = HibernateUtils.getSession();
// 2. 开启事务
session.beginTransaction();

// 3. 执行Action
String result = invocation.invoke();

// 4. 提交事务
session.getTransaction().commit();  // 不需要关闭session

// 返回结果视图
return result;
} catch (Exception e) {
e.printStackTrace();
return "error";
}
}

}


5、关于HibernateUtils的实现

package cn.itcast.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {

// 初始化SessionFactory
private static SessionFactory sf;
static {
sf = new Configuration().configure().buildSessionFactory();
}

// 创建(获取)Session
public static Session getSession() {
// 线程的方式创建session,必须要配置
// 可以不用关闭,会自动关。
return sf.getCurrentSession();
}
}


6、关于hibernate.cfg.xml文件配置

<!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节点代表一个数据库 -->
<session-factory>

<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hib_demo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql
<property name="hibernate.format_sql">true</property>  -->
<!-- 2.3 自动建表  -->
<property name="hibernate.hbm2ddl.auto">update</property>

<!-- 配置session的创建方式:线程方式创建session对象 -->
<property name="hibernate.current_session_context_class">thread</property>

<!--****************** 【连接池配置】****************** -->
<!-- 配置连接驱动管理类 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 配置连接池参数信息 -->
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.max_size">4</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.idle_test_period">30000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>

<!--****************** 【二级缓存配置】****************** -->
<!-- a.  开启二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- b. 指定使用哪一个缓存框架(默认提供的) -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<!-- 开启查询缓存 -->
<property name="hibernate.cache.use_query_cache">true</property>
<!-- c. 指定哪一些类,需要加入二级缓存 -->
<class-cache usage="read-write" class="cn.itcast.b_second_cache.Dept"/>
<class-cache usage="read-only" class="cn.itcast.b_second_cache.Employee"/>
<!-- 集合缓存[集合缓存的元素对象,也加加入二级缓存] -->
<collection-cache usage="read-write" collection="cn.itcast.b_second_cache.Dept.emps"/>

<!-- 3. 加载所有映射
<mapping resource="cn/itcast/a_hello/Employee.hbm.xml"/>
-->
</session-factory>
</hibernate-configuration>

<!-- 开启查询缓存 -->
<property name="hibernate.cache.use_query_cache">true</property>
<!-- c. 指定哪一些类,需要加入二级缓存 -->
<class-cache usage="read-write" class="com.baowei.entity.Group" />
<class-cache usage="read-only" class="com.baowei.entity.User" />
<!-- 集合缓存[集合缓存的元素对象,也加加入二级缓存] -->
<collection-cache usage="read-write"
collection="com.baowei.entity.Group.users" />


7、关于struts.xml文件的配置

<!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>
<!-- 数据库连接信息 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hib_demo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- session创建方式 -->
<property name="hibernate.current_session_context_class">thread</property>

<!-- 加载映射 -->
<mapping resource="cn/itcast/entity/Dept.hbm.xml"/>
<mapping resource="cn/itcast/entity/Employee.hbm.xml"/>

</session-factory>
</hibernate-configuration>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: