Hibernate入门
来源:互联网 发布:python xpath教程 编辑:程序博客网 时间:2024/05/17 02:06
Hibernate3入门
2016/12/18
Hibernate的概述
什么是Hibernate
Hibernate是轻量级JavaEE应用的持久层解决方案,是一个关系数据库ORM框架
轻量级:使用方便(比Apache DbUtils 复杂很多倍)这个概念是个相对概念。(主要是对比EJB来说的,ejb是重量级的(内建组件很多,启动很慢,东西很多))
持久层: JavaEE分为表现层、业务层(service)、持久层(dao)
Struts2 是一个表现层框架
Hibernate 是一个持久层框架,底层就是封装了JDBC,也就说,HIbernate最终进入数据库执行的还是sql语句(操作数据库)
关系数据库: mysql、oracle、db2、sql server、sybase 都是关系型数据库(主流数据库)
ORM(Object Relational Mapping):对象关系映射(一种思想)
Java–面向对象的语言
Mysql–关系型数据库
- 将数据表的关系,映射为类和对象之间关系:
ORM好处: java程序员只需要在程序中操作对象,因为存在映射关系,自动生成SQL语句。不需要你写sql语句,就可以操作数据库。
允许你以面向对象的方式直接操作sql数据库
Java中常见的ORM实现(持久层框架):
JPA( Java Persistence API).Sun公司的规范,JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系(只有接口规范)
Hibernate 最流行ORM框架,通过对象-关系映射配置,可以完全脱离底层SQL(全自动)
MyBatis 本是apache的一个开源项目 iBatis,支持普通 SQL查询,存储过程和高级映射的优秀持久层框架(半自动)
其他的持久层框架(非ORM):Apache DBUtils 、Spring JDBCTemplate
Hibernate的优缺点
优点
Hibernate完全是ORM,自动生成sql语句,完全不需要懂sql,极大简化jdbc的操作。
具有很强的反射和代理机制,生成代理对象,(可以代码增强)在数据获取可以进行延迟抓取、缓存的使用、达到优化的目的。
缺点
- hibernate完全orm,自动生成sql,所以很多企业用户对hibernate性能存在质疑(任何持久层框架都不如jdbc速度快,开发效率和性能的平衡)。在hibernate框架中,提供了很多优化手段。
入门示例
(1)导入jar包到工程
(2)开发准备及步骤(三个准备,7个步骤)
三个准备
1)创建数据库
2)编写实体类和ORM映射文件(实体映射文件)
3)配置Hibernate的灵魂文件
编写实体类:Customer
package cn.itcast.a_quickstart;public class Customer {private int id;private String name;private int age;private String city;public int getId() { return id;}public void setId(int id) { this.id = id;}public String getName() { return name;}public void setName(String name) { this.name = name;}public int getAge() { return age;}public void setAge(int age) { this.age = age;}public String getCity() { return city;}public void setCity(String city) { this.city = city;}}
编写Customer.hbm.xml映射文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="cn.itcast.a_quickstart.Customer" table="t_customer"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <property name="age"></property> <property name="city"></property></class></hibernate-mapping>
配置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:///day31</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">abc</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="cn/itcast/a_quickstart/Customer.hbm.xml"/></session-factory></hibernate-configuration>
7个步骤
1. 加载配置文件:hibernate.cfg.xml
2. 创建会话工厂
3. 创建会话
4. 开启事务
5. 进行CRUD操作
6. 提交事务
7. 关闭连接
测试代码:CRUD操作
package cn.itcast.a_quickstart;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;public class TestCustomer {@Testpublic void save() { // 1.加载灵魂文件 Configuration cfg = new Configuration().configure(); // 2.创建会话工厂 SessionFactory sessionFactory = cfg.buildSessionFactory(); // 3.获取连接 Session session = sessionFactory.openSession(); // 4.开启事务 Transaction tran = session.beginTransaction(); // 5.保存操作 Customer customer = new Customer(); customer.setName("tom"); customer.setAge(18); customer.setCity("sh"); session.save(customer); // 6.提交事务 tran.commit(); // 7.关闭连接 session.close(); sessionFactory.close();}@Testpublic void update() { // 1.加载灵魂文件 Configuration cfg = new Configuration().configure(); // 2.创建会话工厂 SessionFactory sessionFactory = cfg.buildSessionFactory(); // 3.获取连接 Session session = sessionFactory.openSession(); // 4.开启事务 Transaction tran = session.beginTransaction(); // 5.修改操作 Customer customer = new Customer(); customer.setId(1); customer.setName("lucy"); customer.setAge(18); customer.setCity("bj"); session.update(customer); // 6.提交事务 tran.commit(); // 7.关闭连接 session.close(); sessionFactory.close();}@Testpublic void delete() { // 1.加载灵魂文件 Configuration cfg = new Configuration().configure(); // 2.创建会话工厂 SessionFactory sessionFactory = cfg.buildSessionFactory(); // 3.获取连接 Session session = sessionFactory.openSession(); // 4.开启事务 Transaction tran = session.beginTransaction(); // 5.删除操作 Customer customer = new Customer(); customer.setId(2); session.delete(customer); // 6.提交事务 tran.commit(); // 7.关闭连接 session.close(); sessionFactory.close();}@Testpublic void queryById() { // 1.加载灵魂文件 Configuration cfg = new Configuration().configure(); // 2.创建会话工厂 SessionFactory sessionFactory = cfg.buildSessionFactory(); // 3.获取连接 Session session = sessionFactory.openSession(); // 4.开启事务 Transaction tran = session.beginTransaction(); // 5.根据Id查询操作 Customer customer = (Customer) session.get(Customer.class, 1); System.out.println(customer); // 6.提交事务 tran.commit(); // 7.关闭连接 session.close(); sessionFactory.close();}@Testpublic void queryAll() { // 1.加载灵魂文件 Configuration cfg = new Configuration().configure(); // 2.创建会话工厂 SessionFactory sessionFactory = cfg.buildSessionFactory(); // 3.获取连接 Session session = sessionFactory.openSession(); // 4.开启事务 Transaction tran = session.beginTransaction(); // 5.查询所有操作 // HQL List<Customer> list = session.createQuery("from Customer").list(); System.out.println(list); // 6.提交事务 tran.commit(); // 7.关闭连接 session.close(); sessionFactory.close();}
}
- Hibernate 入门
- Hibernate 入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- Hibernate 入门
- Hibernate入门
- Hibernate入门
- Hibernate 入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- hibernate 入门
- Hibernate入门
- hibernate入门
- Django i18n国际化
- 欢迎使用CSDN-markdown编辑器
- OpenGL应用开发---纹理
- 21天教你学会C++
- 欢迎使用CSDN-markdown编辑器
- Hibernate入门
- 欢迎使用CSDN-markdown编辑器
- ScrollView和ListView嵌套显示不完全
- Windows下vs2015编译Chromium[2016/12/18]
- PHPStorm+XDebug进行调试图文教程
- Leetcode Lowest Common Ancestor of a Binary Search Tree
- Codeforces 746C Tram(水题)
- Zen Coding 快速编写HTML/CSS代码的实现
- vovado 2014.4 IP核之Clocking Wizard实现分频与倍频