您的位置:首页 > 编程语言 > Java开发

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

2014-04-07 00:24 417 查看
使用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&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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hibernate java string