来自:Egler > 馆藏分类
配色: 字号:
ssh整合讲解
2017-06-20 | 阅:  转:  |  分享 
  
传智播客——专注于Java、.Net和Php、网页平面设计工程师的培训Spring_day04总结今日任务?使用SSH整合完成客户的保存操作教学导航教学目标

教学方法案例驱动法案例一使用SSH的整合完成客户的保存操作1.1案例需求1.1.1需求描述使用SSH整合完成CRM的客户保存操作

传智播客——专注于Java、.Net和Php、网页平面设计工程师的培训1.2相关知识点:1.2.1SSH简单的回顾:1.2.1.1SSH的基本开发回顾

1.2.2SSH框架的整合方式一:零障碍整合(带有Hibernate配置文件)1.2.2.1创建web项目,引入相关jar包.【Struts2】D:\struts2\struts-2.3.24\apps\struts2-blank\WEB-INF\lib\.jar

传智播客——专注于Java、.Net和Php、网页平面设计工程师的培训Struts2需要了解的jar包:struts2-convention-plugin-2.3.24.jar---Struts2注解的开发包.struts2-json-plugin-2.3.24.jar---Struts2整合AJAX返回JSON数据.struts2-spring-plugin-2.3.24.jar---Struts2整合Spring的插件包.【Hibernate】D:\hibernate-release-5.0.7.Final\lib\required\.jar

日志记录:log4j的包由Spring引入.数据库驱动:Hibernate引入连接池:D:\hibernate-release-5.0.7.Final\lib\optional\c3p0\.jar

【Spring】基本的开发:AOP开发:

传智播客——专注于Java、.Net和Php、网页平面设计工程师的培训JDBC开发:事务管理的开发:整合Hibernate:

整合web项目:1.2.2.2引入相关的配置文件:【Struts2】web.xmlstruts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2/struts.xml【Hibernate】

核心配置:hibernate.cfg.xml映射文件:【Spring】web.xmlorg.springframework.web.context.ContextLoaderListener
传智播客——专注于Java、.Net和Php、网页平面设计工程师的培训class>
contextConfigLocationclasspath:applicationContext.xmlapplicationContext.xml

log4j.propertiess1.2.2.3引入相关的页面并进行修改:1.2.2.4创建包结构和相关的类:

1.2.2.5Struts2和Spring的整合:方式一:Action类由Struts2自己创建【编写Action中的save方法】/保存客户的执行的方法:save/publicStringsave(){System.out.println("Action中的save方法执行了...");returnNONE;}

传智播客——专注于Java、.Net和Php、网页平面设计工程师的培训【配置Action类】【在Action中调用业务层的类】

配置Service:在Action中调用//传统方式的写法WebApplicationContextwebApplicationContext=WebApplicationContextUtils

.getWebApplicationContext(ServletActionContext.getServletContext());CustomerServicecustomerService=(CustomerService)webApplicationContext.getBean("customerService");这种写法很麻烦的,因为需要在每个Action中的每个方法上获取工厂,通过工厂获得类.为了简化这个代码引入一个插件的包:struts2-spring-plugin-2.3.24.jar在这个插件中开启一个Struts2常量默认的情况下struts2将这个常量关闭的,现在引入插件以后,将常量开启了,引发下面的一些常量生效.

struts.objectFactory.spring.autoWire=name那么就可以在Action中提供想注入的属性了:publicclassCustomerActionextendsActionSupportimplementsModelDriven{//模型驱动使用的对象privateCustomercustomer=newCustomer();@OverridepublicCustomergetModel(){returncustomer;}

//注入业务层的类:

传智播客——专注于Java、.Net和Php、网页平面设计工程师的培训privateCustomerServicecustomerService;publicvoidsetCustomerService(CustomerServicecustomerService){this.customerService=customerService;}/保存客户的执行的方法:save

/publicStringsave(){System.out.println("Action中的save方法执行了...");//传统方式的写法/WebApplicationContextwebApplicationContext=WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());CustomerServicecustomerService=(CustomerService)webApplicationContext.getBean("customerService");///自动注入

customerService.save(customer);returnNONE;}}【在Service中编写save方法】publicclassCustomerServiceImplimplementsCustomerService{@Overridepublicvoidsave(Customercustomer){System.out.println("Service中的save方法执行了...");}

}1.2.2.6Struts2和Spring的整合方式二:Action类由Spring创建.(推荐)【引入插件包】struts2-spring-plugin-2.3.24.jar【Action交给Spring管理】将Action配置到Spring中.


