81,090
社区成员
发帖
与我相关
我的任务
分享
package com.jyjy.dao.impl;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
/**
* 文件:hibernateTemplateDao
* 说明:持久层所有实现类的父类,包含所有持久层实现类要使用的资源和公共方法
*
* 创建时间:2010-10-28
* 修改时间:2010-10-28
*/
@Repository("courseDao")//加上Annotation一切正常
public class SuperDao {
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
}
import org.springframework.stereotype.Repository;
import com.jyjy.dao.CourseDao;
import com.jyjy.model.Course;
import com.jyjy.util.Page;
/*
* 文件:CourseDaoImpl.java
* 说明:课程数据访问实现,从SuperDao继承避免多个Dao实现注入HibernateTemplate
*
* 创建日期:2010-10-28
* 修改时间:2010-10-28
*/
@Repository("courseDao")//加上Annotation一切正常
public class CourseDaoImpl extends SuperDao implements CourseDao {
@Override
public void delete(Course course) {
// TODO Auto-generated method stub
}
@Override
public boolean deleteByName(String name) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean existsName(String name) {
// TODO Auto-generated method stub
return false;
}
@Override
public Course loadByName(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Course> loadByPage(Course course) {
// TODO Auto-generated method stub
return null;
}
@Override
public int loadByPageGetCount(Page page) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void save(Course course) {
if (course != null) {
getHibernateTemplate().save(course);
}
}
@Override
public void update(Course course) {
// TODO Auto-generated method stub
}
}
以下是XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--
**文件:beans.xml
**
**说明:全部使用XML配置...
-->
<context:component-scan base-package="com.jyjy.dao" />
<tx:annotation-driven transaction-manager="txManager" />
<!-- 数据源的连接 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true" />
<!-- <property name="url" value="jdbc:mysql://localhost:3306/mis" />-->
<property name="username" value="root" />
<property name="password" value="xxf84340123" />
</bean>
<!-- 将数据源连接传给Hibernate 并对Hibernate进行一些初始化的工作-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.jyjy.model</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.current_session_context_class=thread
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>
<!-- 对Hibernate进行事务管理 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 这里定义的是Hibernate的模板 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 定义一个切面用于实现事务 -->
<!-- -->
<aop:config>
<aop:pointcut id="businessService"
expression="execution (public * com.jyjy.service.*.*(..))" />
<aop:advisor pointcut-ref="businessService" advice-ref="txAdvice" />
</aop:config>
<!-- 事务管理 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 所有Dao父类SuperDao的注入-->
<bean id ="superDao" class="com.jyjy.dao.impl.SuperDao" scope="prototype">
<property name="hibernateTemplate" ref="hibernateTemplate" ></property>
</bean>
<!-- CourseDao与CourseService的注入 -->
<bean id="courseDao" class="com.jyjy.dao.impl.CourseDaoImpl">
</bean>
<bean id="courseService" class="com.jyjy.service.impl.CourseServiceImpl">
<property name="courseDao" ref="courseDao"></property>
</bean>
<!-- 所有Dao父类SuperDao的注入-->
<bean id ="superDao" class="com.jyjy.dao.impl.SuperDao" scope="prototype">
<property name="hibernateTemplate" ref="hibernateTemplate" ></property>
</bean>
<!-- CourseDao与CourseService的注入 -->
<bean id="courseDao" class="com.jyjy.dao.impl.CourseDaoImpl">
</bean>
<bean id="courseService" class="com.jyjy.service.impl.CourseServiceImpl">
<property name="courseDao" ref="courseDao"></property>
</bean>