SSH框架-DAO层搭建
SSH框架-DAO层搭建
1、工具类
1.1例如几个方法都会有相同功能的时候,需要定义一个公共接口类,也就是工具类。
package com.caicai.elec.dao; public interface ICommonDao<T> { public void save(T t); }
1.2 有了接口肯定要有实现类
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); } }
2、编写一个接口需要对哪张表操作
2.1 编写操作表接口
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"; }
2.2 实现这个接口
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 { }
3、编写Spring配置文件Beans.xml
3.1 配置beans.xml
<?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>
4、编写测试类
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); } }
文件框架
开开心心,上班!
快快乐乐,游玩!
及时行乐!