使用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: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&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>
</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;
	}
	

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值