Hibernate是一个非侵入式的ORMapping框架
什么是侵入式的框架?什么是非侵入式的框架?
侵入式的框架:在使用这个框架的时候需要继承 或者 实现某个特定的类或接口。
非侵入式的框架:在使用这个框架的时候不需要继承 或者 实现某个特定的类或接口。
ORMapping是什么? O---object R---relation(关系型的数据库) Mapping---映射
即: 将java对象通过映射关系映射到关系型数据库
理解Hibernate: Hibernate能够直接通过Java对象操纵数据库,也能直接通过数据库得到Java对象
第一个Hibernate程序:
1>:导包
官方下载5.2fina版
required中全部包 jpa-metamodel-generator中全部包 jpa中全部包
java连接mysql包(去官网下载)
2>:编写Hibernate全局配置 hibernate.cfg.xml(在src文件夹写直接编写)
<?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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate_day1</property>
<property name="connection.username">root</property>
<property name="connection.password">0000</property>
<!-- 数据库方言配置 针对不同的数据库配置不同的信息,例子是mysql -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 是否将hibernate编写的数据库语句输出打控制台上(可检查其是否正确)-->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- update 更新 如果存在则更新语句,如果不存在就创建表格
create 直接创建表格 -->
<mapping resource="com/pzhu/pojo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3>:编写实体类
User (id,username,password三个字段生成getter,setter方法)
package com.pzhu.pojo;
public class User {
private int id;
private String username;
private String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4>:编写映射关系
映射关系;对象和表之间字段的对应关系 以及约束 类型
在实体类的同级目录下创建User.hbm.xml
<?xml version="1.0"?>
<hibernate-mapping package="com.pzhu.pojo">
<class name="User">
<!-- 配置主键 -->
<id name="id">
<!-- 自增长 -->
<generator class="native"/>
</id>
<!-- 配置普通列 -->
<property name="username"/>
<property name="password"></property>
</class>
</hibernate-mapping>
5>:编写测试类(这儿应该用juit编写单元测试,但是我没有下载资源包)
package com.pzhu.pojo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class test {
public static void main(String[] args) {
Configuration cfg= new Configuration().configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
//处理业务逻辑
User user=new User();
user.setUsername("111");
user.setPassword("111");
session.save(user);
session.getTransaction().commit();
session.close();
factory.close();
}
}
这是第一个小程序,理解hibernate大致的运行过程。