您的位置:首页 > 编程语言

用泛型技术对业务管理Bean抽象_实现_测试(代码示例)

2009-03-09 14:49 766 查看
DAO.java
package com.itcast.service.base;
public interface DAO {
/**
* 保存实体
* @param entity
*/
public void save(Object entity);
/**
* 更新实体
* @param entity
*/
public void update(Object entity);
/**
* 删除实体
* @param entityClass 实体类
* @param entityid 实体id
*/
public <T> void delete(Class<T> entityClass, Object entityid);
/**
* 删除实体
* @param entityClass 实体类
* @param entityids 实体id数组
*/
public <T> void delete(Class<T> entityClass, Object[] entityids);
/**
* 获取实体
* @param <T>
* @param entityClass 实体类
* @param entityId 实体id
* @return
*/
public <T> T find(Class<T> entityClass, Object entityId);
}

DaoSupport.java
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 {

/**
* 通过spring注入一个实体管理器
* JPA是通过 EnityManager进行数据库的操作,所以必须注入 EnityManager
* @PersistenceContext 注入EnityManager
*/
@PersistenceContext protected EntityManager em;

/**
* 1 AOP
* 2 EntityManager em = 实体赋值;
* 3 在调用此方法前,spring容器会根据Transactional属性默认行为方法开启一个事务
* 4 打开事务
*/
@Override
public <T> void delete(Class<T> entityClass, Object entityid) {
/**getReference取得实体的引用**/
delete(entityClass, new Object[]{entityid});
}
/** 5 关闭事务**/

@Override
public <T> void delete(Class<T> entityClass,Object[] entityids) {
for(Object id : entityids){
em.remove(em.getReference(entityClass, id));
}
}

/** 一般查询不需要事务处理,propagation=Propagation.NOT_SUPPORTED指示此方法不开启事务 **/
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
@Override
public <T> T find(Class<T> entityClass, Object entityId) {
return em.find(entityClass, entityId);
}

@Override
public void save(Object entity) {
em.persist(entity);
}

/** 一般在我们的实体bean变成游离状态的时候,调用此方法,对实体bean进行更改操作 **/
@Override
public void update(Object entity) {
//把实体bean数据从新同步到数据库
em.merge(entity);
}
}

ProductTypeService.java
package com.itcast.service.product;
import com.itcast.service.base.DAO;
public interface ProductTypeService extends DAO{
}

ProductTypeServiceBean.java
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;
//为了让bean纳入spring容器管理
@Service
@Transactional
public class ProductTypeServiceBean extends DaoSupport implements ProductTypeService {
//重载业务管理Bean的删除方法
@Override
public <T> void delete(Class<T> entityClass, Object[] entityids) {
StringBuffer jpql = new StringBuffer();
if(entityids!=null && entityids.length>0){
for(int i=0;i<entityids.length;i++){
jpql.append("?").append(i+2).append(",");
}
jpql.deleteCharAt(jpql.length()-1);

//createQuery()是JPQL查询的方法,不是本地查询(sql语句),JPQL语句是面向对象,jpa规范的查询语言
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();
}
}
}

ProductTest.java
package junit.test;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Assert;
import com.itcast.bean.product.ProductType;
import com.itcast.service.product.ProductTypeService;
public class ProductTest {
private static ApplicationContext cxt;
private static ProductTypeService productTypeService;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
/** 启动spring容器 **/
cxt = new ClassPathXmlApplicationContext("beans.xml");
/** 用接口去引用spring给我们返回的对象 **/
productTypeService = (ProductTypeService)cxt.getBean("productTypeServiceBean");
} catch (Exception e){
e.printStackTrace();
}
}

/*
* 添加单元测试点
* (@Test)
*/
@Test
public void testSave(){
ProductType type = new ProductType();
type.setName("篮球用品");
type.setNote("..产品");
productTypeService.save(type);
}

@Test
public void testFind(){
ProductType type = productTypeService.find(ProductType.class, 2);
org.junit.Assert.assertNotNull("获取不到id为2的记录", type);
}

@Test
public void testUpdate(){
/** 返回一个游离状态,因为业务逻辑方法执行完后,会关闭事务 **/
ProductType type = productTypeService.find(ProductType.class, 5);
type.setName("足球用品");
type.setNote("sd产品");
productTypeService.update(type);
}

@Test
public void testDelete(){
productTypeService.delete(ProductType.class, 2);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