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

  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值