SSH框架的整合
方式一 带有Hibernate配置文件
- 导入Jar包
2.引入配置文件
- Struts2
- web.xml
<!-- 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>
struts.xml
<?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>
</struts>
Hibernate
- 核心配置文件 Hibernate.cfg.xml
<?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:///SSH_01?useUnicode=true&characterEncoding=utf8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 可选配置 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
Spring
- web.xml
<!-- 配置Spring框架整合WEB的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 默认只能加载WEB——INF目录下的配置文件,提供配置方式,加载Src目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
工程结构
image.png
Struts2和Spring的整合
方式一 Action由Struts自己创建
编写一个CustomerAction继承ActionSupport
用于页面的访问
public class CustomerAction extends ActionSupport {
private static final long serialVersionUID = 1L;
/*
* 保存客户
*/
public String save() {
return NONE;
}
}
在struts.xml配置action
<!-- 先配置包结构 -->
<package name="Customer" extends="struts-default" namespace="/">
<action name="customer_*" class="com.coderhong.web.action.CustomerAction" method="{1}"></action> </action>
</package>
在表单访问路径
action="${pageContext.request.contextPath }/customer_save.action"
那么Action配置完成 下面开始编写业务层跟Dao层
写在的项目结构:
业务层
public class CustomerServiceImpl implements CustomerService {
public void save(Customer customer) {
System.out.println("业务层:保存客户");
}
}
dao层
public class ServiceDaoImple implements ServiceDao {
@Override
public void save(Customer customer) {
System.out.println("dao层:保存客户");
}
}
同时也编写了一个Customer的javaBean,用来Struts封装数据
用CustomerAction使用模型驱动的方式封装数据
image.png
让业务层跟dao层类交给Spring管理,配置
<!-- 配置业务层 -->
<bean id="customerService" class="com.coderhong.service.CustomerServiceImpl">
<!-- 注入dao -->
<property name="customerDao" ref="customerDao"></property>
</bean>
<!-- 配置业持久层 -->
<bean id="customerDao" class="com.coderhong.dao.ServiceDaoImple"></bean>
同时让CustomerService拥有持久层属性
public class CustomerServiceImpl implements CustomerService {
// 注入持久层
private CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
public void save(Customer customer) {
System.out.println("业务层:保存客户");
customerDao.save(customer);
}
}
在Action中调用业务层的类
/*
* 保存客户
*/
public String save() {
// 在Action中调用业务层类
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
CustomerService cs = (CustomerService) webApplicationContext.getBean("customerService");
cs.save(customer);
return NONE;
}
这里可以看出Action调用业务类很麻烦,因为需要在每一个Action中每个方法上获取工厂,通过工厂获取业务类。
为了简化这一代码引入一个插件的包 struts2-spring-plugin-2.3-24.jar
这个插件主要是开启了一个常量
<constant name="struts.objetcFactory" value="spring"/>
那么就可以在Action中注入业务类属性了。
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
private static final long serialVersionUID = 1L;
// 模型驱动方式 封装数据
private Customer customer = new Customer();
public void setCustomer(Customer customer) {
this.customer = customer;
}
// 实现ModelDriven接口的抽象方法
public Customer getModel() {
return customer;
}
// 注入业务类
private CustomerService customerService;
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
/*
* 保存客户
*/
public String save() {
customerService.save(customer);
return NONE;
}
}
注意:测试的acton还是由Struts2管理
方式二 Action类由Spring创建(推荐)
引入struts2-spring-plugin-2.3-24.jar
在applicationContext.xml 配置Action
<!-- 配置Action -->
<bean id="customerAction" class="com.coderhong.web.action.CustomerAction">
<!-- 必须手动注入业务类 -->
<property name="customerService" ref="customerService"></property>
</bean>
Hibernate和Spring的整合
带Hibernate配置文件的方式
创建Customer的映射文件 Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.coderhong.domain.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"/>
</id>
<property name="cust_name" column="cust_name"/>
<property name="cust_user_id" column="cust_user_id"/>
<property name="cust_create_id" column="cust_create_id"/>
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_linkman" column="cust_linkman"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping>
在核心配置文件引入Customer的映射文件
<hibernate-configuration>
<session-factory>
<!-- 必须配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///SSH_01?useUnicode=true&characterEncoding=utf8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 可选配置 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置C3P0的连接池 -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 映射配置文件 -->
<mapping resource="com/coderhong/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
在Spring中配置如下设置
<!-- 编写bean 名称都是固定的 加载hibernate.cfg.xml -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
这里用的Hibernate 改写Dao,继承HibernateDaoSupport
public class CustomerDaoImple extends HibernateDaoSupport implements CustomerDao {
@Override
public void save(Customer customer) {
System.err.println("持久层:保存客户");
}
}
配置Dao
<!-- 配置业持久层 -->
<bean id="customerDao" class="com.coderhong.dao.CustomerDaoImple">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
调用模板中的方法:
public class CustomerDaoImple extends HibernateDaoSupport implements CustomerDao {
@Override
public void save(Customer customer) {
// 保存
this.getHibernateTemplate().save(customer);
}
}
这个时候保存数据,会报错。需要添加了事物才可以保存数据。
配置事务
<!-- 配置平台事物管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--开启事物的注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
在CustomerServiceImpl类添加注解:
public class CustomerServiceImpl implements CustomerService {
// 注入持久层
private CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
public void save(Customer customer) {
System.out.println("业务层:保存客户");
customerDao.save(customer);
}
}
再次运行就可以保存数据了。
不带Hibernate配置文件的方式
修改Spring配置文件,将之前Hibernate.cfg.xml内容整合进去。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/SSH_01?useUnicode=true&characterEncoding=utf8"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 编写bean 名称都是固定的 加载hibernate.cfg.xml -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
<!-- 不适用 hibernate.cfg.xml方式-->
<!-- 先加载连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载方言 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 引入映射文件 -->
<property name="mappingResources">
<list>
<value>com/coderhong/domain/Customer.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置平台事物管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--开启事物的注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置业务层 -->
<bean id="customerService" class="com.coderhong.service.CustomerServiceImpl">
<!-- 注入dao -->
<property name="customerDao" ref="customerDao"></property>
</bean>
<!-- 配置业持久层 -->
<bean id="customerDao" class="com.coderhong.dao.CustomerDaoImple">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置Action -->
<bean id="customerAction" class="com.coderhong.web.action.CustomerAction" scope="prototype">
<!-- 必须手动注入业务类 -->
<property name="customerService" ref="customerService"></property>
</bean>
</beans>
*HibernateTemplate的数据操作
/*
* 持久层保存客户
*/
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
// public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
// this.hibernateTemplate = hibernateTemplate;
// }
@Override
public void save(Customer customer) {
System.out.println("持久层:保存客户...");
// 使用Spring提供的JDBC模板类
this.getHibernateTemplate().save(customer);
}
@Override
public void update(Customer customer) {
this.getHibernateTemplate().update(customer);
}
/*
* 通过主键查询
*/
@Override
public Customer getById(long cust_id) {
Customer customer = this.getHibernateTemplate().get(Customer.class, cust_id);
return customer;
}
// 查询所有
@Override
public List<Customer> findAll() {
List<Customer> list = (List<Customer>) this.getHibernateTemplate().find("from Customer");
return list;
}
@Override
public List<Customer> findAllByQBC() {
// 离线条件查询
DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
List<Customer> list = (List<Customer>)this.getHibernateTemplate().findByCriteria(criteria);
return list;
}
}
网友评论