HibernateTemplate的使用详解编程语言
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自动生成数据库表的实体类
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 /beansInfoDao类
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(); }结果还是一样的
16985.html
cjavamysqlxml
相关文章
- linux内核使用的编程语言_linux内核模块编程
- Markdown使用教程详解编程语言
- 由浅入深理解Java线程池及线程池的如何使用详解编程语言
- 工作流引擎Activiti使用总结详解编程语言
- BeautifulSoup模块的简单使用详解编程语言
- BeanUtils简单使用详解编程语言
- Picasso开源图片加载利器使用初探详解编程语言
- 使用微软的标准方法实现Ajax详解编程语言
- mybatis sql循环的使用详解编程语言
- node中使用consolidate后报错Cannot find module ‘ejs’详解编程语言
- [javascript] Promise简单学习使用详解编程语言
- onbeforeunload方法和onunload方法的使用实例详解编程语言
- Flexigrid的使用(整合Struts2)详解编程语言
- java使用POI操作excel文件,实现批量导出,和导入详解编程语言
- Java与邮件系统交互之使用Socket验证邮箱是否存在详解编程语言
- Spring Cloud(七):使用SVN存储分布式配置中心文件和实现refresh详解编程语言
- 使用SHA256WithRSA来签名和验签(.NET/C#)详解编程语言
- 监控JVM内存使用情况,剩余空间小于2M时报警详解编程语言
- 使用LinkedHashMap来实现一个使用LRU(Least Recently Used)算法的cache详解编程语言
- 使用dom4j解析XML详解编程语言
- Java并发编程:线程池的使用详解编程语言
- javascript:使用document.getElementById读取数据为空分析详解编程语言
- HttpClient使用代理IP详解编程语言
- 使用Spring4时, 运行时出现找不到MappingJacksonHttpMessageConverter的情况详解编程语言
- Sap中动态内表的使用详解编程语言
- cmake安装与使用详解编程语言