巴巴运动网 (18--20) 用泛型技术对产品分类的业务管理Bean抽象,测试,重载
2012-07-01 21:23
465 查看
package com.itcast.service.base; public interface DAO { /** * 保存实体 * @param entity 实体id */ public void save(Object entity); /** * 更新实体 * @param entity 实体id */ public void update(Object entity); /** * 删除实体 * @param entityid 实体id */ public <T> void delete(Class<T> entityClass,Object entityid); /** * 删除实体 * @param entityids 实体id 数组。 */ public <T> void delete(Class<T> entityClass,Object[] entityids); /** * 获取实体 * @param entityClass 实体类 * @param entityid 实体id * @return */ public <T> T find(Class<T> entityClass, Object entityid); }
业务逻辑接口实现类:
package com.itcast.service.base; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; // 事务管理。 默认的事务行为。 @Transactional public abstract class DaoSupport implements DAO { // 注入实体管理器: @PersistenceContext protected EntityManager em; @Override public <T> void delete(Class<T> entityClass,Object entityid) { delete(entityClass,new Object[]{entityid}); // em.remove(em.getReference(entityClass, entityid)); } @Override public <T> void delete(Class<T> entityClass,Object[] entityids) { for(Object id : entityids){ // 取得实体的托管对象 em.getReference(arg0, arg1) em.remove(em.getReference(entityClass, id)); } } // 修改事物。1,只能读。2,修改事物传播行为:不需要开事物的。 @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED) public <T> T find(Class<T> entityClass, Object entityid) { return em.find(entityClass, entityid); } @Override public void save(Object entity) { em.persist(entity); } @Override public void update(Object entity) { // 当我们的实体bean 变成游离状态的时候,进行 更新。 em.merge(entity); } }
package com.itcast.service.product; import com.itcast.service.base.DAO; public interface ProductTypeService extends DAO{ }
package com.itcast.service.product.impl; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.itcast.service.base.DaoSupport; import com.itcast.service.product.ProductTypeService; @Service @Transactional public class ProductTypeServiceBean extends DaoSupport implements ProductTypeService { }
测试类:
package junit.test; import junit.framework.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.itcast.bean.ProductType; import com.itcast.service.product.ProductTypeService; public class ProductTest { private static ApplicationContext cxt; private static ProductTypeService productService; @BeforeClass public static void setUpBeforeClass() throws Exception { try { cxt = new ClassPathXmlApplicationContext("beans.xml"); productService = (ProductTypeService) cxt.getBean("productTypeServiceBean"); } catch (Exception e) { e.printStackTrace(); } } @Test public void testSave() { ProductType type = new ProductType(); type.setName("ttt"); type.setNote("good,ttt"); productService.save(type); } @Test public void testFind() { ProductType type = productService.find(ProductType.class, 1); Assert.assertNotNull("获取不到id为1 的记录",type); } @Test public void testUpdate() { ProductType type = productService.find(ProductType.class, 1); type.setName("zuqiu"); type.setNote("good,zuqiu"); productService.update(type); } @Test // 物理删除。 public void testDetele() { productService.delete(ProductType.class, 1); } }
经过测试,可以正常使用。
保存方法:
查找方法:
更新方法:
20节:重载业务管理Bean的删除方法----这个不是物理删除。
需知:
【1】 visible=?1 是位置参数。
【2】 .setParameter(1, false); 1为位置编号(也就是 visible=?1 的 1),false 所设定的参数值是什么。
【3】 o.typeid 为主键id
package com.itcast.service.product.impl; import javax.persistence.Query; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.itcast.service.base.DaoSupport; import com.itcast.service.product.ProductTypeService; @Service @Transactional public class ProductTypeServiceBean extends DaoSupport implements ProductTypeService { @Override public <T> void delete(Class<T> entityClass, Object[] entityids) { if (entityids!=null && entityids.length>0) { StringBuffer jpql = new StringBuffer(); for(int i=0; i < entityids.length;i++){ jpql.append("?").append(i+2).append(","); } // 组拼完会多一个逗号,所以要删除这个逗号。也就是最后一个字符。 jpql.deleteCharAt(jpql.length()-1); // ?2, ?3 , ?4 Query query = em.createQuery( "update ProductType o set o.visible=?1 where o.typeid in(" + jpql.toString() + ")").setParameter(1, false); // 对参数进行 设定。 for(int i=0; i < entityids.length;i++){ query.setParameter(i+2, entityids[i]); } query.executeUpdate(); // 更新一下。 } } }
配置文件:可以参考
巴巴运动网 16.
相关文章推荐
- 巴巴运动网学习笔记三之产品分类实体对象基本属性的JPA映射,用泛型技术对产品分类的业务管理Bean抽象,单元测试产品分类的业务管理Bean.,重载业务管理Bean的删除方法,
- 巴巴运动网-用泛型技术对产品分类的业务管理Bean抽象
- 用泛型技术对业务管理Bean抽象_实现_测试(代码示例)
- 敏捷开发产品管理系列之八:基于业务设计技术架构(兼谈12306性能问题)
- 敏捷开发产品管理系列之八:基于业务设计技术架构(兼谈12306性能问题)
- [软件项目管理与测试论坛]技术是什么?技术永远是为产品服务
- 敏捷开发产品管理系列之八:基于业务设计技术架构(兼谈12306性能问题)
- 8、对产品类别的业务管理分析
- CTO这点事(技术,业务,管理,情商,周期,趋势)转
- [61] 测试技术常见的十一种问题之九:没有产品说明书和需求文档地情况下能够进行黑盒测试吗?
- Web API项目中使用Area对业务进行分类管理
- 项目开发管理技术之项目版本控制、软件建模、软件测试、项目文档管理(转)
- 【性能测试思想】性能测试技术的研究_关于性能测试业务场景设计的研究
- Java中在特定区间产生随机数 (2009-03-26 13:31:49)转载▼ 标签: 杂谈 生成指定范围内的随机数 这个是最常用的技术之一。程序员希望通过随机数的方式来处理众多的业务逻辑,测试
- IT职场人生系列之十三:技术?管理?业务?
- 《BREW进阶与精通——3G移动增值业务的运营、定制与开发》连载之96——BREW中运营商管理的测试UBT
- 巴巴运动网 16 (产品分类实体对象基本属性的JPA映射)
- 互联网产品研发管理-技术商人的能力
- 高级软件测试技术(测试管理工具实践day3)
- 《打造高效率的产品测试体系--产品测试管理》深圳开讲 --2013年4月19~20日, 欢迎报名!