3대 프레임워크 통합 ssh(둘)------DAO층 원본 코드
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에 다음 문장을 추가합니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
node.js 파충류 프레임워크 node-crawler 첫 체험바이두 파충류라는 단어는 일반적으로 나타나는python 관련 자료이다. py에도 파충류 프레임이 많은데, 예를 들면 scrapy, Portia, Crawley 등이다. 이전에는 개인적으로 C#로 파충류를 만드는 것을...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.