1、Hibernate概述
Hibernate是一个开放源代码的对象关系映射(ORM, Object Relation Mapping)框架,是一个轻量级的JavaEE应用的持久层解决方案,它对JDBC进行了非常轻量级的封装,使得Java程序员可以使用面向对象编程的思维来操作数据库。
2、Hibernate开发步骤
3、简单测试案例
创建数据库表结构
1 Create database hibernate_day01; 2 Use hibernate_day01; 3 CREATE TABLE `cst_customer` ( 4 `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT ‘客户编号(主键)‘, 5 `cust_name` varchar(32) NOT NULL COMMENT ‘客户名称(公司名称)‘, 6 `cust_user_id` bigint(32) DEFAULT NULL COMMENT ‘负责人id‘, 7 `cust_create_id` bigint(32) DEFAULT NULL COMMENT ‘创建人id‘, 8 `cust_source` varchar(32) DEFAULT NULL COMMENT ‘客户信息来源‘, 9 `cust_industry` varchar(32) DEFAULT NULL COMMENT ‘客户所属行业‘, 10 `cust_level` varchar(32) DEFAULT NULL COMMENT ‘客户级别‘, 11 `cust_linkman` varchar(64) DEFAULT NULL COMMENT ‘联系人‘, 12 `cust_phone` varchar(64) DEFAULT NULL COMMENT ‘固定电话‘, 13 `cust_mobile` varchar(16) DEFAULT NULL COMMENT ‘移动电话‘, 14 PRIMARY KEY (`cust_id`) 15 ) ENGINE=InnoDB AUTO_INCREMENT=94 DEFAULT CHARSET=utf8;
编写JavaBean实体类
1 package com.alphajuns.domain; 2 3 public class Customer { 4 /*`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT ‘客户编号(主键)‘, 5 `cust_name` varchar(32) NOT NULL COMMENT ‘客户名称(公司名称)‘, 6 `cust_user_id` bigint(32) DEFAULT NULL COMMENT ‘负责人id‘, 7 `cust_create_id` bigint(32) DEFAULT NULL COMMENT ‘创建人id‘, 8 `cust_source` varchar(32) DEFAULT NULL COMMENT ‘客户信息来源‘, 9 `cust_industry` varchar(32) DEFAULT NULL COMMENT ‘客户所属行业‘, 10 `cust_level` varchar(32) DEFAULT NULL COMMENT ‘客户级别‘, 11 `cust_linkman` varchar(64) DEFAULT NULL COMMENT ‘联系人‘, 12 `cust_phone` varchar(64) DEFAULT NULL COMMENT ‘固定电话‘, 13 `cust_mobile` varchar(16) DEFAULT NULL COMMENT ‘移动电话‘,*/ 14 15 private Long cust_id; 16 private String cust_name; 17 private Long cust_user_id; 18 private Long cust_create_id; 19 private String cust_source; 20 private String cust_industry; 21 private String cust_level; 22 private String cust_linkman; 23 private String cust_phone; 24 private String cust_mobile; 25 26 public Long getCust_id() { 27 return cust_id; 28 } 29 public void setCust_id(Long cust_id) { 30 this.cust_id = cust_id; 31 } 32 public String getCust_name() { 33 return cust_name; 34 } 35 public void setCust_name(String cust_name) { 36 this.cust_name = cust_name; 37 } 38 public Long getCust_user_id() { 39 return cust_user_id; 40 } 41 public void setCust_user_id(Long cust_user_id) { 42 this.cust_user_id = cust_user_id; 43 } 44 public Long getCust_create_id() { 45 return cust_create_id; 46 } 47 public void setCust_create_id(Long cust_create_id) { 48 this.cust_create_id = cust_create_id; 49 } 50 public String getCust_source() { 51 return cust_source; 52 } 53 public void setCust_source(String cust_source) { 54 this.cust_source = cust_source; 55 } 56 public String getCust_industry() { 57 return cust_industry; 58 } 59 public void setCust_industry(String cust_industry) { 60 this.cust_industry = cust_industry; 61 } 62 public String getCust_level() { 63 return cust_level; 64 } 65 public void setCust_level(String cust_level) { 66 this.cust_level = cust_level; 67 } 68 public String getCust_linkman() { 69 return cust_linkman; 70 } 71 public void setCust_linkman(String cust_linkman) { 72 this.cust_linkman = cust_linkman; 73 } 74 public String getCust_phone() { 75 return cust_phone; 76 } 77 public void setCust_phone(String cust_phone) { 78 this.cust_phone = cust_phone; 79 } 80 public String getCust_mobile() { 81 return cust_mobile; 82 } 83 public void setCust_mobile(String cust_mobile) { 84 this.cust_mobile = cust_mobile; 85 } 86 87 @Override 88 public String toString() { 89 return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_user_id=" + cust_user_id 90 + ", cust_create_id=" + cust_create_id + ", cust_source=" + cust_source + ", cust_industry=" 91 + cust_industry + ", cust_level=" + cust_level + ", cust_linkman=" + cust_linkman + ", cust_phone=" 92 + cust_phone + ", cust_mobile=" + cust_mobile + "]"; 93 } 94 95 }
编写映射配置文件
1 1 <?xml version="1.0" encoding="UTF-8"?> 2 2 <!DOCTYPE hibernate-mapping PUBLIC 3 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 5 <hibernate-mapping> 6 6 7 7 <!-- 配置类和表结构的映射 name:JavaBean类的全路径名 table:表名 --> 8 8 <class name="com.alphajuns.domain.Customer" table="cst_customer"> 9 9 <!-- 配置id name:JavaBean成员属性 column:表主键字段 --> 10 10 <id name="cust_id" column="cust_id"> 11 11 <!-- 主键生成策略 native表示采用本地策略 --> 12 12 <generator class="native" /> 13 13 </id> 14 14 15 15 <!-- 配置其它属性 --> 16 16 <property name="cust_name" column="cust_name" /> 17 17 <property name="cust_user_id" column="cust_user_id" /> 18 18 <property name="cust_create_id" column="cust_create_id" /> 19 19 <property name="cust_source" column="cust_source" /> 20 20 <property name="cust_industry" column="cust_industry" /> 21 21 <property name="cust_level" column="cust_level" /> 22 22 <property name="cust_linkman" column="cust_linkman" /> 23 23 <property name="cust_phone" column="cust_phone" /> 24 24 <property name="cust_mobile" column="cust_mobile" /> 25 25 </class> 26 26 </hibernate-mapping>
编写Hibernate核心配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <!-- 先配置SessionFactory 一个数据库对应一个SessionFactory --> 8 <session-factory> 9 10 <!-- 必须配置项5个,数据库驱动、url、用户名、密码、数据库的方言 --> 11 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 12 <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property> 13 <property name="hibernate.connection.username">root</property> 14 <property name="hibernate.connection.password">root</property> 15 16 <!-- 数据库的方言 指明为某种数据库特有 --> 17 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 18 19 <!-- 可选配置项 --> 20 <!-- 在控制台显示SQL语句 --> 21 <property name="hibernate.show_sql">true</property> 22 <!-- 格式化sql语句 --> 23 <property name="hibernate.format_sql">true</property> 24 <!-- 生成数据库的表结构 update:没有表时创建表,表存在时,直接对表进行更新 --> 25 <property name="hibernate.hbm2ddl.auto">update</property> 26 27 <!-- 引入映射配置文件 分隔符为/ --> 28 <mapping resource="com/alphajuns/domain/Customer.hbm.xml" /> 29 30 </session-factory> 31 </hibernate-configuration>
测试配置
1 package com.alphajuns.test; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.Transaction; 6 import org.hibernate.cfg.Configuration; 7 import org.junit.Test; 8 9 import com.alphajuns.domain.Customer; 10 11 /* 12 * 测试hibernate框架 13 */ 14 public class Demo { 15 16 @Test 17 public void test1() { 18 /* 19 * 1、加载配置文件 20 * 2、创建SessionFactory对象,生成Session对象 21 * 3、创建Session对象 22 * 4、开启事务 23 * 5、编写保存的代码 24 * 6、提交事务 25 * 7、释放资源 26 */ 27 // 1、加载配置文件 28 Configuration config = new Configuration(); 29 // 默认加载src目录下的hibernate.cfg.xml 30 config.configure(); 31 // 2、创建SessionFactory对象 32 SessionFactory factory = config.buildSessionFactory(); 33 // 3、创建Session对象 34 Session session = factory.openSession(); 35 // 4、开启事务 36 Transaction tr = session.beginTransaction(); 37 // 5、编写保存的代码 38 Customer c = new Customer(); 39 c.setCust_name("中国移动"); 40 c.setCust_level("2"); 41 c.setCust_mobile("10086"); 42 // 保存对象,操作对象就相当于在操作表 43 session.save(c); 44 // 6、提交事务 45 tr.commit(); 46 // 7、释放资源 47 session.close(); 48 factory.close(); 49 } 50 51 }
原文:https://www.cnblogs.com/alphajuns/p/10004526.html