Hibernate1

Hibernate:
 轻量级的jdbc封装,可以使用Hibernate完成jdbc的操作,在dao层使用.
 对象关系映射(ORM)
 使用:
  1、导入lib/required下的jar包
  2、导入数据库的驱动jar包
  3、导入日志相关的jar包
  4、将hibernate/project/etc/log4j.properties文件导入工程的src下
 配置:
  1、hibernate.cfg.xml-->hibernate的核心配置,在工程的src下
  2、xxx.hbl.xml-->描述类与数据库中表的映射关系的配置,与实体类在同一个包下

 映射配置文件:
  名称:类名.hbl.xml
  约束:在hibernate-core核心jar包下,hibernate-mapping-3.0.dtd
  配置:<hibernate-mapping> -->可以通用声明包名,class中就不需写类的全名package="包名"
   <class name="实体类的全路径" tabler="对应表名" catalog="对应数据库名">
   <id name="主键对应的属性名" column="主键名>  <!-- 描述主键 -->
   <generator class="native"></generator>  <!-- 主键生成策略(自动增长) -->
   </id>

   <property name="实体类对应的属性名" column="字段名" length="长度" type="类型"></property>
   <property name="实体类对应的属性名">
   <column name="字段名" length="长度" sql-type="sql数据类型"></column>
   </property>
    </class>
    </hibernate-mapping>
 
 核心配置文件:
  名称:hibernate.cfg.xml/hibernate.properties
  约束:在hibernate-core核心jar包下,hibernate-configuration-3.0.dtd
  配置:可以参考hibernate-release-5.0.7.Final\project\etc\hibernate.properties文件导入工程的src下
    <hibernate-configuration>
    <session-factory>
     <!-- 数据库的四要素 -->
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
     <property name="hibernate.connection.url">jdbc:mysql:///hibernateTest</property>
     <property name="hibernate.connection.username">root</property>
     <property name="hibernate.connection.password">root</property>

     <!-- 设置连接池提供者 -->

     <property         name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

     <!-- c3p0连接池的设置 -->
     <property name="hibernate.c3p0.max_size">20</property> <!-- 最大连接池 -->
     <property name="hibernate.c3p0.min_size">5</property> <!-- 最小连接数 -->
     <property name="hibernate.c3p0.timeout">120</property> <!-- 超时时间 -->
     <property name="hibernate.c3p0.idle_test_period">3000</property> <!-- 空闲连接时间 -->
    
     <!-- 显示想数据库查询的sql -->
     <property name="hibernate.show_sql">true</property>
     <!-- 格式化sql -->
     <property name="hibernate.format_sql">true</property>
     <!-- hibernate的方言,表示使用的是哪种数据库语言 -->
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

     <!-- 自动创建表 -->
     <property name="hibernate.hbm2ddl.auto">update</property>

     <!-- 用于设置事务提交方式 -->
     <property name="hibernate.connection.autocommit">false</property>

    <!-- hibernate映射文件的位置 -->
    <mapping resource="映射配置文件的位置"/>
   <session-factory>
   </hibernate-configuration>
  Create-drop 每次都会创建一个新的表,执行完成后删除。一般在测试中使用
  Create   每次都会创建一个新的表,一般是在测试中使用
  update 如果数据库中有表,不创建,没有表创建,如果映射不匹配,会自动更新表结构(只能添加) 【*****】
  validate  只会使用存在的表,并且会对映射关系进行校验.

入门案例:
 Configuration config = new Configuration().configure(); //加载hibernate.cfg.xml
 SessionFactory sessionFactory = config.buildSessionFactory();
 Session session = sessionFactory.openSession(); //相当于获得Connection
 session.beginTransaction();
 业务操作
 session.getTransaction().commit();
 session.close();
 sessionFactory.close();
 
 业务操作:
 session.save(Object obj); -->insert

 session.get(Object.class, Serializable ser);  -->select

 Object obj = session.get(Object.class, Serializable ser);
 ...
 session.update(obj);  -->update

 Object obj = session.get(Object.class, Serializable ser);
 session.delete(obj); -->delete

 Query query = session.createQuery("from 实体类名");
 List<Object> list = query.list;  -->select All
 
