zl程序教程

您现在的位置是:首页 >  其他

当前栏目

HibernateTemplate的使用详解编程语言

编程语言 使用 详解 hibernateTemplate
2023-06-13 09:11:46 时间

 HibernateTemplate 提供了非常多的常用方法来完成基本的操作,比如增加、删除、修改及查询等操作,Spring 2.0 更增加对命名 SQL 查询的支持,也增加对分页的支持。大部分情况下,使用Hibernate 的常规用法,就可完成大多数DAO对象的 CRUD操作。

下面是 HibernateTemplate的常用方法。

    delete(Object entity): 删除指定持久化实例。

    deleteAll(Collection entities): 删除集合内全部持久化类实例。

    find(String queryString): 根据 HQL 查询字符串来返回实例集合。

    findByNamedQuery(String queryName): 根据命名查询返回实例集合。

    load或get(Classentity Class,Serializable id): 根据主键加载特定持久化类的实例。

    save(Object entity): 保存新的实例。

    saveOrUpdate(Object entity): 根据实例状态,选择保存或者更新。

    update(Object entity): 更新实例的状态,要求entity 是持久状态。

    setMaxResults(intmax Results): 设置分页的大小。

 

HibernateTemplate与session的区别

 

使用方法没有多大的区别,只是使用时不用自己设置事务,也不用关闭session。

 

我们使用HibernateTemplate,有一个很重要的原因就在于我们不想直接控制事务,不想直接去获取,打开Session,开始一个事务,处理异常,提交一个事务,最后关闭一个SessionHibernateTemplate 是Hibernate操作进行封装,我们只要简单的条用HibernateTemplate 对象,传入hql和参数,就获得查询接口,至于事务的开启,关闭,都交给HibernateTemplate  对象来处理我们自己只专注于业务,不想去作这些重复而繁琐的操作。我们把这些责任全部委托给了 HibernateTemplate,然后使用声明式的配置来实现这样的功能。

如果我们通过类似getSession()这样的方法获得了Session,那就意味着我们放弃了上面所说的一切好处。

在使用Spring的时候 DAO类继承了 HibernateDaoSupport 类又因为HibernateDaoSupport 类里面有个属性 hibernateTemplate;所以就可以进行设置注,也就是Spring的一大优点面向切面式编程,进行设置注入,在Tomcat启动的时候由 Tomcat 加载 ApplicationContext.xml,配置文件给 hibernateTemplate赋值,这样的话就实现了,在使用某个对象之前不用给他实例化

实例:

hibernate自动生成数据库表的实体类

HibernateTemplate的使用详解编程语言

 

hibernate.cfg.xml

 ?xml version="1.0" encoding="UTF-8"? 

 !DOCTYPE hibernate-configuration PUBLIC 

 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 

 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" 

 hibernate-configuration 

 session-factory 

 !-- 

 property name="hibernate.connection.driver_class" com.mysql.jdbc.Driver /property 

 property name="hibernate.connection.url" jdbc:mysql://127.0.0.1:3306/mydb /property 

 property name="hibernate.connection.username" root /property 

 property name="hibernate.dialect" org.hibernate.dialect.MySQLDialect /property 

 property name="show_sql" true /property 

 !-- 

 mapping resource="com/itnba/maya/entities/Family.hbm.xml"/ 

 mapping resource="com/itnba/maya/entities/Info.hbm.xml"/ 

 mapping resource="com/itnba/maya/entities/Nation.hbm.xml"/ 

 mapping resource="com/itnba/maya/entities/Title.hbm.xml"/ 

 mapping resource="com/itnba/maya/entities/Work.hbm.xml"/ 

 /session-factory 

 /hibernate-configuration 
db.properties文件
driver >

 Spring的beans.xml文件

 ?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:aop="http://www.springframework.org/schema/aop" 

 xmlns:context="http://www.springframework.org/schema/context" 

 xmlns:tx="http://www.springframework.org/schema/tx" 

 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 

 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 

 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 

 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd" 

 !-- 自动扫描 -- 

 context:component-scan base-package="com.itnba.maya.entities" /context:component-scan 

 !--加载资源对象 -- 

 context:property-placeholder location="classpath:db.properties"/ 

 !-- 实例化c3p0对象 -- 

 bean id="dataSource" 

 property name="driverClass" value="${driverClass}" /property 

 property name="jdbcUrl" value="${jdbcUrl}" /property 

 property name="user" value="${user}" /property 

 property name="password" value="${password}" /property 

 property name="minPoolSize" value="${minPoolSize}" /property 

 property name="maxPoolSize" value="${maxPoolSize}" /property 

 property name="initialPoolSize" value="${initialPoolSize}" /property 

 /bean 

 !-- 配置Hibernate的SessionFactory -- 

 bean id="factory" 

 property name="dataSource" ref="dataSource" /property 

 property name="configLocation" value="classpath:hibernate.cfg.xml" /property 

 property name="mappingLocations" value="com/itnba/maya/entities/*.hbm.xml" /property 

 /bean 

 !-- 配置spring的声明性事务 -- 

 bean id="transactionManager" !-- 要根据hibernate的版本配置 -- 

 property name="sessionFactory" ref="factory" /property 

 /bean 

 !-- 配置事务属性 -- 

 tx:advice id="txAdvice" transaction-manager="transactionManager" 

 tx:attributes 

 tx:method name="*"/ 

 /tx:attributes 

 /tx:advice 

 !-- 配置事务切入点 -- 

 aop:config 

 aop:pointcut expression="execution(* com.itnba.maya.entities.*.*(..))" id="pointCut"/ 

 aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/ 

 /aop:config 

 !-- 配置 HibernateTemplate -- 

 bean id="hibernateTemplate" 

 property name="sessionFactory" ref="factory" /property 

 /bean 

 /beans 

 InfoDao类

package com.itnba.maya.entities; 

import org.hibernate.Session; 

import org.hibernate.SessionFactory; 

import org.springframework.beans.factory.annotation.Autowired; 

import org.springframework.orm.hibernate5.HibernateTemplate; 

import org.springframework.stereotype.Repository; 

@Repository//自动扫描 

public class InfoDao { 

 @Autowired//注解 

 private HibernateTemplate ht; 

 public void select() { 

 Info data =ht.get(Info.class, "p005"); 

 System.out.println(data.getName()); 

 /* 之前用是下面的之中写法 

 private SessionFactory factory; 

 public Session getSession(){ 

 return factory.getCurrentSession(); 

 public void select() { 

 Info data = getSession().get(Info.class, "p005"); 

 System.out.println(data.getName()); 

}

main方法类

package com.itnba.maya.entities; 

import java.sql.Connection; 

import java.sql.SQLException; 

import javax.sql.DataSource; 

import org.hibernate.Session; 

import org.hibernate.SessionFactory; 

import org.hibernate.cfg.Configuration; 

import org.springframework.context.ApplicationContext; 

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class Test { 

 public static void main(String[] args) throws SQLException { 

 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 

 InfoDao data=(InfoDao) context.getBean("infoDao"); 

 data.select(); 

}

结果还是一样的

HibernateTemplate的使用详解编程语言

 

16985.html

cjavamysqlxml