- 1
- 0
- 约7.35千字
- 约 17页
- 2016-12-09 发布于河南
- 举报
09_应用细解析:strutshibernate示例
Hibernate与Struts集成 教学内容 在模型层使用业务代理模式 模型层通过Hibernate完成业务数据的持久化 业务代理工厂类负责构造业务代理类实例 在Struts Action中访问业务代理类的业务方法 采用MVC框架的应用的各个层次之间的依赖关系 模型应该和视图以及控制器之间保持独立。在分层的框架结构中,位于上层的视图和控制器依赖于下层模型,而下层模型不应该依赖于上层的视图和控制器。 模型层采用业务代理模式 客户层通过业务代理接口访问模型,当模型的实现发生变化,只要业务代理接口不变,就不会影响客户层中访问模型层的程序代码。 netstore应用的分层结构 netstore应用的分层结构 netstore应用的业务代理接口为INetstoreService,它有两种实现方式: 方式一:由业务代理实现类NetstoreServiceImpl来实现业务逻辑,它通过Hibernate API访问数据库。 方式二:采用无状态会话EJB组件来实现业务逻辑。EJB组件通过Hibernate API访问数据库,业务代理实现类NetstoreEJBFromFactoryDelegate调用EJB组件方法来为控制层提供服务。 业务代理接口INetstoreService public interface INetstoreService extends IAuthentication { /** 批量检索Item对象,beginIndex参数指定查询结果的起始位置,length指定检索的Item 对象的数目。对于Item对象的所有集合属性,都使用延迟检索策略 */ public List getItems(int beginIndex,int length) throws DatastoreException; /** 根据id加载Item对象 */ public Item getItemById( Long id ) throws DatastoreException; /** 根据id加载Customer对象,对于Customer对象的orders属性,显式采用迫切左外连接检 索策略 */ public Customer getCustomerById( Long id ) throws DatastoreException; /** 保存或者更新Customer对象,并且级联保存或更新它的orders集合中的Order对象 */ public void saveOrUpdateCustomer(Customer customer ) throws DatastoreException; /** 保存订单 */ public void saveOrder(Order order) throws DatastoreException; public void setServletContext( ServletContext ctx ); public void destroy(); } IAuthentication接口 public interface IAuthentication { /** 登出Web应用 */ public void logout(String email); /** 根据客户的email和password验证身份,如果验证成功,返回 匹配的Customer对象,它的orders集合属性采用延迟检索策略, 不会被初始化 */ public Customer authenticate(String email, String password) throws InvalidLoginException, ExpiredPasswordException,AccountLockedException, DatastoreException; } NetstoreServiceImpl的authenticate()方法 tx = session.beginTransaction(); //对与Customer关联的Order对象采用延迟检索策略 Query query = session.createQuery(from Customer c where c.email=:email and c.password=:password); query.setString(email,email); query.setString(password,password); List
原创力文档

文档评论(0)