(2)开发环境搭建-DAO层

来源:互联网 发布:总裁知错了txt下载 编辑:程序博客网 时间:2024/05/22 23:38
  1. 在com.itheima.elec.dao 中创建两个接口(公用接口和业务接口)
    1.1公用接口
public interface ICommonDao<T> {    void save(T entity);}

1.2业务接口(需要继承公共接口,并且制定泛型T所对应的对象

public interface ITestElecDao extends ICommonDao<TestElec> {    public static final String SERVICE_NAME = "com.itheima.elec.dao.impl.TestElecDaoImpl";}

2.在com.itheima.elex.dao.implement 中创建两个接口的实现类

2.1 公用类
(需要继承HibernateDaoSupport 这样可以方便使用HibernateTemplate对象)

public class CommonDaoImpl<T> extends HibernateDaoSupport implements        ICommonDao<T> {    // 注解    @Resource(name = "sessionFactory")    public void setDi(SessionFactory sessionFactory) {        this.setSessionFactory(sessionFactory);    }    public void save(T entity) {        this.getHibernateTemplate().save(entity);    }}

2.2 业务类
需要继承公用类,这样可以使用其中的方法

@Repository(ITestElecDao.SERVICE_NAME)public class TestElecDaoImpl extends CommonDaoImpl<TestElec> implements        ITestElecDao {}

2.3 在src中创建spring的配置文件(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-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/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">     <!-- 1:添加组件对注解的支持 -->    <context:component-scan base-package="com.itheima.elec"/>    <!-- 2:? -->    <!-- 3:spring整合hibernate的核心 -->    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="configLocation">            <value>                classpath:hibernate.cfg.xml            </value>        </property>    </bean>    <!-- 4:创建事务管理器 -->    <bean id="trManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <!-- 5:事务处理:注解,在Service层填写一个注解@Transcational -->    <tx:annotation-driven transaction-manager="trManager"/>    <!-- 事务处理:配置文件     <tx:advice id="aa" transaction-manager="trManager">        <tx:attributes>            <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>            <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>            <tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>            <tx:method name="*" read-only="true"/>        </tx:attributes>    </tx:advice>    <aop:config>        <aop:pointcut expression="execution(* com.itheima.elec.service..*.*(..))" id="bb"/>        <aop:advisor advice-ref="aa" pointcut-ref="bb"/>    </aop:config>    --></beans>

2.4 完成junit测试

public class TestDao {    @Test    public void save() {        // 加载spring 容器        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");        ITestElecDao testElecDao = (ITestElecDao) ac                .getBean(ITestElecDao.SERVICE_NAME);        TestElec et = new TestElec();        et.setTextName("爱情");        et.setTextDate(new Date());        et.setTextRemark("这是也一个测试");        testElecDao.save(et);    }}

注意:如果数据没有保存,需要设置事务自动提交:在hibernate.cfg.xml中添加:

<!-- 使用hibernate的方式提交事务(自动提交) -->        <property name="hibernate.connection.autocommit">true</property>
0 0
原创粉丝点击