(2)开发环境搭建-DAO层
来源:互联网 发布:总裁知错了txt下载 编辑:程序博客网 时间:2024/05/22 23:38
- 在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
- (2)开发环境搭建-DAO层
- (1)开发环境搭建-持久层
- (crm笔记1-2) Dao层的搭建
- php框架开发四(DAO层)
- Mybatis 开发dao层
- dao层开发代码
- ssm框架搭建(一)-Dao层搭建-mybatis
- Struts 2 + Hibernate + Spring 开发环境的搭建与数据访问Dao的生成
- Myeclipse8.6 快速搭建ssh(JPA)开发环境 并自动生成dao ,model
- 搭建DAO层和Service层代码
- Mybatis-Dao层开发之原始dao
- sping+hibernate Dao层开发
- web基础dao层开发
- MyBatis (二) Dao层开发
- MyBatis的Dao层开发
- DAO层小结2(查询)
- DAO层开发小结1(注释版)
- 开发自己的DAO层公共类(Spring+Hibernate)
- SVN的could not connect to server
- zmodem transfer canceled by remote side
- 霍夫曼树
- ROS 环境配置问题
- Activity的四种启动模式
- (2)开发环境搭建-DAO层
- MOS管栅极串联电阻的作用
- php判断字符串是否含有中文
- C. Alyona and the Tree
- js中怎么获取系统当前时间 格式 yyyy-MM-dd HH:MM:SS
- 1001. A+B Format
- POJ - 2349----Arctic Network(prim)
- linux读书摘要--正规表示法与文件格式化处理
- ListView里TextView使用ClickableSpan,ListView的Item无法点击的解决办法