三大框架整合ssh(二)------DAO层 源码

一、最高层的DAO接口(ICommonDao.java)
public interface ICommonDao<T> {
	public void save(T t);
}


二、最高层的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);
	}

}


三、具体ElecText的DAO接口(IElecTextDao.java)

public interface IElecTextDao<ElecText> extends ICommonDao<ElecText>{
	public static final String SERVICENAME = "IElecTextDao";
}


四、具体ElecText的DAO接口的实现(ElecTextDaoImpl.java)

@Repository(IElecTextDao.SERVICENAME)
public class ElecTextDaoImpl extends CommonDaoImpl<ElecText> implements
		IElecTextDao<ElecText> {
}


五、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>


六、测试类(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里面加入下面语句。

<!-- hibernate与spring整合时,自动提交事务 hibernate与spring整合后,要记得加上这句话 -->
  <property name="hibernate.connection.autocommit">true</property>

 

至此DAO层写完,接下来是service层搭建。

下一篇的地址为:

三大框架整合ssh(三)-----service层

 


你可能感兴趣的:(框架,ssh,搭建,DAO层)