Hibernate开发(一)

ORM:Object Relational Mapping (对象关系映射)将java中的对象和关系数据库中的表建立一种映射(实现操作对象就可以操作数据中的表)

Hibernate:Hibernate是一个持久层的ORM框架

Hibernate环境搭建:

  1. 首先要导入Hibernate所需要的jar包,mysql驱动,log4j日志的3个jar包
  2. 在mysql中创建表如:customer
  3. 在idea中创建实体类,如:Customer,类中的属性与表中的列名相对应,然后生成get、set方法
  4. 创建映射:
    1. 命名规范:类名.hbm.xml
    2. 引入约束(xml文件的知识):在hibernate-core的jar包下找到hibernate-mapping-3.0.dtd的类似文件找到
      <!DOCTYPE hibernate-mapping PUBLIC 
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

       

    3. 建立映射:

      <hibernate-mapping>
          <!--//建立与表的映射-->
          <class name="com.bean.Customer" table="customer">
              <!--id 是用来建立类中的属性与表中的主键对应-->
              <id name="cust_id" column="cust_id">
                  <generator class="native"></generator>
              </id>
              <!--建立类中的普通属性与表的其他字段相对应-->
              <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_phone" column="cust_phone"></property>
              <property name="cust_mobile" column="cust_mobile"></property>
          </class>
      </hibernate-mapping>

       

  5. hibernate的核心配置文件:
    1. 命名:hibernate.cfg.xml
    2. 引入约束:在hibernate-core的jar包下找到hibernate-configuration-3.0.dtd的类似文件找到:
      <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

       

    3.  基本配置:

      <hibernate-configuration>
          <session-factory>
              <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
              <property name="hibernate.connection.url">jdbc:mysql://localhost/shop?useSSL=false&amp;serverTimezone=Hongkong</property>
              <property name="hibernate.connection.username">root</property>
              <property name="hibernate.connection.password">root</property>
              <!--配置Hibernate方言-->
              <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      
              <mapping resource="com/bean/Customer.hbm.xml"/>
          </session-factory>
      </hibernate-configuration>

       

  6. 编写测试类:
    1. 加载hibernate核心配置文件
    2. 创建一个SessionFactory对象(类似于JDBC连接池)
    3. 通过SessionFactory获取到Session对象(类似于JDBC中的Connection)
    4. 手动开启事务
    5. 编写代码
    6. 提交事务
    7. 释放资源
       @Test
          public void demo1(){
              //  1.加载hibernate核心配置文件
              Configuration configuration = new Configuration().configure();
              //  2.创建一个SessionFactory对象(类似于JDBC连接池)
              SessionFactory sessionFactory = configuration.buildSessionFactory();
              //  3.通过SessionFactory获取到Session对象(类似于JDBC中的Connection)
              Session session=sessionFactory.openSession();
              //  4.手动开启事务
              Transaction transaction = session.beginTransaction();
              //  5.编写代码
              Customer customer = new Customer();
              customer.setCust_name("赵云");
              session.save(customer);
              //  6.提交事务
              transaction.commit();
              //  7.释放资源
              session.close();
          }

       

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值