hibernate框架的入门
来源:互联网 发布:手机淘宝名字可以改吗 编辑:程序博客网 时间:2024/04/29 23:51
Hibernate是java开发中使用的框架之一,不管开发桌面程序或者hibernate,hibernate不局限使用在java web中,由于hibernate的出现大大提高了项目开发的效率,在中小型项目中经常使用hibernate。Hibernate是基于ORM(对象关系映射:Object ralation mapping:我们可以理解为一个接口)实现了JDBC进行了非常轻量级的对象封装,因此hibernate的框架使我们可以使用对象的思想来操作数据库。此时也许会想起我们刚刚学习如何访问数据库时,那种简单式的访问,到平常做项目中的使用到DBUtils工具(或者ibatis和mybatis)提高开发的效率,再到hibernate框架,都是我们几乎不用接触到JDBC底层访问数据。
hibernate是实现orm,DBUtils工具(ibatis和mybatis可以与其媲美)也实现了ORM的部分功能。
hibernate:无sql,但是提供了HQL语言,当然最终是转化为sql
DButils:有sql
ibatis和mybatis:少量的sql,
第一个例子的步骤:
创建web工程,
导入包;
创建类和数据库:
use example_db
create table person(
id int(5) primary key,
name varchar(20),
gender varchar(1),
birthday date
);
Person类:
package example.domain;import java.util.Date;public class Person {private Integer id;private String name;private String gender;private Date birthday;public Person() {super();// TODO Auto-generated constructor stub}public Person(Integer id, String name, String gender, Date birthday) {super();this.id = id;this.name = name;this.gender = gender;this.birthday = birthday;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}}
hibernate的映射类型:
hibernate-distribution-3.5.6-Final-dist解压之后可以通过
hibernate-distribution-3.5.6-Final-dist\hibernate-distribution-3.5.6-Final\project\tutorials\web\src\main\resources 下找到路径找到hibernate.cfg.xml的配置;
hibernate-distribution-3.5.6-Final-dist\hibernate-distribution-3.5.6-Final\project\etc 路径下可以找到hibernate.properties的配置参数说明
<?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 package="example.domain"><class name="Person" table="person"><id name="id" column="id" type="int"><generator class="increment"></generator></id><property name="name" column="name" type="string"></property><property name="gender" column="gender" type="string"></property><property name="birthday" column="birthday" type="date"></property></class></hibernate-mapping>
在id自动增长策略中,单线程可以用increment,多线程则不能使用。property中的type可以不写,hibernate底层会自动识别。
src下的hibernate.cfg.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?><!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="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1:3306/example_db</property> <property name="connection.username">root</property> <property name="connection.password">04010</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><!-- 以下是配置加载映射文件 --><mapping resource="example/domain/Person.hbm.xml"/></session-factory></hibernate-configuration>
对SessionFactory和session的简单理解:
1、SessionFactory应该而且可以只创建一次,但他是线程安全,因为创建和销毁非常消耗时间和资源,被共享,所以建议放在静态代码块中初始化。
2、session是不耗费资源,可随时创建随时销毁,但是session是线程不安全的,他也被称为一级缓存。
3、session中只有事务提交才产生sql语句把session一级缓存的数据与数据库交互。
package example.dao;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;import example.domain.Person;public class PersonDao {@Testpublic void addPerson(){Person person=new Person(111, "小明", "男", new Date());Session session=null;try{//创建configure配置文件Configuration cfg=new Configuration();//加载映射文件cfg.configure();//创建sessionFactory工厂对象SessionFactory sessionFactory=cfg.buildSessionFactory();//创建session对象session= sessionFactory.openSession();//开启事务Transaction ts= session.getTransaction();//开始事务ts.begin();//保存对象到数据库session.save(person);ts.commit();}catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally{session.close();}}}
测试成功
- hibernate框架的入门
- Hibernate数据库框架的入门
- 【Hibernate框架】Hibernate入门
- Hibernate框架入门
- Hibernate框架入门一
- Hibernate框架基础入门
- Hibernate框架入门案例
- 【SSH框架】--Hibernate入门
- Hibernate框架入门(一)
- hibernate框架入门
- Hibernate框架入门2
- hibernate框架入门
- Hibernate框架入门Demo
- Hibernate框架入门
- Hibernate框架入门
- Hibernate框架入门
- Hibernate框架快速入门
- hibernate框架入门
- 嵌入式 arm平台kernel启动第二阶段分析
- Unity3D浏览器调用HTML网页中的函数
- 判断year年份是否为闰年(能被4整除是闰年,能被整除100不是,能被400整除是闰年)。闰年leap_year为1,否则为0.
- hdu 4493 Tutor(水题)
- Nagios 快速安装手册
- hibernate框架的入门
- int占几个字节
- ok6410初始化内存
- 软考复习之路—组成原理
- linux gcc常用命令
- 多角度彻底理解数据库事务中的脏读,虚读和不可重复读
- Use GraceNote SDK in iOS(一)通过序列化的GDO查询专辑封面
- VC win32中的各种字符型和相互转换
- 利用Hog特征和SVM分类器进行行人检测