使用Hibernate模板持久化对象
2014-04-06 23:35
274 查看
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: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">
<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>
</beans>
2、接口
package main.java.com.lkt.dao;
import java.io.Serializable;
import java.util.List;
public interface CommDao<T> {
/**
* 获取所有
* @return
*/
public List<T> getAll(T obj);
/**
* 根据Id获取
* @param id
* @return
*/
public T getById(Serializable id);
/**
* 根据名称获取
* @param name
* @return
*/
public T getByName(String name);
/**
* 删除
* @param obj
* @return
*/
public Serializable del(T obj);
/**
* 添加
* @param obj
* @return
*/
public Serializable add(T obj);
/**
* 修改
* @param obj
* @return
*/
public Serializable update(T obj);
/**
* 批量添加
* @param objList
* @return
*/
public Serializable addBatch(final List<T> objList);
}
3、实现,继承HibernateDaoSupport,注入sessionFactory,使用getHibernateTemplate()获取Hibernate模板
package main.java.com.lkt.dao;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
@Repository("hibernateDaoImpl")
public class HibernateDaoImpl<T> extends HibernateDaoSupport implements CommDao<T> {
@Resource(name="sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
@Override
public List<T> getByHql(String hql, Object... param) {
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;
}
@Override
public Serializable add(T obj) {
return getHibernateTemplate().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;
}
}
<?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: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">
<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>
</beans>
2、接口
package main.java.com.lkt.dao;
import java.io.Serializable;
import java.util.List;
public interface CommDao<T> {
/**
* 获取所有
* @return
*/
public List<T> getAll(T obj);
/**
* 根据Id获取
* @param id
* @return
*/
public T getById(Serializable id);
/**
* 根据名称获取
* @param name
* @return
*/
public T getByName(String name);
/**
* 删除
* @param obj
* @return
*/
public Serializable del(T obj);
/**
* 添加
* @param obj
* @return
*/
public Serializable add(T obj);
/**
* 修改
* @param obj
* @return
*/
public Serializable update(T obj);
/**
* 批量添加
* @param objList
* @return
*/
public Serializable addBatch(final List<T> objList);
}
3、实现,继承HibernateDaoSupport,注入sessionFactory,使用getHibernateTemplate()获取Hibernate模板
package main.java.com.lkt.dao;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
@Repository("hibernateDaoImpl")
public class HibernateDaoImpl<T> extends HibernateDaoSupport implements CommDao<T> {
@Resource(name="sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
@Override
public List<T> getByHql(String hql, Object... param) {
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;
}
@Override
public Serializable add(T obj) {
return getHibernateTemplate().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上下文会话持久化对象
- Hibernate持久化对象三种状态的区分,以及save,update,saveOrUpdate,merge,persist等的使用
- 持久化模式,第 1 部分: 现代 ORM 工具的策略和最佳实践,使用 Hibernate 特性在领域模型上应用面向对象原则
- 使用hibernate扩展工具Hbm2JavaTask根据配置文件生成持久化对象类(2.1.2)
- hibernate中使用session对象获得持久化对象get和load方法的区别
- Hibernate持久化对象的三种状态
- Hibernate的学习之路十(持久化对象的三个状态)
- hibernate的持久化对象的状态,n+1问题和load和get方法区别
- Ruby on Rails,使用find方法加载持久化对象
- hibernate中持久化对象的生命周期(三态:自由态,持久态,游离态 之间的转换)
- Ruby on Rails,使用where方法对持久化对象进行条件查询
- 【SSH框架】--Hibernate持久化对象的三种状态
- Hibernate持久化对象的状态及状态之间的转换
- hibernate 持久化对象 session中的方法
- Hibernate基础使用:CRUD方法及实体对象的三种状态
- 使用VS2010的Database项目模板统一管理数“.NET研究”据库对象
- Entity Framework 4 in Action读书笔记——第七章:持久化对象到数据库:使用SaveChanges持久化实体
- hibernate 深入理解持久化对象
- hibernate中使用sql返回结果转为非持久化对象
- 自己扩展封装Hibernate模板DAO-之二单独使用(hibernate)