1、定义接口
package cn.joeho.base;
import java.io.Serializable;
public interface DAO<T> {
public String getSubClass();
public Class<T> getEntityClass();
public void save(T entity);
public void update(T entity);
public void delete(Serializable id);
public T find(Serializable id);
public long getCount();
}
2、接口的实现类
package cn.joeho.base;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import javax.persistence.Entity;
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<T> implements DAO<T> {
@PersistenceContext protected EntityManager em;
protected Class<T> entityClass=getEntityClass();
@Override
public String getSubClass(){
return super.getClass().getName();
}
@Override
public Class<T> getEntityClass(){
Type parentType =getClass().getGenericSuperclass();
if(parentType instanceof ParameterizedType){
ParameterizedType type=(ParameterizedType)parentType;
return (Class<T>)type.getActualTypeArguments()[0];
}
return null;
}
public String getEntityName(){
String entityName=this.entityClass.getSimpleName();
Entity entity=this.entityClass.getAnnotation(Entity.class);
String ename=entity.name();
if(ename!=null&&!"".equals(ename)){
entityName=ename;
}
return entityName;
}
@Override
public void save(T entity) {
em.persist(entity);
}
@Override
public void update(T entity) {
em.merge(entity);
}
@Override
public void delete(Serializable id) {
em.remove(em.getReference(entityClass, id));
}
@Override
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public T find(Serializable id) {
return em.find(entityClass, id);
}
@Override
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public long getCount() {
return (Long) em.createQuery("select count(o) from "+getEntityName()+" o").getSingleResult();
}
}
3、业务操作类接口
package cn.joeho.service;
import cn.joeho.base.DAO;
import cn.joeho.entity.Buyer;
public interface BuyerService extends DAO<Buyer>{
}
4、业务操作类实现
package cn.joeho.service.impl;
import org.springframework.stereotype.Service;
import cn.joeho.base.DaoSupport;
import cn.joeho.entity.Buyer;
import cn.joeho.service.BuyerService;
@Service
public class BuyerServiceBean extends DaoSupport<Buyer> implements BuyerService {
}
5、测试类
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.joeho.entity.Buyer;
import cn.joeho.service.BuyerService;
public class BuyerTest {
public static ApplicationContext act =null;
public static BuyerService buyerService = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
act = new ClassPathXmlApplicationContext("beans.xml");
buyerService=(BuyerService)act.getBean("buyerServiceBean");
}
@Test
public void aaa(){
System.out.println(buyerService.getSubClass());
}
@Test
public void save(){
Buyer buyer=new Buyer();
buyer.setName("xupeng1");
buyer.setPassword("123344");
buyer.setEmail("xupeng@hotmail.com");
buyerService.save(buyer);
}
@Test
public void update(){
Buyer buyer=new Buyer();
buyer.setName("lisi");
buyer.setPassword("123456");
buyer.setEmail("xupeng@hotmail.com");
buyerService.update(buyer);
}
@Test
public void delete(){
buyerService.delete("xupeng1");
}
@Test
public void find(){
Buyer buyer=buyerService.find("xupeng");
System.out.println(buyer.getName()+"="+buyer.getEmail());
}
@Test
public void count(){
System.out.println(buyerService.getCount());
}
}