SSH 三大框架整合

1. Spring整合Struts2框架,存在2种方式(Action对象由哪个框架创建的)

* 导入开发的jar包
* struts2-spring-plugin-2.3.24.jar

* Action对象可以由Struts2框架创建,默认的方式
* 因为导入struts2-spring-plugin-2.3.24.jar包,提供类struts-plugin.xml配置文件,
该文件中配置了常量:<constant name="struts.objectFactory" value="spring" />
* 允许在Action中可以去Spring的容器中获取到指定名称的service的对象,默认按名称进行注入的
* 具体整合的代码如下
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{


private static final long serialVersionUID = 4406904609220191155L;

private Customer model = new Customer();

public Customer getModel() {
return model;
}

private CustomerService customerService;
public CustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}


/**
* 查询所有的数据
* @return
* @throws Exception
*/
public String list() throws Exception {
System.out.println("查询所有的客户...");

/* 之前的整合的方式,底层的原理
// 获取到web版本的工厂,获取到service的对象,调用方法
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
CustomerService cs = (CustomerService) context.getBean("customerService");
cs.findAll();
*/

// 第一种方式,Action对象由Struts2框架来创建
customerService.findAll();

return NONE;
}

}

<package name="crm" extends="struts-default" namespace="/">
<!-- 客户的模块 -->
<action name="customerAction_*" class="cn.itcast.customer.action.CustomerAction" method="{1}">

</action>
</package>


* service的代码
public class CustomerServiceImpl implements CustomerService {

public List<Customer> findAll() {
System.out.println("查询所有的客户...");
return null;
}

}

<bean id="customerService" class="cn.itcast.customer.service.impl.CustomerServiceImpl"/>

* Action对象可以由Spring框架来创建,推荐的方式
* 在applicationContext.xml配置文件中,配置Action类:<bean id="customerAction" class="cn.itcast.customer.action.CustomerAction"/>
* Action对象默认就是多例的,必须要配置成多例的
* <bean id="customerAction" class="cn.itcast.customer.action.CustomerAction" scope="prototype"/>

* struts.xml配置文件,编写的方式需要变化
* <action name="customerAction_*" class="customerAction" method="{1}"></action>

* 把service对象手动的注入到Action对象中去
<bean id="customerAction" class="cn.itcast.customer.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService"/>
</bean>


2. Spring整合hibernate框架,提供2种方式(整合只有一种方式,编写有2种方式)

* 有hibernate.cfg.xml配置文件
* 让Spring加载hibernate.cfg.xml配置文件,构建SessionFactory对象
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>

* 目的:操作数据库,Spring提供模板类,HibernateTemplate。配置类,给dao注入该对象
<!-- 配置HibernateTemplate类 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

* 编写dao类
public class CustomerDaoImpl implements CustomerDao {

private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}

public List<Customer> findAll() {
System.out.println("持久层:查询所有的客户...");
return (List<Customer>) hibernateTemplate.find("from Customer");
}
}

* 完成注入
<!-- 配置dao类 -->
<bean id="customerDao" class="cn.itcast.customer.dao.impl.CustomerDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>

* dao编写继承HibernateDaoSupport类
* 可以注入HibernateTemplate
* 也可以注入SessionFactory对象

* 事务管理,配置文件如下
<!-- 进行事务管理 -->
<!-- 平台事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 事务的通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- AOP的事务增强 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(public * cn.itcast.*.service.impl.*ServiceImpl.*(..))"/>
</aop:config>


* 没有hibernate.cfg.xml配置文件,把hibernate.cfg.xml配置文件中的内容给LocalSessionFactoryBean对象注入进去
<!-- 配置C3P0的连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///spring_day05"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>

<!--
Spring框架整合hibernate框架 
加载hibernate.cfg.xml的配置文件,构建SessionFactory对象,把sessionFactory对象存入到容器中
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池对象 -->
<property name="dataSource" ref="dataSource"/>

<!-- 把其他的属性的值注入进来 -->
<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>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>

<!-- 引入映射的配置文件 -->
<property name="mappingLocations">
<array>
<value>classpath:cn/itcast/*/domain/*.hbm.xml</value>
</array>
</property>
</bean>

3. SSH整合是配置文件 + 注解

* Spring整合Struts2框架
* 在Action类上添加Struts2的注解,启动服务器能访问到Action
@ParentPackage(value="crm")
@Namespace("/")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>

@Action(value="customerAction_list")
public String list() throws Exception {
System.out.println("查询所有的客户...");
// 第一种方式,Action对象由Struts2框架来创建
List<Customer> list = customerService.findAll();
for (Customer customer : list) {
System.out.println(customer.getCustName());
}
return NONE;
}

* Spring整合Action
// Spring的注解
@Controller(value="customerAction")
@Scope(value="prototype")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>

注入service对象
@Autowired
private CustomerService customerService;

* Spring整合hibernate框架
* 把Customer类使用注解的方式实现
@Entity
@Table(name="cst_customer")
public class Customer implements Serializable{

private static final long serialVersionUID = -2590899645789409209L;

// 包装类型,尽量使用包装类型
@Id
@Column(name="cust_id")
@GenericGenerator(name="sysNative",strategy="native")
@GeneratedValue(generator="sysNative")
private Long custId;

@Column(name="cust_name")
private String custName;
@Column(name="cust_source")
private String custSource;
@Column(name="cust_industry")
private String custIndustry;
@Column(name="cust_level")
private String custLevel;
@Column(name="cust_address")
private String custAddress;
@Column(name="cust_phone")
private String custPhone;

* Spring整合hibernate配置文件,LocalSessionFactoryBean对象,扫描包
<!-- 配置C3P0的连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///spring_day05"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>

<!-- Spring整合hibernate框架 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池对象 -->
<property name="dataSource" ref="dataSource"/>

<!-- 把其他的属性的值注入进来 -->
<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>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>

<!-- 引入映射的配置文件 -->
<property name="packagesToScan">
<array>
<value>cn.itcast.*.domain</value>
</array>
</property>
</bean>

* 配置HibernateTemplate模板对象
<!-- 配置hibernate模板对象 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

* 在dao中注入模板对象
@Repository("customerDao")
public class CustomerDaoImpl implements CustomerDao {

@Resource(name="hibernateTemplate")
private HibernateTemplate hibernateTemplate;

public List<Customer> findAll() {
System.out.println("持久层:查询所有的客户...");
return (List<Customer>) hibernateTemplate.find("from Customer");
}


public void save(Customer model) {
hibernateTemplate.save(model);
}
}


* 配置事务,采用注解的方式
<!-- 平台事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>


@Transactional
public class CustomerServiceImpl implements CustomerService

4. Spring整合Struts2框架

* 把Action对象交给Spring去管理(Action必须是多例的),把service对象注入到Action对象中去。


5. Spring整合hibernate框架

* 让Spring框架加载hibernate.cfg.xml配置文件,使用LocalSessionFactoryBean对象,构建sessionFactory对象
* Spring提供hibernateTemplate模板类,使用该模板类完成增删改查的操作

















































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值