Spring 整合Hibernate

Hibernate是常用的ORM(Object Relation Mapping)框架,使用Hibernate能够从对象的角度操作关系型数据库。

Spring框架提供了Hibernate的抽象层,可以整合Hibernate框架,从而使得应用中不仅可以使用Hibernate的ORM特性,又能使用Spring框架的其他服务。


Hibernate框架的核心是Session对象,Session对象需要使用SessionFactory获得。

使用Spring框架整合Hibernate框架的第一个步骤,就是可以使用IoC容器管理装配SessionFactory实例。

Spring框架中提供了LocalSessionFactoryBean类,用来创建Hibernate框架中的SessionFactory实例。


LocalSessionFactoryBean类中常用的setter方法:

1、setDataSource(DataSource dataSource)

任何一个LocalSessionFactoryBean实例都需要指定一个DataSource,作为获得数据库连接的数据源对象,可以使用各种连接池组件,如dbcp、C3P0等。

2、setConfigLocation(Resource configLocation)

获得Hibernate框架的SessionFactory对象,需要依赖Hibernate框架的配置文件,属性configLocation可以用来指定Hibernate配置文件位置。

例如,可以指定为“classpath:hibernate.cfg.xml”。

3、setConfigLocations(Resource[] configLocations)

如果存在多个Hibernate配置文件,就可以通过configLocations属性指定,使用都好隔开即可,如“classpath:hibernate.cfg.xml,classpath:example.cfg.xml”。

4、setMappingResources(String[] mappingResources)

如果需要在applicationContext.xml文件中指定Hibernate框架的.hbm.xml文件的位置,可以通过属性mappingResources进行设置,如“vo/Customer.hbm.xml”。

值得注意的是,如果在applicationContext.xml中通过configLocation指定了hibernate.cfg.xml文件位置,而且在hibernate.cfg.xml中已经定义了hbm.xml文件的位置,那么就不需要再次使用mappingResources属性指定.hbm.xml文件的位置。

5、setHibernateProperties(Properties hibernateProperties)

可以通过hibernateProperties属性指定Hibernate框架的任意属性,例如“hibernate.dialect”等。

如果Hibernate框架的属性都已经在hibernate.cfg.xml文件中定义,并且在applicationContext.xml中指定了configLocation属性,则不需要再次使用hibernateProperties进行指定。


配置SessionFactory:

	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
		<property name="dataSource">
			<ref bean="dataSource"/>
		</property>
	</bean>


HibernateTemplate类

Spring框架提供了HibernateTemplate类,该类和JdbcTemplate类相似,是一个IoC方式的模板类,该类能够简化Hibernate操作。


在IoC容器中创建并装配一个可用的HibernateTemplate实例

	<bean id="hibTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory"></ref>
		</property>
	</bean>

HibernateTemplate中常用的方法:

1、save(Object entity)

该方法可以持久化一个实体对象,可以将对象的状态保存到数据库中,作为一条记录插入。

2、delete(Object entity)

该方法将删除一个持久化实体对象,可以将与该实体对象对应的记录从数据库中删除。

3、delete(String queryString)

该方法的参数是一个查询语句,该方法将删除所有查询返回的实体对象。

4、public int delete(String queryString ,Object[] values,Type type)

该方法的查宿queryString 是一个HQL查询语句,参数values是查询语句的参数,参数type是语句中参数的类型。该方法将删除查询返回的所有实体对象。

5、update(Object entity)

该方法可以更新一个实体对象,从而更新实体对象对应的数据库记录。

6、List find(String queryString)

该方法执行了一个HQL查询语句,将结果集封装成实体对象,返回到List实例中。

7、List fin(String queryString,Object[] values,Type[] types)

该方法的参数queryString是一条HQL查询语句,values是该HQL语句的参数,types是参数的类型。该方法将结果集封装成实体对象,返回到List实例中。


Spring整合Hibernate的实例

1、导入Spring核心包,同时导入Spring整合的Hibernate的包

2.导入Hibernate相关包

3、选择Hibernate属性文件策略

4、指定SessionFactory的id值

5、配置SessionFactory需要的DataSource

6、使用Hibernate逆向工程,生成Customer.hbm.xml文件

applicationContext.xml文件配置如下:

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" scope="prototype">
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/mldn</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>password</value>
		</property>
		<property name="maxActive">
			<value>10</value>
		</property>
		<property name="initialSize">
			<value>2</value>
		</property>
	</bean>
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
		<property name="dataSource">
			<ref bean="dataSource"/>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>vo/Customer.hbm.xml</value>
			</list>
		</property>
	</bean>

7、创建新类CustomerDAOHiernateTemplateImpl,实现CustomerDAO接口

package dao.Impl;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;

import vo.Customer;
import dao.CustomerDAO;

public class CustomerDAOHibernateTemplateImpl implements CustomerDAO {

	private HibernateTemplate hibTemplate;

	public void setHibTemplate(HibernateTemplate hibTemplate) {
		this.hibTemplate = hibTemplate;
	}

	@Override
	public List<Customer> selectAll() {
		String hql="from Customer";
		List<Customer> list = hibTemplate.find(hql);
		return list;
	}

	@Override
	public Customer selectByName(String custname) {
		String hql="from Customer where custname=?";
		List<Customer>list = hibTemplate.find(hql,custname);
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}

	@Override
	public Customer selectByNamePwd(String custname, String pwd) {
		String hql="from Customer where custname=? and pwd=?";
		List<Customer>list = hibTemplate.find(hql, new String[] {custname,pwd});
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}

	@Override
	public void insert(Customer cust) {
		hibTemplate.save(cust);

	}

}

8、在applicationContext.xml中配置bean

	<bean id="hibTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory"></ref>
		</property>
	</bean>
	<bean id="dao" class="dao.Impl.CustomerDAOJdbcTemplateImpl">
		<property name="jdbcTemplate">
			<ref bean="jdbcTemplate"/>
		</property>
	</bean>
	<bean id="service" class="service.CustomerServiceImpl">
		<property name="dao">
			<ref bean="dao"/>
		</property>
	</bean>




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值