1 下载Hibernate5

http://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/hibernate-release-5.0.7.Final.zip/download

项目目录:

Hibernate的入门:-LMLPHP

2 创建表

 Create database hibernate_day01;
Use hibernate_day01;
CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_user_id` bigint(32) DEFAULT NULL COMMENT '负责人id',
`cust_create_id` bigint(32) DEFAULT NULL COMMENT '创建人id',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
`cust_linkman` varchar(64) 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=94 DEFAULT CHARSET=utf8;

3 引入Hibernate的开发包

数据库驱动包:

Hibernate/lib/required/*

引入日志记录的包:

Hibernate的入门:-LMLPHP

4  创建实体

package com.itheima.domain;

/*
* 客户的javaBean
* @author chenyanlong
*/
public class Customer {
private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public Long getCust_user_id() {
return cust_user_id;
}
public void setCust_user_id(Long cust_user_id) {
this.cust_user_id = cust_user_id;
}
public Long getCust_create_id() {
return cust_create_id;
}
public void setCust_create_id(Long cust_create_id) {
this.cust_create_id = cust_create_id;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
} }

5 创建映射

 <?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.itheima.domain.Customer" table="cst_customer">
<!-- 配置id
name属性,JavaBean的属性
column属性,是表结构的字段
-->
<id name="cust_id" column="cust_id">
<!-- 只要是主键 需要有一个主键的生成策略: -->
<generator class="native"/> </id> <!-- 非主键的属性都使用property标签配置映射 name是类中的属性名 column表中字段名 -->
<property name="cust_name" column="cust_name"></property>
<property name="cust_user_id" column="cust_user_id"></property>
<property name="cust_create_id" column="cust_create_id"></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_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property> </class>
</hibernate-mapping>

6 创建Hibernate的核心配置文件

 <?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>
<!-- 记住:先配置sessionFactoryy --> <session-factory>
<!-- 必须的配置 -->
<!-- 配置连接数据库的基本的信息: -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property> <!-- 数据库的方言: -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Hibernate的可选项 --> <!-- 加载映射 -->
<mapping resource="com/itheima/domain/Customer.hbm.xml"/> </session-factory>
</hibernate-configuration>

7 编写代码

 package com.itheima.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test; import com.itheima.domain.Customer; /*
* 测试Hibernate框架
* @author chenyanlong
*/
public class Demo1 { @Test
public void testSave(){
/*
* 1.先加载配置文件
* 2.创建SessionFactory对象,生成Session对象
* 3.创建Sesison对象
* 4.开启事务
* 5.编写保存代码
* 6.提交事务
* 7.释放资源
*/ //1.加载配置文件
Configuration config=new Configuration();
//默认加载src目录下hibernate.cfg.xml的配置文件
config.configure();
//2.创建SessionFactory对象
SessionFactory factory=config.buildSessionFactory();
//3.创建session对象
Session session=factory.openSession();
//4.开启事务
Transaction tr= session.beginTransaction(); //5.编写保存代码
Customer customer = new Customer();
customer.setCust_name("小王");
customer.setCust_source("小广告"); session.save(customer);
//6.提交事务
tr.commit(); //7.释放资源
session.close();
factory.close();
}
}

8 运行效果

Hibernate的入门:-LMLPHP

05-08 14:52