프로젝트에서 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());
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
프로젝트 경험helm 노드의 탄성 신축, 클라우드 호스트에 대한 탄성 신축을 이용하여 알리 클라우드 로그 서비스 은 jenkins shell 또는 drone을 사용할 수 있습니다. 이것은 클라우드 서비스 업체가 제공하는 서비스입...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.