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.