Hibernate的入门案例
什么是Hibernate?
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。
其实Hibernate就是一个持久层的ORM框架
ORM:对象关系映射
首先下载Hibernate并解压
地址如下
https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/
在eclipse创建javaweb项目并导入包
在eclipse创建javaweb项目并导入包
在 lib/required 可找到部分包
最终导入的全部包如下
1.数据库驱动包
2.必须包
3.日志记录的包
配置数据库的表格
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_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=1 DEFAULT CHARSET=utf8;
总体结构
创建实体Customer.java(持久化类)并配置get和set方法
package cn.zhicheng.domain;
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;
private String cust_linkman;
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 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_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;
}
public String getCust_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
}
配置映射文件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 package="cn.zhicheng.domain">
<!-- 建立数据库表与类的映射 -->
<class name="Customer" table="cst_customer">
<!-- 建立类中属性与表中主键的对应 -->
<id name="cust_id" column="cust_id">
<!-- 配置主键生成策略 -->
<generator class="native"></generator>
</id>
<!-- 建立类中普通 属性对应表的字段的对应-->
<!-- property中还有 length type not-null unique 灯属性-->
<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_linkman" column="cust_linkman" ></property>
<property name="cust_phone" column="cust_phone" ></property>
<property name="cust_mobile" column="cust_mobile" ></property>
</class>
</hibernate-mapping>
配置核心Hibernate核心配置文件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:///hibernate_day01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">a2195261</property>
<!-- 配置数据库的方言为mysql -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 打印sql语句配置,格式化mysql语句(可选) -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 自动创建表 常用有update和vaildate(不会创建表,会校验你的数据库结构和表结构是否一致,不一致报错) -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 引入配置文件,复制Customer配置文件全路径(注意复制路径后要删除src那一段 -->
<mapping resource="cn/zhicheng/domain/Customer.hbn.xml"/>
</session-factory>
</hibernate-configuration>
编写测试类demo01
package cn.zhicheng.domain;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class Demo01 {
@Test
public void add(){
//加载配置核心文件
Configuration configuration = new Configuration().configure();
//建立SessionFactory此对象是线程安全的可以定义全局的,此对象资源消耗大保证在一个web项目中只有一个此对象
SessionFactory sessionFactory = configuration.buildSessionFactory();
//建立session,session代表的是一个链接,类似于JDBC的connection不是线程安全的
//所以session不能定义全局的
Session session = sessionFactory.openSession();
//开启事务并获得操作失误的对象
Transaction transaction = session.beginTransaction();
//执行代码,执行操作 增删改查 操作
//增加
Customer customer = new Customer();
customer.setCust_name("爷爷");
session.save(customer);
/*删除
Customer customer = session.get(Customer.class, 1);
session.delete(customer);*/
/*修改
Customer customer = session.get(Customer.class, 1);
customer.setCust_name("祖父");
session.save(customer);*/
/*查询
Customer customer = session.get(Customer.class,1l);
System.out.println(customer);*/
//提交事务
transaction.commit();
//关闭资源
session.close();
}
}
最终结果
可以在表中看到cust-name有"爷爷"字段