프로젝트에서 DAO의 구조적 실현


 
먼저 기본 DAO 인터페이스를 정의합니다.
 
 
package com.tch.test.ssh.dao;

import java.io.Serializable;
import java.util.List;

public interface BaseDao {
	/**
	 *  Created on 2013-9-16 
	 * 

DiscripEion:

* @reEurn void */ void save(E entity); /** * Created on 2013-9-16 *

DiscripEion:

* @reEurn void */ void update(E entity); /** * Created on 2013-9-16 *

DiscripEion:

* @reEurn void */ void delete(E entity); /** * CreaEed on 2013-9-16 *

DiscripEion: id

* @reEurn void */ E get(Serializable id); /** * Created on 2013-9-16 *

DiscripEion:

* @reEurn void */ List getAll(); }

 
 
그리고 sessionFactory의 기본 클래스를 구성합니다.
 
package com.tch.test.ssh.dao;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class CommomDao extends HibernateDaoSupport{

	@Resource(name="sessionFactory")//        setSessionFactory()   final ,    ,         ,          setSessionFactory()  
	public void setSuperSessionFactory(SessionFactory sessionFactory){
		this.setSessionFactory(sessionFactory);
	}
	
}

 
엔티티 클래스 및 맵 파일:
 
 
package com.tch.test.ssh.entity;
// default package

import java.util.Date;


/**
 * MyTime entity. @author MyEclipse Persistence Tools
 */

public class MyTime  implements java.io.Serializable {


    // Fields    

     private static final long serialVersionUID = 1L;
     private Integer id;
     private Date addTime;


    // Constructors

    /** default constructor */
    public MyTime() {
    }

    
    /** full constructor */
    public MyTime(Date addTime) {
        this.addTime = addTime;
    }

   
    // Property accessors

    public Integer getId() {
        return this.id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }

    public Date getAddTime() {
        return this.addTime;
    }
    
    public void setAddTime(Date addTime) {
        this.addTime = addTime;
    }

}

 
 




    
        
            
            
        
        
            
        
    


 
다음은 기본 DAO 구현 클래스입니다.
 
 
package com.tch.test.ssh.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

public class BaseDaoImpl extends CommomDao implements BaseDao{

	private Class clazz;
	
	@SuppressWarnings("unchecked")
	public BaseDaoImpl(){
		this.clazz = (Class)((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];//               
	}
	
	@Override
	public void delete(E entity) {
		getHibernateTemplate().delete(entity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public E get(Serializable id) {
		return (E)getHibernateTemplate().get(clazz, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List getAll() {
		return getHibernateTemplate().loadAll(clazz);
		/*String hql = "from "+clazz.getName();
		System.out.println("hql: "+hql);
		return getSession().createQuery(hql).list();*/
	}

	@Override
	public void save(E entity) {
		getHibernateTemplate().save(entity);
	}

	@Override
	public void update(E entity) {
		getHibernateTemplate().update(entity);
	}

}

 
다음은 인터페이스 데모입니다.
 
 
 
package com.tch.test.ssh.dao;

import com.tch.test.ssh.entity.MyTime;

public interface TimeDao extends BaseDao{

	
}

 
 
구현 클래스 프레젠테이션(BaseDaoImpl 클래스를 계승하기만 하면 기본적인 삭제 및 수정 방법이 포함되어 있음):
 
 
 
package com.tch.test.ssh.dao;

import org.springframework.stereotype.Repository;

import com.tch.test.ssh.entity.MyTime;
@Repository("TimeDao")
public class TimerDaoImpl extends BaseDaoImpl implements TimeDao{

}

 
 
최근에 응용 프로그램 Context를 붙였습니다.xml 내용:
 

	
	
	
	
	
	
	
	
		
		
		
		
	
	
	
		
	
	
	
		
		
			 classpath:com/tch/test/ssh/entity/*.hbm.xml
		
		
			
				true
			
		
	

 
테스트 클래스 추가:
 
package test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tch.test.ssh.dao.TimeDao;
import com.tch.test.ssh.entity.MyTime;

public class TestSpring {

	public static void main(String[] args) throws Exception {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		TimeDao dao = (TimeDao) context.getBean("TimeDao");
		List entities = dao.getAll();
		for(MyTime entity:entities){
			System.out.println(entity.getId()+" , "+entity.getAddTime());
		}
	}
	
}

 
 

좋은 웹페이지 즐겨찾기