hibernate集成
hibernate是一个优秀的持久化框架负责简化将对象保存到数据库中,或从数据库中读取数据并封装到对象的工作。hibernate通过简单配置和编码即可替代jdbc繁琐的程序代码。
下面是集成hibernate的具体步骤
下载hibernate的.jar包并在项目中进行引用
在classpath下创建并编写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="connection.driver_class">oracle.jdbc.OracleDriver</property><!--驱动类的全名称-->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property><!--数据库连接的url-->
<property name="connection.username">WZ</property><!--用户名-->
<property name="connection.password">*****</property><!--密码--> <!-- 配置SQL方言 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- 设置Session上下文-->
<property name="current_session_context_class">thread</property>
<!-- 格式化sql -->
<property name="format_sql"> true</property> <!-- 在控制台显示hibernate自动生成的sql语句 -->
<property name="show_sql">true</property>
<!--格式换在控制台显示的sql语句-->
<property name="hbm2ddl.auto">
update
</property>
<!--数据实体类的配置文件的引用-->
<mapping resource="xxx.hbm.xml" /> </session-factory> </hibernate-configuration>
创建并编写实体类的配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="student_entity"> <class name="类名" table="表名">
主键类配置
<id name="sid" column="stuno">
<generator class="sequence">
<param name="sequence">
SQL_ID
</param> </generator> </id>
属性配置
<property name="sage"/>
<property name="name" column="stuname"/> </class> </hibernate-mapping>
其中:<generator class=””/>配置主键的生成策略其取值如下:
increment:对类型为long、short、int类型的主键,已自动增长的方式生成主键的值,增量为1
sequence:对于支持序列的数据库可使用序列来给主键赋值其通过param子元素来指定序列名
identity:对支持标识列的数据库可使用该策略。但需要在数据控的该列设置为标识列
native:由hibernate自动判断使用何种策略
assigned:主键有应用程序生成,hibernate无需参与
在实体类的配置文件编写完成之后还要在hibernate.cfg.xml文件中添加该配置文件的引用如图:
测试
以上步骤全部完成后就可以在程序中使用hibernate提供的api来进行数据操作,
Configuration cfgt=new Configuration().configure();//创建配置对象将hibernate.cfg.xml文件中的信息封装到cfgt对象中
SessionFactory factory = cfgt.buildSessionFactory();//创建命令工厂
Session session = factory.openSession();//打开命令对象
Transaction tsc = session.beginTransaction();//开始事务 session.save(stu);将stu中的信息保存到数据库中(stu为已创建的实体类对象) tsc.commit();//提交事务
session.close();//关闭命令对象
hibernate集成的更多相关文章
- Spring与Hibernate集成中的Session问题
主要讨论Spring与Hibernate集成中的session问题 1.通过getSession()方法获得session进行操作 public class Test extends Hibernat ...
- hibernate集成ehcahe进行缓存管理
ehcace是现在非常流行的缓存框架,有轻量.灵活.可扩展.支持集群/分布式等优点. 在项目中,使用ehcace可以对数据进行缓存(一般使用.基于注解.基于aop),使用filter可以对页面进行缓存 ...
- Spring和Hibernate集成配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- Spring和Hibernate集成的HibernateTemplate的一些常用方法总结
1:get/load存取单条数据 public Teacher getTeacherById(Long id) { return (Teacher)this.hibernateTemplate.get ...
- spring和hibernate集成事物管理配置方法
spring+hibernate,采用声明式事务 1.声明式事务配置 * 配置SessionFactory * 配置事务管理器 * 事务的传播特性 * 那些类那些方法使用事务 2.编写业务逻辑方法 * ...
- Hibernate 集成 Ehcache 开启二级缓存
一.将 Ehcache.xml 放到 classpath 下 <?xml version="1.0" encoding="UTF-8"?> < ...
- springMvc和Hibernate集成实现用户添加
源码:http://pan.baidu.com/s/1i4xVLE9(百度云) 步骤:一.创建数据库(mysql) 二.导入相应jar包(注意不同数据库jdbc.jar包)配置web.xml.spri ...
- Spring 与 Hibernate 集成 Transactional设置为只读
@Transactional标签用于标记ServiceImpl使用事务,并且能够打开一个sessionFactory的session,并且打开事务. 如果在这个标签为@Transactional(pr ...
- Spring,Hibernate 集成解决多hbm.xml文件繁多的方案
开发一个大一点的项目有很多的hbm.xml文件,有时候上百个也不稀奇,如果用 <property name="mappingLocations"> <list&g ...
随机推荐
- CentOS下httpd下php 连接mysql 本机可以,127.0.0.1不能访问
你看到的这个文章来自于http://www.cnblogs.com/ayanmw php代码很简单: $server="127.0.0.1"; println("Begi ...
- Huxley 是一个用于Web应用 UI 测试的工具
Huxley 是一个用于Web应用 UI 测试的工具,由 Pete Hunt 和 Maykel Loomans 用 Python 开发. UI 测试比较令人头疼. UI测试不好写,而且很容易失效: ...
- Objective-C在windows开发环境的搭建
转自 http://www.hdj.me/objective-c-in-windows 安装GNUstep GNUstep Windows Installer提供了Windows平台下的Objecti ...
- android里TextView加下划线的几种方式
如果是在资源文件里: <resources> <string name="hello"><u>phone:0123456</u>&l ...
- 每日英语:A New Way to Learn Chinese
Entrepreneur and author ShaoLan Hsueh thinks that English-speakers can start learning to read Chines ...
- OGG-01232 Receive TCP params error: TCP/IP error 104 (Connection reset by peer), endpoint:
源端: 2015-02-05 17:45:49 INFO OGG-01815 Virtual Memory Facilities for: COM anon alloc: mmap(MAP_ANON) ...
- WPF快速精通版
命名空间: xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:U ...
- javaBean List Map json(转)
1.依赖的包 根据:http://json-lib.sourceforge.net/ 可见json-lib使用需要依赖一下包: 下载地址:http://download.csdn.net/down ...
- 物料分类账 [COML] PART 2 - 总体流程
核心流程概要: [1]. 分类账在物料主数据的影响 首先描述下SAP中物料价格的 物料主数据相关的几个点: q价格控制(Price Control): 决定物料计价方式. S 标准价格(Standar ...
- 解决play-1.4.0在linux或mac下提示No such file or directory的问题
问题原因:"play"脚本中有特殊符号. 解决方案:写脚本去掉即可. 代码:fixplay.py 放在play-1.4.0目录下执行.亲测在osx与ubuntu下均可用. with ...