package com.caicai.elec.dao; public interface ICommonDao<T> { public void save(T t); }
package com.caicai.elec.dao.impl; import com.caicai.elec.dao.ICommonDao; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import javax.annotation.Resource; //一般要使用模板,直接继承HibernateDaoSupport //HibernateDaoSupport 是Spring 提供的一个包含增、删、改、查一系列操作,所以需要继承它 public class ICommonDaoImpl<T> extends HibernateDaoSupport implements ICommonDao<T> { @Override public void save(T t) { this.getHibernateTemplate().save(t); } //注入SessionFactory @Resource(name="sessionFactory") public final void setSessionFactory1(SessionFactory sessionFactory) { //调用父类的 setSessionFactory super.setSessionFactory(sessionFactory); } }
package com.caicai.elec.dao; import com.caicai.elec.doamin.ElecText; //继承工具类 public interface IElecTextDao extends ICommonDao<ElecText> { public final static String serivicename="com.caicai.dao.impl.IElecTextDaoImpl"; }
package com.caicai.elec.dao.impl; import com.caicai.elec.dao.IElecTextDao; import com.caicai.elec.doamin.ElecText; import org.springframework.stereotype.Repository; //解释一下,为什么这个我们是这么写,应为这里我们是面向接口的,所以需要这么,如果面向对象,我们可以直接注入形式写
//例如:@Repository("com.caicai.dao.impl.IElecTextDaoImpl")
@Repository(IElecTextDao.serivicename) public class IElecTextDaoImpl extends ICommonDaoImpl<ElecText> implements IElecTextDao { }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!--1、配置注解扫描范围--> <context:component-scan base-package="com.caicai.elec"></context:component-scan> <!--2、配置数据源--> <!--3、创建SessionFactory,Spring 整合hibernate入口--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!--找到hibernate配置文件路径--> <property name="configLocation"> <value> classpath:hibernate.cfg.xml </value> </property> </bean> <!-- 创建事务管理器--> <bean id="txManage" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!-- 将SessionFactory 注入到事务中--> <!--ref 对应上面的ID sessionFactory --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 5、以注解的形式管理事务 --> <tx:annotation-driven transaction-manager="txManage"/> </beans>
package junit; import com.caicai.elec.dao.IElecTextDao; import com.caicai.elec.doamin.ElecText; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.Date; public class TestDao { @Test public void TestSave(){ //获取配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); IElecTextDao iElecTextDao = (IElecTextDao)applicationContext.getBean(IElecTextDao.serivicename); //实例化po 对象,赋值、保存 ElecText elecText = new ElecText(); elecText.setTextName("this is test dao save2"); elecText.setTextDate(new Date()); elecText.setTextRemark("test dao remark1"); iElecTextDao.save(elecText); } }
原文:https://www.cnblogs.com/caicai920/p/14674560.html