java ssh 整合_Java - 框架之 SSH 整合

十四. ssh 整合1 - 包

1. Struts jar 包

- Struts-2.xx\apps\stutrs2-blank\WEB-INF\lib

2. Hibernate jar 包

- hibernate-release-5.0.7.Final\lib\required

- mysql 驱动包

- 日志记录

3. Spring jar 包

十五. ssh 整合 - 配置文件

1. Struts 配置文件

- web.xml

- struts.xml

2. Hibernate 配置文件

- hibernate.cfg.xml

- 映射文件

3. Spring 配置文件

- web.xml

- applicationContext.xml

- 日志记录

十六. ssh 整合2 - Spring 和 Struts2 的整合方式一: Action 由 Struts2 创建

1. 导入 struts2-spring-plugin-2.3.33.jar 整合包

2. applicationContext.xml 文件中配置 Service (applicationContext.xml)

3.  配置Action (struts.xml)

4. CustomerAction.java 文件中

importcom.opensymphony.xwork2.ActionSupport;importcom.opensymphony.xwork2.ModelDriven;importcom.q.ssh.domain.Customer;importcom.q.ssh.service.CustomerService;public class CustomerAction extends ActionSupport implements ModelDriven{//模型驱动使用对象

privateCustomer customer;

@OverridepublicCustomer getModel() {returncustomer;

}//注入 CustomerService

privateCustomerService customerService;public voidsetCustomerService(CustomerService customerService) {this.customerService =customerService;

}//保存客户 :save

publicString save(){

System.out.println("save>>>");

customerService.save(customer);//这里就可以直接调用 save 方法。

returnNONE;

}

}

十七. ssh 整合2 - Spring 和 Struts2 的整合方式二: Action 由 Spring 创建

注意:

- 需要配置 Action 为多例。

- 需要手动注入 Service。

1. 导入 struts2-spring-plugin-2.3.33.jar 整合包

2. 将 Action 交给 Spring (applicationContext.xml)

3. 配置 Action (Struts.xml)

十八. ssh 整合3 - Service 调用 DAO

1. 将DAO交给 Spring 管理

2. 业务层注入DAO (CustomerServiceImpl.java)

importcom.q.ssh.dao.CustomerDao;importcom.q.ssh.domain.Customer;importcom.q.ssh.service.CustomerService;public class CustomerServiceImpl implementsCustomerService {//注入 DAO

privateCustomerDao customerDao;public voidsetCustomerDao(CustomerDao customerDao) {this.customerDao =customerDao;

}

@Overridepublic voidsave(Customer customer) {

System.out.println("Service 中的 save...");

customerDao.save(customer);

}

}

3. 在Service配置中注入 (applicationContext.xml)

// 将customerDao注入 :

十九. ssh 整合3 - Spring 整合 Hibernate 框架

1. 创建数据库

2. 在 Spring 的配置文件中,引入 Hibernate 的配置信息

3. 修改 CustomerDaoImpl.java 的类 继承 HibernateDaoSupport

importcom.q.ssh.dao.CustomerDao;importcom.q.ssh.domain.Customer;importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;public class CustomerDaoImpl extends HibernateDaoSupport implementsCustomerDao {

@Overridepublic voidsave(Customer customer) {

System.out.println("DAO 中的 save...");

}

}

4. 配置DAO 中导入

5. 在DAO中 Hibernate 的模板完成保存操作

public class CustomerDaoImpl extends HibernateDaoSupport implementsCustomerDao {

@Overridepublic voidsave(Customer customer) {

System.out.println("DAO 中的 save...");//调用 Save 方法。

this.getHibernateTemplate().save(customer);

}

}

6. 配置 Spring 的事务管理

7. 开启注解事务

8. 在业务层使用注解

@Transactionalpublic class CustomerServiceImpl implementsCustomerService {//注入 DAO

privateCustomerDao customerDao;public voidsetCustomerDao(CustomerDao customerDao) {this.customerDao =customerDao;

}

@Overridepublic voidsave(Customer customer) {

System.out.println("Service 中的 save...");

customerDao.save(customer);

}

}

二十. ssh 整合3 - Spring 整合 Hibernate 框架 (不带 Hibernate 配置文件)

1. 将 Hibernate 文件 使用Spring 整合到 applicationContext 文件中

org.hibernate.dialect.MySQLDialect

true

true

update

com/q/ssh/domain/Customer.hbm.xml

二十一. ssh 整合3 - Hibernate 模板使用

# 增删改查操作

public class CustomerDaoImpl extends HibernateDaoSupport implementsCustomerDao {//增

@Overridepublic voidsave(Customer customer) {

System.out.println("DAO 中的 save...");//将数据保存到数据库

System.out.println(customer);this.getHibernateTemplate().save(customer);

}//改

@Overridepublic voidupdate(Customer customer) {this.getHibernateTemplate().update(customer);

}//删

@Overridepublic voiddelete(Customer customer) {this.getHibernateTemplate().delete(customer);

}//查 (一个)

@OverridepublicCustomer findById(Integer id) {return this.getHibernateTemplate().get(Customer.class, id);

}//查 (多个)

@Overridepublic ListfindAllByHQL() {//List list = (List) this.getHibernateTemplate().find("from");//return list;

return null;

}//查 (多个)

@Overridepublic ListfindAllByQBC() {

DetachedCriteria criteria= DetachedCriteria.forClass(Customer.class);

List list = (List) this.getHibernateTemplate().findByCriteria(criteria);returnlist;

}

}

二十二. ssh 整合 - 延迟加载问题解决

# 由于 session 的开启和关闭都在 Service 层,一旦 Action 层调用已经关闭的 session 就会报错,

# 解决:将 session 的开启和关闭放在 Action 层。

OpenSessionInViewFilter

org.springframework.orm.hibernate4.support.OpenSessionInViewFilter

OpenSessionInViewFilter

*.action

二十三. ssh 整合 - web.xml 文件

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:applicationContext.xml

OpenSessionInViewFilter

org.springframework.orm.hibernate4.support.OpenSessionInViewFilter

OpenSessionInViewFilter

*.action

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值