BaseDao基于JPA的实现

一,说明

基本DAO的实现也是基于上一篇文章,Spring3+Hibernate4+JPA2.0整合之后,DAO层的实现

二,DAO层的功能

      这里要简单提一下MVC模式了,MVC本身不是一种设计模式,只是在程序开发过程中逐渐形成的一种模式,也可以说是一种结构,它的目的就是为了分层解耦。

      spring框架也有自己的MVC开发模式,spring是面向接口编程(切面),View层调用Controller,Controller调用Service,而Service又调用数据访问层,Model即实体类

      更加详细的介绍可以参考这篇文章,应该是比较循序渐进的入门级的http://jinnianshilongnian.iteye.com/blog/1594806。如果英文比较好,建议直接读英文API文档

      DAO接口代码:

public interface DAO {

	public void save(Object entity);
	
	public void update(Object entity);
	
	public  void delete(Class entityClass,Object entityid);
	
	public  void delete(Class entityClass,Object[] entityids);
	
	public  T find(Class entityClass,Object entityid);
	
	/**
	 * 获取分页数据的封装
	 * @param 
	 * @param entityClass 实体类
	 * @param firstindex 开始索引
	 * @param maxresult 需要获取的记录数
	 * @return
	 */
	public  QueryResult getScrollData(Class entityClass,int firstindex,int maxresult);
	
	/**
	 * 带排序功能的分页实现
	 * @param 
	 * @param entityClass
	 * @param firstindex
	 * @param maxresult
	 * @param orderby 按照添加的key的顺序排序
	 * @return
	 */
	public  QueryResult getScrollData(Class entityClass,int firstindex,int maxresult,LinkedHashMap orderby);
	
	/**
	 * 带排序功能的分页实现
	 * @param 
	 * @param entityClass
	 * @param firstindex
	 * @param maxresult
	 * @param wheresql 添加限制条件
	 * @param orderby
	 * @return
	 */
	public  QueryResult getScrollData(Class entityClass,int firstindex,int maxresult,String wheresql,Object[] queryParams,LinkedHashMap orderby);
	
}


 

DAO实现层代码:

@Transactional
public abstract class DaoSupport implements DAO {

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

	@Override
	public void update(Object entity) {
		em.merge(entity);
	}

	@Override
	public  void delete(Class entityClass, Object entityid) {
		delete(entityClass, new Object[]{entityid});
	}

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

	@Override
	public  T find(Class entityClass, Object entityid) {
		return em.find(entityClass, entityid);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public  QueryResult getScrollData(Class entityClass,
			int firstindex, int maxresult) {
		QueryResult qr = new QueryResult();
		String entityname = getEntityName(entityClass);
		Query query = em.createQuery("select o from "+entityname+" o ");
		query.setFirstResult(firstindex).setMaxResults(maxresult);
		qr.setResultlist(query.getResultList());
		query = em.createQuery("select count(o) from " + entityname + " o ");
		qr.setTotalrecord((Long)query.getSingleResult());
		return qr;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public  QueryResult getScrollData(Class entityClass,
			int firstindex, int maxresult,String wheresql,Object[] queryParams, LinkedHashMap orderby) {
		QueryResult qr = new QueryResult();
		String entityname = getEntityName(entityClass);
		Query query = em.createQuery("select o from "+entityname+" o " + (wheresql == null ? "" : " where " + wheresql) + buildOrderby(orderby));
		setQueryParams(query,queryParams);
		query.setFirstResult(firstindex).setMaxResults(maxresult);
		List list = query.getResultList();
		qr.setResultlist(list);
		qr.setTotalrecord(list.size());
		return qr;
	}
	
	protected void setQueryParams(Query query,Object[] queryParams) {
		if(null != queryParams && 0 < queryParams.length) {
			for (int i = 0; i < queryParams.length; i++) {
				query.setParameter(i+1, queryParams[i]);
			}
		}
	}
	
	@Override
	public  QueryResult getScrollData(Class entityClass,
			int firstindex, int maxresult, LinkedHashMap orderby) {
		return getScrollData(entityClass, firstindex, maxresult, null,null, orderby);
	}
	
	protected String buildOrderby(LinkedHashMap orderby) {
		StringBuffer sb = new StringBuffer();
		if(null != orderby && 0 != orderby.size()) {
			sb.append(" order by ");
			for (String key : orderby.keySet()) {
				sb.append("o.").append(key).append(" ").append(orderby.get(key)).append(",");
			}
			sb.deleteCharAt(sb.length() - 1);
		}
		return sb.toString();
	}
	
	/**
	 * 获取实体的名称
	 * @param 
	 * @param entityClass 实体类
	 * @return
	 */
	protected  String getEntityName(Class entityClass) {
		String entityname = entityClass.getSimpleName();
		Entity entity = entityClass.getAnnotation(Entity.class);
		if(entity.name() != null && !"".equals(entity.name())) {
			entityname = entity.name();
		}
		return entityname;
	}

}


三,添加Service层

public interface JMusicService extends DAO {

}


 

Service实现

@Service(value="jMusicServiceBean")
@Transactional
public class JMusicServiceBean extends DaoSupport implements JMusicService {

	
	
}


 

可以看到,dao层利用泛型的方式来实现,使Service层代码非常简单,但是,这里没有利用依赖注入的方式将dao层和Service层分离,后续中有待处理

四,测试dao层的实现

public class SpringJpaTest {

	private static ApplicationContext cxt;
	private static JMusicService jmService;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		//WebRoot/WEB-INF/
		cxt=  new ClassPathXmlApplicationContext("spring-mvc.xml");
		jmService = (JMusicService) cxt.getBean("jMusicServiceBean");
	}
	
	@Test
	public void testsave() {
		
		for(int i = 0;i < 15;i++) {
			JMusic jm = new JMusic();
			jm.setAlbum("BigBun");
			jm.setDistributeDate(Timestamp.valueOf(DateUtil.getNowDate()));
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			jm.setMusicname(uuid.substring(3, 10));
			jm.setMusicurl("http://www.baidu.com/music/20150308/"+uuid.substring(3, 10)+".mp3");
			jm.setSinger("Johns Brooks");
			jmService.save(jm);
		}
	}
	
	@Test
	public void testfind() throws UnsupportedEncodingException {
		JMusic jm = jmService.find(JMusic.class, 1000);
		if(null != jm)
			System.out.println(jm.getSinger());
	}
	
	
	@Test
	public void testdelete () {
		jmService.delete(JMusic.class, 1);
		jmService.delete(JMusic.class, 2);
	}
	/***
	 * 测试分页
	 */
	@Test
	public void testPages() {
		QueryResult qr = jmService.getScrollData(JMusic.class, 11, 10);
		for(JMusic jm : qr.getResultlist()) {
			System.out.println(jm.getId());
		}
	}
	
	/***
	 * 测试带有排序的分页
	 */
	@Test
	public void testOrderPages() {
		LinkedHashMap orderby = new LinkedHashMap();
		orderby.put("musicname", "desc");
		orderby.put("id", "asc");
		//JPA占位符
		QueryResult qr = jmService.getScrollData(JMusic.class, 0, 100," singer=?1",new Object[]{"Jay"},orderby);
		for(JMusic jm : qr.getResultlist()) {
			System.out.println(jm.getMusicname() + "-" + jm.getId());
		}
	}

}

这里可以测试一下,增删改查的各个功能,还有各种分页的效果的测试

你可能感兴趣的:(J2EE,spring,hibernate,jpa)