hibernate入门
来源:互联网 发布:城乡发展不平衡数据 编辑:程序博客网 时间:2024/04/30 00:37
Hibernate介绍:
持久化java对象
封装了JDBC,实现了or-mapping一个开源的持久层框架
orm:(Object Relation Mapping)
Jdbc特点:
* 操作代码复杂
* JDBC的应用与数据库耦合度强
* JDBC的应用很难做到轻松的数据库移植
* 不能实现对数据的缓存
* 对象 与 关系之间的转换,需要用户实现
* 性能高(数据量非常庞大的情况)
Hibernate特点:
* 封装了JDBC底层的操作
* Hibernate的应用与数据库耦合度低
* 不再需要使用sql语句,直接操作对象
* orm的实现,抵抗不匹配
User{uid,username....}
save(user);
映射(xml)
t_user(uid,username)
* 做不到sql优化
* 缓存的实现
* 不适合对数据量非常庞大的系统
Hibernate的使用:
* 搭建环境:
hibernate 3.5
hibernate3.5\lib\require\*.jar
1.antlr-2.7.6.jar-------------------语言转换工具(hql语句)
2.commons-collections-3.1.jar-------增强java处理集合的能力
3.dom4j-1.6.1.jar-------------------解析XML
4.jta-1.1.jar-----------------------提供标准Java事务处理接口
//3.5源代码中没有如下两个jar(log4j.jar, slf4j-log4j12.jar),在hibernate3.4\lib\test\下找
下面四个jar包都与日志系统有关
5.log4j.jar-------------------------处理日志
6.slf4j-log4j12.jar-----------------日志系统
7.slf4j-api-1.5.8.jar---------------日志系统
8.commons-logging-1.1.jar
hibernate3.5\
9.hibernate3.jar--------------------核心Jar
以下四个jar包都与hibernate的懒惰式加载有关
10.asm.jar
11.backport-util-concurrent-2.1.jar
12. cglib-2.1.3.jar
13.Javassist-3.9.0.GA.jar------------Java类提供扩展功能
数据库驱动
14.mysqldriver.jar-------------------数据库驱动
* 配置文件
* 映射文件(实现映射)User.hbm.xml
* 配置文件(底层数据库连接信息,hibernate\project\etc\hibernate.cfg.xml)
具体步骤
1.配置数据库的连接信息,需要查找两个文件,都在hibernate3.5.6/project/etc下,hibernate.cfg.xml, hibernate.properties
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!-- 必须配置的属性 --><!-- 底层数据库连接的基本信息 --><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hdemo01</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">865631572</property><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><!--**配置所使用的数据库方言**--><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><!-- 可选配置 --><!-- 显示SQL语句 --><property name="hibernate.show_sql">true</property><!-- 格式化显示SQL语句 --><property name="hibernate.format_sql">true</property><!-- 表示由hibernate自动生成表结构 --><property name="hibernate.hbm2ddl.auto">create</property><!-- 必须配置加载的映射文件 --><mapping resource="com/tarena/demo/po/User.hbm.xml"/></session-factory></hibernate-configuration>
2.实体类(领域模型,po persistenceobject,pojo)
User
uid
username
password
3.实现对象与关系的映射,新建User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.tarena.demo.po.User" table="t_user"><id name="uid" column="uid"><generator class="identity"></generator></id><property name="userName"></property><property name="userPassword"></property></class></hibernate-mapping>
4.使用hibernate api 实现数据CRUD的操作
添加User
package com.tarena.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.tarena.demo.po.User;public class TestApp {public static void main(String[] args) {User user = new User();user.setUserName("李瑞");user.setUserPassword("865631572");//保存底层连接信息Configuration cfg = new Configuration();//解析Hibernate配置文件cfg.configure();//创建session工厂SessionFactory factory= cfg.buildSessionFactory();//创建session(和hibernate之间的会话)Session session = factory.openSession();//开启事务(查询的时候不需要开启事务)Transaction tr = session.beginTransaction();//保存对象session.save(user);//提交事务tr.commit();}}
项目结构:
- Hibernate 入门
- Hibernate 入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- Hibernate 入门
- Hibernate入门
- Hibernate入门
- Hibernate 入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- hibernate 入门
- Hibernate入门
- hibernate入门
- 关于JS脚本中拼接Html代码注意事项
- 网站学习备忘001——运用POST方法获取表单中的数据,并显示在同一网页中
- cf267a
- prim-普里姆 poj 1287 示例 [ 实现用到并查集 ]
- CentOS5.4安装redmine详细步骤
- hibernate入门
- 一直没发现的磁盘管理功能
- 深入理解HTTP协议
- 迪杰斯特拉 示例 : poj 2387 Til the Cows Come Home
- Java集合中的ArrayList LinkedList HashSet TreeSet HashMap TreeMap 的区别
- Floyd-template 示例
- CSS声音IE5.5
- 拓扑排序 : hdu 1285 示例
- Android开发之旅: Intents和Intent Filters(理论部分)