Hibernate笔记
- Hibernate是一个持久层的ORM框架(对象关系映射)
- ORM:Object Relational Mapping
导入jar包和数据库
- 创建数据库cst_customer和表cst_customer
CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
`cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
创建Java类Customer
- 创建Customer与表cst_customer中的字段一一对应
public class Customer {
private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_mobile;
}
创建映射文件Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.zhengbin.hibernate.demo01.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"/>
</id>
<property name="cust_name" column="cust_name"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
</class>
</hibernate-mapping>
创建核心配置文件hibernate.cfg.xml
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cst_customer?serverTimezone=UTC</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">sunday</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping resource="com/zhengbin/hibernate/demo01/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
测试类
public class HibernateDemo01 {
@Test
public void demo01() {
Configuration configuration=new Configuration().configure();
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction transaction=session.beginTransaction();
Customer customer=new Customer();
customer.setCust_name("CR7");
session.save(customer);
transaction.commit();
session.close();
}
}
hibernate的映射配置
映射的配置
- class标签的配置:标签用来建立类与表之间的映射关系
- 属性:
- name:类的全路径。
- table:表名(类名与表名一致,table可以省略)
- catalog:数据库名
- id标签的配置:标签用来建立类中的属性与表中的主键的映射关系
- 属性:
- name:类中的属性名
- column:表中的字段名(类中的属性名和表中的字段名一致,column可以省略)
- length:长度
- type:类型
- property标签的配置:标签用来建立类中的属性与表中字段的映射关系
- 属性
- name:类中的属性名
- column:表中的字段名
- length:长度
- type:类型
- not-null:设置非空属性
- unique:设置唯一
核心配置hibernate.cfg.xml
- 必须的配置
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cst_customer?serverTimezone=UTC</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">sunday</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
* none:不使用hibernate的自动建表
* create:如果数据库中已经有表了,删除原有表,重新创建,如果没有表,新建表(测试)
* create-drop:如果数据库中已经有表了,删除原有表,执行操作后删除这个表,如果没有表,新建一个表,使用完了删除这个表(测试),必须关闭工厂:sessionFactory.close();
* update:如果数据库中有表,使用原有表,如果没有表,创建新表(更新表结构)
* validate:如果没有表,不会创建,只会使用数据库中原有的表(校验映射和表结构)
<mapping resource="com/zhengbin/hibernate/demo01/Customer.hbm.xml"/>
hibernate的核心API
Configuration:hibernate的配置对象
- 加载核心配置文件
- hibernate.properties
Configuration configuration=new Configuration()
Configuration configuration=new Configuration(