巴巴巴運動網(18--20)は汎用技術で製品分類の業務管理Bean抽象、テスト、重荷
5166 ワード
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 void delete(Class entityClass,Object entityid);
/**
*
* @param entityids id 。
*/
public void delete(Class entityClass,Object[] entityids);
/**
*
* @param entityClass
* @param entityid id
* @return
*/
public T find(Class 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 void delete(Class entityClass,Object entityid) {
delete(entityClass,new Object[]{entityid});
// em.remove(em.getReference(entityClass, entityid));
}
@Override
public void delete(Class 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 find(Class 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 void delete(Class 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.