学习过hibernate很久了,但是由于公司的项目使用mybatis作为项目持久层框架,所以在实际的工作中hibernate没了用武之地。为了不会随着时间的流逝而忘却了最初的hibernate,在此写点东西作为纪念。
一、基本概念
1.基本作用:hibernate框架主要用于对数据库的操作,是对JDBC的封装。在应用中实现数据访问层。
2.优点:
- 避免编写大量复杂SQL
- hibernate自动生成SQL,增强不同数据库的移植性。
- 自动完成数据记录与java对象之间的映射。
3.基本原理:ORM,Object Relation Mapping,对象关系映射。可以完成Java对象和数据表记录之间的映射,只需要在业务层对对象进行操作就可以实现对数据库的操作。
二、基本使用
1.主要步骤
- 创建项目,引入相应的jar
在src添加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="show_sql">true</property> <property name="format_sql">true</property> <property name="current_session_context_class">thread</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/earl_test</property> <property name="connection.username">root</property> <property name="connection.password"></property> <mapping resource="config/mapping/Pet.hbm.xml"/> </session-factory></hibernate-configuration>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
定义实体类Pet,如下:
import java.sql.Date;public class Pet { private int id; private String name; private String owner; private String species; private int sex; private Date birth; private Date death; private boolean is_dead; ......}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
定义映射描述文件,例如Pet.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="com.earl.entity.Pet" table="pet" > <id name="id" column="id" type="integer"> <generator class="identity"></generator> </id> <property name="name" column="name" type="string"/> <property name="owner" column="owner" type="string"/> <property name="species" column="species" type="string"/> <property name="sex" column="sex" type="int"/> <property name="birth" column="birth" type="date"/> <property name="death" column="death" type="date"/> <property name="is_dead" column="is_dead" type="yes_no"/> </class></hibernate-mapping>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
2.基本操作
基本操作就是针对于数据库的CURD操作。
先创建一张测试表,这张表也是我之前练习时从其他地方找来的,下面是建表语句及数据。
CREATE TABLE IF NOT EXISTS `pet` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `owner` varchar(20) DEFAULT NULL, `species` varchar(20) DEFAULT NULL, `sex` int(11) DEFAULT NULL, `birth` date DEFAULT NULL, `death` date DEFAULT NULL, `is_dead` char(50) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;INSERT INTO `pet` (`id`, `name`, `owner`, `species`, `sex`, `birth`, `death`, `is_dead`) VALUES (1, 'nokia', 'Harold', 'cat', 1, '1993-02-04', NULL, 'N'), (2, 'Claws', 'Gwen', 'cat', 0, '1994-03-17', NULL, 'n'), (3, 'Buffy', 'Harold', 'dog', 1, '1989-05-13', NULL, 'n'), (4, 'Fang', 'Benny', 'dog', 1, '1990-08-27', NULL, 'n'), (5, 'Bowser', 'Diane', 'dog', 0, '1979-08-31', '1995-07-29', 'y'), (6, 'Chirpy', 'Gwen', 'bird', 1, '1998-09-11', NULL, 'n'), (7, 'Whistler', 'Gwen', 'bird', 1, '1997-12-09', NULL, 'n'), (8, 'Slim', 'Benny', 'snake', 0, '1996-04-29', NULL, 'n'), (9, 'Tom', 'Jerry', 'cat', 1, '2016-01-11', NULL, 'N');
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
废话不多说,下面来看看具体的使用。
@Testpublic void testInsert(){ java.util.Date birthDate=new java.util.Date(); Pet pet=new Pet(); pet.setName("Tom"); pet.setOwner("Jerry"); pet.setSpecies("cat"); pet.setSex(1); pet.setBirth(new Date(birthDate.getTime())); pet.setIs_dead(true); Configuration configuration=new Configuration(); configuration.configure(); SessionFactory factory=configuration.buildSessionFactory(); Session session=factory.openSession(); Transaction transaction=session.beginTransaction(); session.save(pet); transaction.commit(); session.close();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
@Testpublic void testDelete(){ Pet pet=new Pet(); pet.setId(11); Session session=SessionUtil.getSession(); Transaction transaction=session.beginTransaction(); session.delete(pet); transaction.commit(); SessionUtil.closeSession(session);}
@Testpublic void testUpdate(){ Session session=SessionUtil.getSession() Transaction transaction=session.beginTransaction() //对于更新操作,必须要先从数据库中查询到要更新的对象,再将其属性进行更新 //load方法是按照主键查询,返回根据主键查询到的对象 Pet pet=session.load(Pet.class, 9) pet.setSpecies("cat") pet.setName("Tom") pet.setOwner("Jerry") session.update(pet) transaction.commit() SessionUtil.closeSession(session)}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 查询
查询的方法有很多,这里先初步介绍一下使用HQL语句查询。
什么是HQL?Hibernate Query Language,hibernate查询语言。一眼看上去是不是觉得它有一个兄弟?没错,就是SQL。它和SQL语句不仅外表很相似,在使用上也有很多共通的地方。
看下面这两条语句:
HQL:select from Pet
SQL:select * from pet
它们语法大体相同,不同的是HQL面向的是对象Pet,SQL面向的数据表pet。
对于下面的案例,这里就先简单说到这,之后会对HQL有更详细的介绍。
@Testpublic void testFindAll(){ Session session=SessionUtil.getSession(); String hql="from Pet"; Query query=session.createQuery(hql); List<Pet> pets=query.list(); for(Pet pet:pets){ System.out.println(pet); } SessionUtil.closeSession(session);}
三、总结
以上就是对hibernate的一个基本使用了。之后会有文章再对hibernate的主要配置文件hibernate.cfg.xml,对象映射文件hbm.xml,以及HQL进行更加详细的说明。OK,bye,See you later!