一、Hibernate导包
二、如何搭建一个Hibernate环境
搭建一个Hibernate环境,开发步骤:
1. 下载源码
版本:hibernate-distribution-3.6.0.Final
2. 引入jar文件
hibernate3.jar核心 + required 必须引入的(6个) + jpa 目录 + 数据库驱动包
3. 写对象以及对象的映射
Employee.java 对象
Employee.hbm.xml 对象的映射 (映射文件)
4. src/hibernate.cfg.xml 主配置文件
- 数据库连接配置
- 加载所用的映射(*.hbm.xml)
5. App.java 测试
三、案例
sql脚本
CREATE TABLE USER(
u_id VARCHAR(100) PRIMARY KEY ,
u_name VARCHAR(100),
u_birth DATETIME
)
hibernate.cfg.xml
hibernate.cfg.xml,hibernate总配置文件,将该配置文件放在src/hibernate.cfg.xml。
<!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节点代表一个数据库 -->
<session-factory>
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jsoft</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 2.3 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 3. 加载所有映射 -->
<mapping resource="org/jsoft/demo/UserEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
UserEntity.hbm.xml
UserEntity.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">
<hibernate-mapping package="org.jsoft.demo">
<class name="UserEntity" table="USER">
<!-- 主键 ,映射 -->
<id name="id" column="u_id">
</id>
<!-- 非主键,映射 -->
<property name="name" column="u_name"></property>
<property name="birth" column="u_birth"></property>
</class>
</hibernate-mapping>
UserEntity.java
public class UserEntity {
private String id;
private String name;
private Date birth;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
App.java
public class Demo {
public static void main(String[] args) {
//创建对象
UserEntity u=new UserEntity();
u.setId(UUID.randomUUID().toString());
u.setName("222");
u.setBirth(new Date());
//1.创建配置类,加载配置。
Configuration cf=new Configuration();
cf.configure();//默认读取src/hibernate.cfg.xml
//2.创建工厂,开启session
SessionFactory sessionFactory = cf.buildSessionFactory();
Session session = sessionFactory.openSession();
//开启事务
Transaction tr = session.beginTransaction();
//保存
session.save(u);
//提交事务
tr.commit();
}
}
三、hibernate的执行流程
1. hibernate开发环境搭建
----》 引入jar: hibernate.jar + required + jpa + 驱动包
---》 hibernate.cfg.xml
---》 javabean/*.hbm.xml
---》 Application 测试
2.hibernate api
----》 Configuration
----》 SessionFactory
----》 Session
---》 Transaction
---》Query
---》Criteria
---》SQLQuery
3.配置
---》 主配置
---》映射配置
*.hbm.xml