Hibernate学习第一天

什么是Hibernate: 

Hibernate是一个持久层ORM框架。

什么是orm: 

Object Relational Mapping(对象关系映射)。指的是将一个Java中的对象与关系型数据库中的表建立一种映射关系,从而操作对象就可以操作数据库中的表。

下载开发Hibernate环境

hibernate下载

解压Hibernate

  • documentation :Hibernate开发的文档
  • lib :Hibernate开发包
    • required  :Hibernate开发的必须的依赖包
    • optional  :Hibernate开发的可选的jar包
  • project:Hibernate提供的项目

HIbernate引入jar包

  1. 数据库驱动包
  2. Hibernate开发的必须的jar包
  3. Hibernate引入日志记录包

HIbernate 入门测试

1.创建实体类

2.创建对应表结构

3.创建映射(映射需要通过XML的配置文件来完成,这个配置文件可以任意命名。尽量统一命名规范(类名.hbm.xml))

<!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.zsw.demo1.Customer" table="cst_customer">
		<id name="cust_id" column="cust_id">
			<generator class="native"></generator>
		</id>
		<!-- 建立类中的普通的属性和表的字段的对应 -->
		<property name="cust_name" column="cust_name" length="32" />
		<property name="cust_source" column="cust_source" length="32"/>
		<property name="cust_industry" column="cust_industry"/>
		<property name="cust_level" column="cust_level"/>
		<property name="cust_phone" column="cust_phone"/>
		<property name="cust_mobile" column="cust_mobile"/>
	</class>
</hibernate-mapping>

