n.MainActionquot/gt d修改 struts.xml 文件,让 struts2 中 action 完全让 spring 容器管理 ltaction classquotmainActionquot/gt 注意:struts 中 class 值要与 spring 中 id 相同 struts 会根据 class 名字去 spring 容器查找 对象 6测试 struts 与 spring 集成3 strutshibernatespring 集成 a添加类库 antlr-2.7.6.jar commons-collections-3.1.jar hibernate3.jar javassist-3.9.0.GA.jar jta-1.1.jar slf4j-api-1.5.8.jar slf4j-nop-1.5.8.jar 添加数据驱动类库 mysql-connector-java-5.1.16-bin.jar b创建实体类 User.java c添加 hibernate 配置文件 在源文件 config 下创建包 hibernate.cfg hibernate.cfg.xml hibernate.hbm .hbm.xml d对象模型生成关系模型 在项目工程中创建一个 test 源文件 作用:放置所有测试文件,方便统一管理 com.tarena.util.hibernate.ExportUtil espring 与 hibernate 集成 spring 管理 hibernate 映射文件 hibernate.cfg.xml .hbm.xml lt-- spring 管理 hibernate--gt ltbean idquotsessionFactoryquotclassquotorg.springframework.orm.hibernate3.LocalSessionFactoryBeanquotgt ltproperty namequotconfigLocationsquotgt ltlistgt ltvaluegtclasspath:hibernate/cfg/hibernate.cfg.xmllt/valuegt lt/listgt lt/propertygt lt/beangt f编写数据访问层 需要集成 spring 支持 hibernate 模板类,方便开发 hibernate 中 session 交与 spring 统一管理 UserDaoImpl extends HibernateDaoSupport g测试 spring 配置文件 com.tarena.util.spring.SpringTest h配置 spring 声明事物管理 1添加类库 aspectjweaver.jar aspectjrt.jar 2在 spring 配置文件中配置声明事物管理 lt-- spring 事物管理配置 spring AOP实现 --gt lt-- 创建一个事物管理器 --gt ltbean idquottransactionManagerquotclassquotorg.springframework.orm.hibernate3.HibernateTransactionManagerquotgt ltproperty namequotsessionFactoryquot refquotsessionFactoryquot/gt lt/beangt lt-- 通知 --gt lttx:advice idquottranAdvicequot transaction-managerquottransactionManagerquotgt lttx:attributesgt lttx:method namequotsavequot propagationquotREQUIREDquot/gt lttx:method namequotupdatequot propagationquotREQUIREDquot/gt lttx:method namequotdeletequot propagationquotREQUIREDquot/gt lttx:method namequotfindquot read-onlyquottruequot/gt lt/tx:attributesgt lt/tx:advicegt lt-- 事物通知织入到切面 --gt ltaop:configgt lt-- 定义切面 --gt ltaop:pointcut idquottranPointCutquot expressionquotexecutioncom.tarena.dao....quot /gt lt-- 织入 --gt ltaop:advisor advice-refquottranAdvicequot pointcut-refquottranPointCutquot/gt lt/aop:configgt 注意:spring 管理下 hibernate 连接 spring 容器管理关闭 声明式事务管理: 了解事务的几种传播特性 1PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务。
如果没有事务则开启。
2PROPAGATION_SUPPORTS: 如果存在一个事务,支持当前事务。
如果没有事务,则非事务的执行。
3PROPAGATION_MANDATORY: 如果已经存在一个事务, 支持当前事务。
如果没有一个活动的事务,则抛出异常。
4PROPAGATION_REQUIRES_NEW: 总是开启一个新的事务。
如果一个事务已经存在,则将这个存在的事务挂起。
5PROPAGATION_NOT_SUPPORTED: 总是非事务地执行,并挂起任何存在的事务。
6PROPAGATION_NEVER: 总是非事务地执行,如果存在一个活动事务,则抛出异常。
7PROPAGATION_NESTED:如果一个活动的事务存在,则运行在一个嵌套的事务中. 如果没有活动事务 则按 TransactionDefinition.PROPAGATION_REQUIRED 属性执行 i装配业务层 ltbean idquotuserServImplquot classquotcom.tarena.biz.impl.UserServImplquotgt lt-- IUserDao iuserDao new UserDaoImpl --gt ltproperty namequotiuserDaoquot refquotuserDaoImplquot/gt lt/beangt j测试业务层 k编写 web 层完成从 save.
jsp---gtUserActionregUser ---gtok 页面跳转成功 mspring 配置文件中完成表示层的装配 ltbean idquotuserActionquot classquotcom.tarena.web.action.UserActionquotgt lt-- new IUserServ iuserServ new UserServImpl --gt ltproperty namequotiuserServquot refquotuserServImplquot/gt lt/beangt n执行 SpringTest 测试 spring 配置文件是否装配成功4 统一测试 ssh 完成添加5 输入数据请使用正常数据6、spring Test 测试: 使用 spring Testjunit 测试: 1添加类库 sping 2.5.6 spring-test.java junit4.4 注意:版本兼容问题 2使用方法: //junit 采用 spring 提供测试方法测试 RunWithSpringJUnit4ClassRunner.class //spring 读取配置文件并获取 ApplicationContext 对象 ContextConfiguration locations quotclasspath:spring/applicationContext.xmlquot //自动注入 spring 容器获取到 UserDaoImpl 实例 //注意:不用提供测试方法 Autowired private IUserDao iuserDao 测试方法 Test //Ignore public void saveUser User user new User user.setUsernamequot小黑quot user.setPasswordquot1234quot int userIdiuserDao.saveUseruser System.out.printlnuserId 注解:1、后期维护比较困难,维护需要改变类文件 2、配置文件与源代码耦合在一起,更改框架难度比较大 3、注解版本更新与老版本不兼容 了解注解方式7、在 spring 配置文件中引入其他 spring 配置文件 目的:方便项目升级与维护 lt-- 引入其他 spring 配置文件 --gt applicationContext.xml: 放置所有的 spring 配置公共信息 ltimport resourcequotapplicationContext-web.xmlquot/gt 放置 struts2 的 action 对象 ltimport resourcequotapplicationContext-biz.xmlquot/gt 放置 业务层对象 ltimport resourcequotapplicationContext-dao.xmlquot/gt 放置所有的数据访问层对象8、spring 如何处理 hibernate 延时懒加载文件 lt-- spring 支持 hibernate 延时过滤器 --gt ltfiltergt ltfilter-namegtopenSessionInViewFilterlt/filter-namegt ltfilter-classgtorg.springframework.orm.hibernate3.support.OpenSessionInViewFilterlt/filter-classgt lt-- spring 连接工厂名字建议叫 sessionFactory 否则需要指定 sessionFactoryBeanName 名字, sessionFactoryBeanName 默认 sessionFactory --gt ltinit-paramgt ltparam-namegtsessionFactoryBeanNamelt/param-namegt ltparam-valuegtsessionFactorylt/param-valuegt lt/init-paramgt lt/filtergt ltfilter-mappinggt ltfilter-namegtopenSessionInViewFilterlt/filter-namegt lturl-patterngt/lt/url-patterngt lt/filter-mappinggt hibernate 延迟文件解决方案: 1、java web 项目开发 a自定义一个延时过滤器 bstruts2 可以使用过滤器、拦截器 c在 hibernate.hbm.xml配置文件中 添加 lazyquotfalsequot dHibernate.initializeobj e使用 spring 提供 openSessionInViewFilter9、spring 管理配置文件 a在 applicationContext.xml 文件注入属性管理类 ltbean idquotpropertyPlaceholderConfigurerquotclassquotorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerquotgt ltproperty namequotlocationsquotgt ltlistgt ltvaluegtclasspath:props/db.propertieslt/valuegt lt/listgt lt/propertygt lt/beangt b在 props 包下创建 db.properties 文件 并编写 keyvalue c在 xml 中使用key简介:spring 中文过滤器 使用在非 struts2 框架开发: org.springframework.web.filter.CharacterEncodingFilterltfiltergt ltfilter-namegtcharacterEncodingFilterlt/filter-namegt ltfilter-classgt org.springframework.web.filter.CharacterEncodingFilter lt/filter-classgt ltinit-paramgt ltparam-namegtencodinglt/param-namegt ltparam-valuegtUTF-8lt/param-valuegt lt/init-paramgt ltinit-paramgt ltparam-namegtforceEncodinglt/param-namegt ltparam-valuegttruelt/param-valuegt lt/init-paramgtlt/filtergtltfilter-mappinggt ltfilter-namegtcharacterEncodingFilterlt/filter-namegt lturl-patterngt/lt/url-patterngtlt/filter-mappinggt10、spring 中如何编写 HQL、QBC、SQL 语句 第一种方式: 查询 id:getHibernateTemplate.findhqlObject.get0 getHibernateTemplate.findhqlObject all:getHibernateTemplate.findhql 删除、修改 getHibernateTemplate.bulkUpdatehqlObject getHibernateTemplate.bulkUpdatehqlObject 第二种方式 查询所有 this.getHibernateTemplate.executeFindnew HibernateCallback Override public Object doInHibernateSession session throws HibernateException SQLException ... return query.list 根据 Id 查询 this.getHibernateTemplate.execute new HibernateCallback Override public Object doInHibernateSession session throws HibernateException SQLException ... return query.uniqueResult 删除、修改 Integer this.getHibernateTemplate.execute new HibernateCallback Override public Object doInHibernateSession session throws HibernateException SQLException ... return query.executeUpdate 添加 Integer this.getHibernateTemplate.
上一篇:
动态网站建设 doc
下一篇:
电咖汽车入选2018独角兽企业榜百强 发布“天际”品牌将推高估值