ORM(对象关系映射)
原来操作数据库是以sql语句的方式,现在严格按照面向对象的方式通过操作javabean来操作数据库
开发的步骤
1、下载hibernate的jar包(hibernate的官网 hibernate.org)
2、引入mysql的jar包
3、编写数据库的表(后期也可以不编写)
4、写javaBean(要和创建的表,一一对应)
5、配置映射文件(xxx.hbm.xml)(要和javabean在最好同一个包下,而且 名字 要和javabean一样)
6、配置核心文件hibernate.cfg.xml 放在src根目录下
7、测试
配置文件详解
1、映射配置文件(xxx.hbm.xml)
<?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">
<!--package 指向的就是pojo所在的包名称-->
<hibernate-mapping package="com.offcn.pojo"><!--包名的命名规范 公司的域名.项目名.包名 com.offcn.day03.pojo-->
<!--name指向的是pojo table指向的是数据库的表名称-->
<class name="Person" table="person">
<!--id配置的是主键-->
<!--name对应的是pojo里的属性 column对应数据库的字段名-->
<id name="id" column="id">
<!--class id的主键自增策略
uuid 用uuid生成的字符串当做主键的id
increment 主键的自增(常用)
native 主键的自增(常用)
Sequence orcale数据库的自增
Identity 主键的自增(过时,只针对主键)
-->
<generator class="increment"/>
</id>
<!--property 其他非主键的属性配置-->
<!--name 和pojo对应的属性名称 type类型 clomn 对应数据库的字段 (如果不写column它默认就和pojo的属性一样)-->
<property name="name" type="string" column="name"/>
<property name="password" type="java.lang.String" column="password"/>
</class>
</hibernate-mapping>
2、核心配置文件
<?xml version='1.0' encoding='utf-8'?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!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">java.sql.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/0708test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- SQL dialect 方言 告诉系统,用的是哪一款数据库-->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--可选配置文件 hbm2ddl.auto 会根据不同的属性来决定是否重新创建数据库-->
<!--update 如果数据库存在就更新数据,如果数据库不存在就创建数据库-->
<!--create 每次开始都重新创建数据库-->
<property name="hbm2ddl.auto" >create</property>
<property name="show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!--加载映射配置文件-->
<mapping resource="com/offcn/pojo/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
五、hibernate对数据的crud
1、添加数据
2、删除数据
3、更新
4、查询
hibernate 自带的hql查询在5.0以后,基本上不在维护了,所以尽量少用
hibernate的三种状态
1、游离状态
已经有持久化的标记(oid),但是没有纳入session的管理
2、瞬时状态
没有持久化的标记(oid),也没有被纳入session的管理
3、持久状态
有持久化的标记(oid),也被session进行管理
hibernate的多表操作(一对一,一对多,多对多)
一对一
实体对象
映射配置文件
核心配置文件
测试
多对一、一对多
实体
映射配置
核心配置文件
测试
Configuration conf = new Configuration();
conf.configure();
// 获取session工程
SessionFactory sessionFactory = conf.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tr = session.beginTransaction();
Teacher t = new Teacher();
t.setName("教师");
Stu s = new Stu();
s.setName("AA");
s.setTeacher(t);
Stu s1 = new Stu();
s1.setId(2);
s1.setName("BB");
s1.setTeacher(t);
t.getStus().add(s);
t.getStus().add(s1);
// 保存
session.save(s);
session.save(s1);
session.save(t);
tr.commit();
session.close();
多对多
实体
映射配置文件
核心配置文件
测试
Configuration conf = new Configuration();
conf.configure();
// 获取session工程
SessionFactory sessionFactory = conf.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tr = session.beginTransaction();
// 创建book和图书馆对象
Book b = new Book();
Book b1 = new Book();
TuShuGuan t = new TuShuGuan();
TuShuGuan t1 = new TuShuGuan();
t.setName("新华");
b.setName("辞海");
b1.setName("三国演义");
t1.setName("水浒");
// 关系
t.getBooks().add(b);
t.getBooks().add(b1);
b.getTuShuGuans().add(t);
b.getTuShuGuans().add(t1);
session.save(b);
session.save(b1);
session.save(t1);
session.save(t);
tr.commit();
// 关闭
session.close();