使用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;
}
}
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中的持久化上下文与Entity对象的状态
- 使用hibernate扩展工具Hbm2JavaTask根据配置文件生成持久化对象类(2.1.2)
- 持久化模式,第 1 部分: 现代 ORM 工具的策略和最佳实践,使用 Hibernate 特性在领域模型上应用面向对象原则
- Hibernate持久化对象三种状态的区分,以及save,update,saveOrUpdate,merge,persist等的使用
- 使用Hibernate模板持久化对象
- hibernate中使用session对象获得持久化对象get和load方法的区别
- Hibernate in action阅读笔记-吵吵冷饭-持久化对象
- Hibernate持久化对象的状态
- Hibernate中的持久化对象(精讲)
- hibernate持久化对象的状态
- Hibernate持久化对象的状态
- Hibernate持久化对象的生命周期
- 使用 hibernate 存取大对象数据类型(clob和blob)
- Hibernate--可持久化对象
- hibernate框架(3)---持久化对象
- Hibernate持久化对象的状态及状态之间的转换
- Hibernate(三)---通过Session操纵持久化对象
- Hibernate的持久化对象状态
- 【SSH框架】--Hibernate持久化对象的三种状态
- Hibernate——(5)持久化对象和一级缓存机制