Hibernate入门
ORM:对象关系映射(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping)是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换。其实就是将关系和对象通过映射文件进行联系。我们在程序中只需要操作对象即可操作对应的数据库表的数据。
Hibernate:一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 (摘自各路大神)
POJO:Plain Ordinary Java Object
导入需求的jar包,这个可以从官网上下载,下下来之后把这里的包都拷贝过去就可以。
hibernate-release-5.0.7.Final\lib\required
第一步:建立数据库表
第二步:src文件下创建hibernate.cfg.xml文件
该文件用于初始化hibernate需要用的信息,做hibernate的全局配置,也可以放在classes文件下。
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<!--数据库驱动类 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!--数据库url ,db是我的数据库名称-->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db?characterEncoding=UTF-8</property>
<!--用户名 -->
<property name="hibernate.connection.username">name</property>
<!--密码 -->
<property name="hibernate.connection.password">password</property>
<!--方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--映射实体类 -->
<mapping resource="bean/student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
第三步:创建实体类student
不添加构造方法会报错
package bean;
public class student {
private int ID;
private String name;
private String sex;
private int age;
public student(){
super();
}
public student(int ID,String name,String sex,int age){
this.ID = ID;
this.name = name;
this.sex = sex;
this.age = age;
}
public int getID() {
return ID;
}
public void setID(int id) {
ID = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
第四步:创建student.hbm.xml文件,对象映射数据库
一个class对应一个类,一个property对应一个属性,id为主键。
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping>
<class name="bean.student" table="students">
<id name="ID" column="id" type="java.lang.Integer">
<generator class="assigned"/>
<!--generator:主键生成策略
class:标记主键如何生成
取值:
1、native:自动增长,会根据当前的数据库自动切换
2、identity:mySQL的自增策略
3、sequence:Oracle的自增标记
4、uuid:32位字符串
5、assigned:自定义字符串
6、foreign:外键
7、increment:自己维护自增关系
-->
</id>
<property name="name" column="name" type="string"></property>
<property name="sex" column="sex" type="string"></property>
<property name="age" column="age" type="java.lang.Integer"></property>
</class>
</hibernate-mapping>
注意要将映射文件配置到全局配置中
<mapping resource="bean/student.hbm.xml"/>
第五步:编写测试类测试
// 加载配置文件
Configuration configuration = new Configuration().configure();
// 创建Session工厂
SessionFactory factory = configuration.buildSessionFactory();
// 创建Session对象
Session session = factory.openSession();
//开始事务,默认是非自动提交
Transaction tx = session.beginTransaction();
// 操作数据
session.save(new student(3,"测试", "666666",15));
//提交事务
tx.commit();
//结束
session.close();
factory.close();
执行原理与流程
a、应用程序先调用Configuration类,该类读取Hibernate配置文件及映射文件中的信息,
b、用这些信息生成一个SessionFactory对象,
c、从SessionFactory对象生成一个Session对象,
d、用Session对象生成Transaction对象;
e、通过Session对象的get(),load(),save(),update(),delete()和saveOrUpdate()、createQuery()等方法对进行CURD等操作;
f、提交事务。
学习博客
https://www.cnblogs.com/blackdeng/p/7026729.html
https://blog.csdn.net/xingfei_work/article/details/75290263