一、搭建struts2与Hibernate整合的环境
1、创建web项目,导入jar包
(1)、hibernate所需包
(2)、log4j日志包
(3)、数据库驱动包
(4)、c3p0连接池包
(5)、struts2包
(6)、删除重复的低版本的jar包
(7)、导入jstl包
2、编写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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/crm_hibernate_strtus</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin123</property>
<!-- 2、hibernate的基本配置 -->
<!-- 数据库的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 是否显示SQL语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 是否格式化SQL语句 -->
<property name="hibernate.format_sql">false</property>
<!-- 选择生成DDL语句的策略
它只能用于更新表结构,不能用于创建数据库
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置连接池的提供商-->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 把session绑定到当前线程上 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 3、指定映射文件的位置 -->
<mapping class="cn.itcast.domain.Customer"/>
</session-factory>
</hibernate-configuration>
3、编写struts2主配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 开启开发者模式 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 动作配置 -->
<package name="customer" extends="struts-default" namespace="/customer">
<!-- 查询所有客户 -->
<action name="findAllCustomer" class="cn.itcast.web.action.CustomerAction" method="findAllCustomer">
<result name="findAllCustomer" type="dispatcher">/jsp/customer/list.jsp</result>
</action>
</package>
</struts>
4、在web.xml中配置sruts2核心过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>crm_sh</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置struts2核心过滤器 -->
<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>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5、编写web层代码
package cn.itcast.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import cn.itcast.domain.Customer;
import cn.itcast.service.CustomerService;
import cn.itcast.service.impl.CustomerServiceImpl;
/**
* 客户动作类
* @Description: TODO
* @author wingzhe
* @date 2017年8月7日 上午10:00:01
* @version V1.0
*/
public class CustomerAction {
private CustomerService custService = new CustomerServiceImpl();
/*
* 查询所有客户
*/
public String findAllCustomer(){
//调用业务层方法查询
List<Customer> customers = custService.findAllCustomer();
//获取request对象
HttpServletRequest request = ServletActionContext.getRequest();
//把参数放入域对象
request.setAttribute("customers", customers);
return "findAllCustomer";
}
}
6、编写hibernate工具类
package cn.itcast.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* hibernate工具类
*
* @Description: TODO
* @author wingzhe
* @date 2017年8月7日 上午9:28:44
* @version V1.0
*/
public class HibernateUtil {
private static SessionFactory factory;
static {
try {
Configuration config = new Configuration().configure();
factory = config.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取新的session
*/
public static Session openSession() {
return factory.openSession();
}
/**
* 获取本地绑定session
*/
public static Session getCurrentSession() {
return factory.getCurrentSession();
}
}
7、编写domain层
package cn.itcast.domain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
public class Customer implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cust_id")
private Long custId;
@Column(name = "cust_name")
private String custName;
@Column(name = "cust_source")
private String custSource;
@Column(name = "cust_indeustry")
private String custIndustry;
@Column(name = "cust_level")
private String custLevel;
@Column(name = "cust_address")
private String custAddress;
@Column(name = "cust_phone")
private String custPhone;
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustSource() {
return custSource;
}
public void setCustSource(String custSource) {
this.custSource = custSource;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
@Override
public String toString() {
return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource
+ ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress
+ ", custPhone=" + custPhone + "]";
}
}
8、编写service层代码
package cn.itcast.service;
import java.util.List;
import cn.itcast.domain.Customer;
/**
* 业务层接口
* @Description: TODO
* @author wingzhe
* @date 2017年8月7日 上午9:34:12
* @version V1.0
*/
public interface CustomerService {
//查询所有客户
public List<Customer> findAllCustomer();
}
package cn.itcast.service.impl;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import cn.itcast.dao.CustomerDao;
import cn.itcast.dao.impl.CustomerDaoImpl;
import cn.itcast.domain.Customer;
import cn.itcast.service.CustomerService;
import cn.itcast.utils.HibernateUtil;
/**
* service层实现类
* @Description: TODO
* @author wingzhe
* @date 2017年8月7日 上午9:45:26
* @version V1.0
*/
public class CustomerServiceImpl implements CustomerService {
private CustomerDao custDao = new CustomerDaoImpl();
public List<Customer> findAllCustomer() {
Session session = null;
Transaction tx = null;
try {
//使用工具类获取本地线程session
session = HibernateUtil.getCurrentSession();
//开启事务
tx = session.beginTransaction();
//执行查询操作
List<Customer> custList = custDao.findAllCustomer();
//提交事务
tx.commit();
return custList;
} catch (Exception e) {
//回滚事务
tx.rollback();
e.printStackTrace();
}
return null;
}
}
9、编写dao层代码
package cn.itcast.dao;
import java.util.List;
import cn.itcast.domain.Customer;
/**
* dao层接口
* @Description: TODO
* @author wingzhe
* @date 2017年8月7日 上午9:46:12
* @version V1.0
*/
public interface CustomerDao {
public List<Customer> findAllCustomer();
}
package cn.itcast.dao.impl;
import java.util.List;
import org.hibernate.Session;
import cn.itcast.dao.CustomerDao;
import cn.itcast.domain.Customer;
import cn.itcast.utils.HibernateUtil;
public class CustomerDaoImpl implements CustomerDao {
//查询所有用户
public List<Customer> findAllCustomer() {
// 从当前线程获取sesion去查询
Session session = HibernateUtil.getCurrentSession();
List<Customer> custList = (List<Customer>) session.createQuery("from Customer");
return custList;
}
}