hibernate工作原理:
 1、通过Configuration().configure();读取并解析hibernate.cfg.xml配置文件。
 2、由hibernate.cfg.xml中的<mappingresource="com/xx/User.hbm.xml"/>读取解析映射信息。
 3、通过config.buildSessionFactory();//得到sessionFactory。
 4、sessionFactory.openSession();//得到session。
 5、session.beginTransaction();//开启事务。
 6、persistent operate;
 7、session.getTransaction().commit();//提交事务
 8、关闭session;
 9、关闭sessionFactory; 

Hibernate常用的API:【*****】
Hibernate的核心类和接口一共有6个,分别为:Session、SessionFactory、Transaction、Query、Criteria和Configuration

 Configuration:
  它主要是用于加载hibernate配置.
  Configuration config=new Configuration().config(); 主要加载src下的hibernate.cfg.xml
  Configuration config=new Configuration();主要加载的src下的hibernate.properties
  Configuration config=new Configuration().config(核心配置文件名称);加载指定的名称的配置文件

 SessionFactory:
  通过Configuration获得
  SessionFactory接口负责初始化Hibernate。
  它充当数据存储源的代理,并负责创建Session对象。
  这里用到了工厂模式。
  需要注意的是SessionFactory并不是轻量级的,
  因为一般情况下,一个项目通常只需要一个SessionFactory就够,
  当需要操作多个数据库时,可以为每个数据库指定一个SessionFactory。
  如何获得:
   通过SessionFactory可以得到Session.-->openSession();
   从连接池中获取一个连接-->getCurrentSession();
   如何保证一个项目中所使用的SessionFactory是同一个:
    工具类:
    private static Configuration config;
    private static SessionFactory sessionFactory;

    static{
     config = new Configuration().configure();
     sessionFactory = config.buildSessionFactory();
    }

    public static Session openSession() {
     return sessionFactory.openSession();
    }

 Session:
  Session接口负责执行被持久化对象的CRUD操作(CRUD的任务是完成与数据库的交流,包含了很多常见的SQL语句)。
  但需要注意的是Session对象是非线程安全的。
  如何解决Session的线程安全问题:
   方法内部来使用Session就可以
  如何获得Session:
   SessionFactory.openSession();  相当于直接通过SessionFactory创建一个新的Session,使用完成后要手动调用    close来关闭。
   SessionFactory.getCurrentSession(); 获取一个与线程绑定的Session,当我们提交或事务回滚后会自动关闭。
  常用方法:
   save  保存对象
   update 修改操作
   delete删除
   get/load 根据id进行查询
   savenOrUpdate 执行save或update操作
   createQuery()获取一个Query对象
   CreateSQLQUery()获取一个可以操作sql的SQLQuery对象
   createCriteria() 获取一个Criteria它可以完成条件查询

 Transaction:
  Transaction接口主要用于管理事务,hibernate的事务接口,对底层的事务进行了封装。使用它可以进行事务操作。
  如果在程序中没有开启事务,有事务,session的每一个操作就会开启一个事务。
  默认情况下事务是不会自动提交的。
  如何获得:
   Session.beginTransaction();

 Query:
  Query接口让你方便地对数据库及持久对象进行查询,它可以有两种表达方式:HQL语言或本地数据库的SQL语句。
  Query经常被用来绑定查询参数、限制查询记录数量,并最终执行查询操作。
  通过Query可以执行hql语句:
   Query query=Session.createQuery(hql)
  通过SQLQuery可以执行sql语句:
   SQLQUery sqlQuery=Session.createSQLQuery(sql);
   SQLQUery是Query的子类

 Criteria:
  Criteria接口与Query接口非常类似,允许创建并执行面向对象的标准化查询。
  值得注意的是Criteria接口也是轻量级的,它不能在Session之外使用
  如何获得:
   Criteria criteria=Session.createCriteria();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值