使用Hibernate上下文会话持久化对象


使用Spring HIbernateTemplate可以方便进行持久化操作,但是,使用它就意味着Dao要依赖Spring的API。那么我们就可以用Hibernate上下文会话来持久化对象了。


1、配置数据源,事务

<?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:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.1.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">

	<context:annotation-config  />
	<context:component-scan base-package="main.java.com">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="url"
			value="jdbc:mysql://localhost:3306/usermanager?useUnicode=true&amp;characterEncoding=UTF-8">
		</property>
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="username" value="root"></property>
		<property name="password" value="liketing"></property>
		<!--连接池启动时的初始值-->
		<property name="initialSize" value="1" />
		<!--连接池最大值-->
		<property name="maxActive" value="2" />
		<!--最大空闲值-->
		<property name="maxIdle" value="1" />
		<!--最小空闲值-->
		<property name="minIdle" value="1" />
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">
					true
				</prop>
			</props>
		</property>
		<property name="packagesToScan" value="main.java.com.lkt.system.bean"></property>
	</bean>
	<!-- 事务 -->
	<tx:annotation-driven/>
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
</beans>

2、dao接口

3、dao实现 ,注入sessionFactory,使用sessionFactory.getCurrentSession()来获取当前上下文会话,来实现持久化

package main.java.com.lkt.dao;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Repository("sessionDaoImpl")
public class SessionDaoImpl<T> implements CommDao<T> {

	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Override
	public List<T> getByHql(String hql, Object... param) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<T> getAll(T obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public T getById(Serializable id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public T getByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Serializable del(T obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public Serializable add(T obj) {
		return getSessionFactory().getCurrentSession().save(obj);
	}

	@Override
	public Serializable update(T obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Serializable addBatch(List<T> objList) {
		// TODO Auto-generated method stub
		return null;
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值