4.创建核心配置文件(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://localhost/hibernate</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123123</property>
		<!-- 打印sql语句 -->
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<!-- hibernate方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 自动建表 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!-- 映射文件 -->
		<mapping resource="com/zsw/demo1/Customer.hbm.xml" />
	</session-factory>
</hibernate-configuration>

5.测试类

package com.zsw.demo1;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateDemo1 {
	public static void main(String[] args) {
		//加载hibernate 核心配置文件
		Configuration configuration = new Configuration().configure();
		//创建一个sessionFactory对象  类似JDbc连接池
		SessionFactory factory = configuration.buildSessionFactory();
		//	获取session
		Session session = factory.openSession();
	
		//开启事务
		Transaction beginTransaction = session.beginTransaction();
		//编写代码  
	    Customer c = new Customer();
		c.setCust_name("zsw");
		session.save(c);
		//事务提交
		beginTransaction.commit();
		//资源回放
		session.close();
		
		
		
	}
}

 

xml配置提示问题

 

 

 

Hibernate映射的配置

  1. 【class标签的配置】
    1. 标签用来建立类与表的映射关系
    2. 属性:
      1. name:类的全路径
      2. table:表名(类名与表名一致,table可以省略)
      3. catalog  :数据库名
  2. 【id标签的配置】
    1. 标签用来建立类中的属性与表中的主键的对应关系
    2. 属性:
      1. name :类中的属性名
      2. column :表中的字段名(类中的属性名和表中的字段名如果一致,column可以省略)
      3. length :长度
      4. type :类型
  3. 【property标签的配置】
    1. 标签用来建立类中的普通属性与表的字段的对应关系
    2. 属性:
      1. name  :类中的属性名
      2. column:表中的字段名
      3. length :长度
      4. type :类型
      5. not-null :设置非空
      6. unique:设置唯一

Hibernate核心配置

  1. 必须的配置
    1. 连接数据库的基本的参数
      1. 驱动类  hibernate.connection.driver_class
      2. url路径 hibernate.connection.url
      3. 用户名 hibernate.connection.username
      4. 密码 hibernate.connection.password
    2. 方言 hibernate.dialect
  2. 可选的配置
    1. 显示SQL         :hibernate.show_sql
    2. 格式化SQL     :hibernate.format_sql
    3. 自动建表        :hibernate.hbm2ddl.auto
      1. none                :不使用hibernate的自动建表
      2. create              :如果数据库中已经有表,删除原有表,重新创建,如果没有表,新建表。(测试)
      3. create-drop     :如果数据库中已经有表,删除原有表,执行操作,删除这个表。如果没有表,新建一个,使用完了删除该表。(测试)
      4. update             :如果数据库中有表,使用原有表,如果没有表,创建新表(更新表结构)
      5. validate           :如果没有表,不会创建表。只会使用数据库中原有的表。(校验映射和表结构)。
  3. 映射文件的引入    
    1. 引入映射文件的位置        <mapping resource="com/zsw/demo1/Customer.hbm.xml" />

Hibernate的核心API

Configuration:Hibernate的配置对象

  1. 作用:

加载核心配置文件 

Configuration cfg = new Configuration().configure();

SessionFactory:Session工厂

SessionFactory内部维护了Hibernate的连接池和Hibernate的二级缓存(不讲)。是线程安全的对象。一个项目创建一个对象即可。

  1.抽取出来的工具类

package com.zsw.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Hibernateutils {
		public static final Configuration cfg;
		public static final SessionFactory sf;
		static {
			cfg = new Configuration().configure();
			sf = cfg.buildSessionFactory();
		}
		
		public static Session openSession() {
			return sf.openSession();
		}
}

Session

Session代表的是Hibernate与数据库的链接对象。不是线程安全的。与数据库交互桥梁。

     session中的api

  1. 保存方法:
    1. Serializable save(Object obj);
    2. 查询方法:
      1. T get(Class c,Serializable id);
      2. T load(Class c,Serializable id);
      3. get方法和load方法的区别?
      4. @Test
        	// 查询:
        	// ***** get方法和load方法的区别
        	public void demo2(){
        		Session session = HibernateUtils.openSession();
        		Transaction tx = session.beginTransaction();
        		/**
        		 * get方法
        		 * 	* 采用的是立即加载,执行到这行代码的时候,就会马上发送SQL语句去查询。
        		 *  * 查询后返回是真实对象本身。
        		 * 	* 查询一个找不到的对象的时候,返回null
        		 * 
        		 * load方法
        		 * 	* 采用的是延迟加载(lazy懒加载),执行到这行代码的时候,不会发送SQL语句,当真正使用这个对象的时候才会发送SQL语句。
        		 *  * 查询后返回的是代理对象。javassist-3.18.1-GA.jar 利用javassist技术产生的代理。
        		 *  * 查询一个找不到的对象的时候,返回ObjectNotFoundException
        		 */
        		// 使用get方法查询
        		/*Customer customer = session.get(Customer.class, 100l); // 发送SQL语句
        		System.out.println(customer);*/
        		
        		// 使用load方法查询
        		Customer customer = session.load(Customer.class, 200l);
        		System.out.println(customer);
        		
        		tx.commit();
        		session.close();
        	}
        	
    3. 修改方法
      1. void update(Object obj);
      2. @Test
        	// 修改操作
        	public void demo3(){
        		Session session = HibernateUtils.openSession();
        		Transaction tx = session.beginTransaction();
        		
        		// 直接创建对象,进行修改
        		/*Customer customer = new Customer();
        		customer.setCust_id(1l);
        		customer.setCust_name("王聪");
        		session.update(customer);*/
        		
        		// 先查询,再修改(推荐)
        		Customer customer = session.get(Customer.class, 1l);
        		customer.setCust_name("王小贱");
        		session.update(customer);
        		
        		tx.commit();
        		session.close();
        	}

         

    4. 删除方法
      1. void delete(Object obj);
    5. 保存或更新

       

      1. void saveOrUpdate(Object obj)
    6. 查询所有

public void demo6(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		// 接收HQL:Hibernate Query Language 面向对象的查询语言
		/*Query query = session.createQuery("from Customer");
		List<Customer> list = query.list();
		for (Customer customer : list) {
			System.out.println(customer);
		}*/
		
		// 接收SQL:
		SQLQuery query = session.createSQLQuery("select * from cst_customer");
		List<Object[]> list = query.list();
		for (Object[] objects : list) {
			System.out.println(Arrays.toString(objects));
		}
		tx.commit();
		session.close();
	}

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值