JavaWeb学习之Hibernate框架(一)

一、框架是什么?

        1、框架是用来提高开发效率的;

        2、框架封装好了一些功能,我们需要使用这些功能时,调用即可,不需要再手动实现;

        3、框架可以理解为一个半成品的项目,只需要懂得如何驾驭这些功能即可。

二、hibernate框架是什么?

           

三、hibernate框架的好处

       操作数据库的时候,可以以面向对象的方式来完成,不需要书写SQL语句

四、hibernate是一款orm框架

       orm:object  relational  mapping   对象关系映射

       数据库中的一张表就代表着一个实体类,一个实体类就代表着一个对象,那么对象关系映射就是把对象与数据库中的字段一一对应。

        

       orm分4级

        hibernate属于4级:完全面向对象操作数据库

        mybatis属于2级

        dbutils属于1级

五、hibernate框架的搭建

       1、导包

               

               驱动包

              

          2、创建数据库,准备表、实体

               

           3、书写orm元数据(对象与表的映射配置文件)

                导入约束:

                

                

               实体:

               

              orm元数据:将实体中的所有成员都一一配置

               

          4、书写配置文件    

<?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>
	<!--  #hibernate.dialect org.hibernate.dialect.MySQLDialect
             #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
             #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
             #hibernate.connection.driver_class com.mysql.jdbc.Driver
             #hibernate.connection.url jdbc:mysql:///test
             #hibernate.connection.username gavin
             #hibernate.connection.password-->
	 <!--必须配置的5条 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///crm?useUnicode=true&characterEncoding=UTF8</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.show_sql true   自动打印sql在控制台
		#hibernate.format_sql true  规范格式化打印在控制台的sql-->
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<!-- 配置hibernate操作数据库隔离级别 -->
		<!-- #hibernate.connection.isolation  4 -->
		<property name="hibernate.connection.isolation">4</property>
		<!-- 获得与当前线程绑定session对象时必须要配置的 -->
		<property name="hibernate.current_session_context_class">thread</property>
		<!-- 	如果没有表就会自动生成表,如果有就会自动更新表	
		#hibernate.hbm2ddl.auto update -->
		<property name="hibernate.hbm2ddl.auto">update</property>
        <mapping resource="com/domain/Customer.hbm.xml"/>
        <mapping resource="com/domain/LinkMan.hbm.xml"/>
        <mapping resource="com/domain/User.hbm.xml"/>
        <mapping resource="com/domain/Role.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

  5、书写代码测试

             

配置文件详解

orm元数据

       根元素

        

       class元素

         

       id元素

         

        property元素

         

hibernate主配置文件

        必须要配置的属性(5个)

        

       可选的属性配置(3个)

        

         元数据引入配置

         

单元测试代码

package com.domain;

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

public class Demo {
	@Test
	public void saveCustomer(){
		Configuration conf=new Configuration();
		conf.configure();
		//单例模式   例子
		//工厂模式   例子
		//代理模式   例子
		//创建session的工厂对象
		SessionFactory sessionFactory=conf.buildSessionFactory();
		//会话(数据库   Connection)
		Session session=sessionFactory.openSession();
		//开启事务
		Transaction tx=session.beginTransaction();
		//保存客户信息
		Customer c=new Customer();
		c.setCust_name("aaa");
		
		session.save(c);
		//提交事务
		tx.commit();
		//关闭session
		session.close();
		//关闭sessionFactory
		sessionFactory.close();
	}

}

  

猜你喜欢

转载自www.cnblogs.com/Java-125/p/9138095.html
今日推荐