传智播客——专注于Java、.Net和Php、网页平面设计工程师的培训scope="prototype">Action的配置:

1.2.2.7在业务层调用DAO【将DAO配置到Spring中】【在业务层注入Dao】

publicclassCustomerServiceImplimplementsCustomerService{//注入DaoprivateCustomerDaocustomerDao;publicvoidsetCustomerDao(CustomerDaocustomerDao){this.customerDao=customerDao;}@Overridepublicvoidsave(Customercustomer){

System.out.println("Service中的save方法执行了...");customerDao.save(customer);}}

传智播客——专注于Java、.Net和Php、网页平面设计工程师的培训1.2.2.8Spring整合Hibernate:【创建映射文件】

【加载到核心配置文件】

【在Spring的配置文件中完成如下配置】【改写DAO】publicclassCustomerDaoImplextendsHibernateDaoSupportimplementsCustomerDao{

@Overridepublicvoidsave(Customercustomer){System.out.println("DAO中的save方法执行了...");}}

传智播客——专注于Java、.Net和Php、网页平面设计工程师的培训【调用模板中的方法】@Overridepublicvoidsave(Customercustomer){System.out.println("DAO中的save方法执行了...");//保存:this.getHibernateTemplate().save(customer);

}1.2.2.9配置Spring的事务管理:【配置事务管理器】【注解事务管理的开启】

【在业务层添加一个注解】1.2.3SSH框架的整合方式二:不带Hibernate的配置文件1.2.3.1复制一个SSH1的项目.1.2.3.2查看Hibernate的配置文件:

Hibernate的配置文件包含如下内容:连接数据库必要的参数:Hibernate的属性:连接池的配置:映射文件的引入:

传智播客——专注于Java、.Net和Php、网页平面设计工程师的培训1.2.3.3替换数据库连接参数和连接池的配置:创建jdbc.propertiesjdbc.driverClass=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql:///ssh1jdbc.username=rootjdbc.password=123

在Spring中引入外部属性文件配置连接池:1.2.3.4配置Hibernate的其他属性及映射:


key="hibernate.dialect">org.hibernate.dialect.MySQLDialecttruetrueupdate


传智播客——专注于Java、.Net和Php、网页平面设计工程师的培训cn/itcast/ssh/domain/Customer.hbm.xml1.2.4HibernateTemplate的使用:

publicclassCustomerDaoImplextendsHibernateDaoSupportimplementsCustomerDao{@Overridepublicvoidsave(Customercustomer){System.out.println("DAO中的save方法执行了...");//保存:this.getHibernateTemplate().save(customer);}@Overridepublicvoidupdate(Customercustomer){

this.getHibernateTemplate().update(customer);}@Overridepublicvoiddelete(Customercustomer){this.getHibernateTemplate().delete(customer);}@OverridepublicCustomerfindById(Longid){returnthis.getHibernateTemplate().load(Customer.class,id);

}@OverridepublicListfindAllByHQL(){Listlist=(List)this.getHibernateTemplate().find("fromCustomer");returnlist;}publicListfindAllByQBC(){DetachedCriteriadetachedCriteria=

DetachedCriteria.forClass(Customer.class);Listlist=(List)

传智播客——专注于Java、.Net和Php、网页平面设计工程师的培训this.getHibernateTemplate().findByCriteria(detachedCriteria);returnlist;}}1.2.5延迟加载的问题的解决:OpenSessionInViewFilter

OpenSessionInViewFilterorg.springframework.orm.hibernate5.support.OpenSessionInViewFilterOpenSessionInViewFilter.action

献花(0)
+1
(本文系Egler首藏)