Spring + JPA + Hibernate配置

阅读更多
<1>persistence.xml放到类路径下的META-INF下面

	
	

	


<2>applicationContext.xml配置


	
	
    
    
    

	
    
        
        
            
                
                
                
            
        
    

    
        
        
        
        
    
	
	

	 
    
        
    

	
    

	
 	
 	
	
		
	

	
	
	


<3>web.xml文件



    jpa_test

	
		contextConfigLocation
		
			classpath:applicationContext.xml
		
	
	
    
        
            org.springframework.web.context.ContextLoaderListener
        
    
	
	   
       
        encoding   
           
            org.springframework.web.filter.CharacterEncodingFilter   
           
           
            encoding   
            UTF-8   
           
       
       
       
        encoding   
        /*   
       

    
        index.jsp
    
	


<4>泛型dao定义
public interface GenericDao {
	public T save(T entity);

	public T update(T entity);

	public Integer updateBySql(final String sql);
	
	public void delete(T entity);
	
	public T findById(ID id);
	
	public List findByJPQL(String jpql);
	
	public List findBySQL(String sql);
	
	public List findAll();
	
	public int findRowCount();
}

public class GenericDaoImpl extends JpaDaoSupport
		implements GenericDao {

	private Class persistentClass;

	public GenericDaoImpl() {
		this.persistentClass = (Class) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
	}

	public Class getPersistentClass() {
		return persistentClass;
	}

	public T save(Object entity) {
		getJpaTemplate().persist(entity);
		return (T)entity;
	}

	public T update(Object entity) {
		getJpaTemplate().merge(entity);
		return (T)entity;
	}

	public Integer updateBySql(final String sql){
		return (Integer)getJpaTemplate().execute(new JpaCallback(){
			public Integer doInJpa(EntityManager em) throws PersistenceException{
				return em.createNativeQuery(sql).executeUpdate();
			}
		});
	}

	public void delete(Object entity) {
		getJpaTemplate().remove(entity);
	}
	
	public T findById(Serializable id) {
		return (T)getJpaTemplate().find(this.getPersistentClass(), id);
	}

         //要立即抓取时用"JOIN FETCH"
	public List findByJPQL(String jpql) {
		return getJpaTemplate().find(jpql);
	}

	/**
	 * 返回:list中装入的是对象数组(Object[])
	 */
	public List findBySQL(final String sql) {
		return  getJpaTemplate().executeFind(new JpaCallback(){
			public List doInJpa(EntityManager em) throws PersistenceException{
				return em.createNativeQuery(sql).getResultList();
			}
		});
	}

	public List findAll() {
		return getJpaTemplate().executeFind(new JpaCallback() {
			public Object doInJpa(EntityManager em) throws PersistenceException {
				StringBuffer jpql = new StringBuffer("from ");
				jpql.append(getPersistentClass().getName());
				jpql.append(" obj");
				return em.createQuery(jpql.toString()).getResultList();
			}
		});
	}

	
	public int findRowCount() {
		return ((Long) getJpaTemplate().execute(new JpaCallback() {
			public Object doInJpa(EntityManager em) throws PersistenceException {
				StringBuffer strBuff = new StringBuffer("select count(*) from ");
				strBuff.append(getPersistentClass().getName());
				return em.createQuery(strBuff.toString()).getResultList().get(0);
			}
		})).intValue();
	}
	
}
  • lib.rar (9.7 MB)
  • 描述: 库文件
  • 下载次数: 607
  • jpa_test.rar (13.8 KB)
  • 描述: 例子
  • 下载次数: 710

你可能感兴趣的:(JPA,Spring,Hibernate,配置管理,SQL)