3대 프레임워크 통합 ssh(둘)------DAO층 원본 코드

1. 최고층 DAO 인터페이스(ICommonDao.java)
public interface ICommonDao<T> {
	public void save(T t);
}

2. 최고층 DAO 인터페이스의 실현(CommonDaoImpl.java)
public class CommonDaoImpl<T> extends HibernateDaoSupport implements
		ICommonDao<T> {

	@Override
	public void save(T t) {
		this.getHibernateTemplate().save(t);
	}

	@Resource(name = "sessionFactory")
	public void setSessionFactoryDI(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}

}

3. 구체적인 ElecText의 DAO 인터페이스(IElecTextDao.java)
public interface IElecTextDao<ElecText> extends ICommonDao<ElecText>{
	public static final String SERVICENAME = "IElecTextDao";
}

4. 구체적인 ElecText의 DAO 인터페이스의 실현(ElecTextDaoImpl.java)
@Repository(IElecTextDao.SERVICENAME)
public class ElecTextDaoImpl extends CommonDaoImpl<ElecText> implements
		IElecTextDao<ElecText> {
}

5.spring의 프로필(spring.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
	   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<!--           -->
	<context:component-scan base-package="com.evan"></context:component-scan>

	<!--   sessionFactory,  hibernate spring      -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
	</bean>

	<!--      spring   -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!--            -->
	<tx:annotation-driven transaction-manager="transactionManager" />

</beans>

6. 테스트 클래스(ElecTextDaoImplTest.java)
public class ElecTextDaoImplTest {

	@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
		@SuppressWarnings("unchecked")
		IElecTextDao<ElecText> IelecTextDao = (IElecTextDao<ElecText>) ac
				.getBean(IElecTextDao.SERVICENAME);

		ElecText elecText = new ElecText();
		elecText.setTextName("dao   ");
		elecText.setTextDate(new Date());
		elecText.setTextRemark("   spring hibernate    ,   dao     。      ");

		IelecTextDao.save(elecText);
	}

}

만약 위의 코드를 모두 쓴 후에 Junit 테스트로 통과했지만 데이터베이스에 새로 삽입된 데이터가 없다는 것을 발견할 수 있을 것이다.자동 제출을 설정하지 않았기 때문입니다.
hibernate에 있습니다.cfg.xml에 다음 문장을 추가합니다.

좋은 웹페이지 즐겨찾기