Hibernate5的学习笔记(一)

这篇博客介绍了Hibernate5的入门配置,包括ORM框架的概述和JPA开发的步骤。重点讲解了如何使用JPA注解进行实体类配置,如@Entity、@Table、@Column、@Id等,以及各种关联关系的注解,如@OneToMany、@ManyToOne、@OneToOne、@ManyToMany等。还分享了一个小技巧:在保存数据时,应先保存不维护关联关系的实体,后保存维护关联关系的实体。
摘要由CSDN通过智能技术生成

Hibernate5的入门配置

一、我们喜欢Hibernate的哪些东西

  • 它封装了JDBC对象,让人不用写sql,还是开源的ORM的框架
    (ORM框架有哪些?HIbernate、Ibatis、Mybatis、EclipseLink、JFinal)
  • 将数据库表和实体类对应起来,对数据进行持久化操作(数据持久化(PO)就是将内存中的数据模型转换为存储模型,以及将存储模型转换为内存中的数据模型的统称.数据模型可以是任何数据结构或对象模型,存储模型可以是关系模型、XML、二进制流等。cmp和Hibernate只是对象模型到关系模型之间转换的不同实现)
  • 配置对象关系映射可自由选择,一种是基于xml的方式,另一种是基于annotation的注解方式,个人偏爱JPA注解开发,可以提高开发效率。
    二、怎么使用JPA开发
    1、我们选择使用maven,新建一个项目
    2、接着需要在pom.xml配置我们需要的Hibernate和 Druid,使用注解开发需要hibernate5.3.7.jar,hibernate-annotations- 3.3.0.jar,hibernate-commons-annotations.jar 和ejb3-persistence.jar ,不过我们通过maven可以轻松的获取到,不用太担心。
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.XXX</groupId>
      <artifactId>hibernate-web</artifactId>
      <packaging>pom</packaging>
      <version>1.0.0</version>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>4.11</junit.version>
        <common.jdbc.version>1.0.0</common.jdbc.version>
        <commons.lang3.version>3.1</commons.lang3.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>${junit.version}</version>
          <scope>test</scope>
        </dependency>
    
        <!-- 使用注解开发你得有hibernate5.3.7.jar,
        hibernate-annotations- 3.3.0.jar,
        hibernate-commons-annotations.jar
        和ejb3-persistence.jar 四个jar包 -->
        <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
          <version>5.3.7.Final</version>
        </dependency>
        <dependency>
          <groupId>org.hibernate.common</groupId>
          <artifactId>hibernate-commons-annotations</artifactId>
          <version>5.0.4.Final</version>
        </dependency>
        <dependency>
          <groupId>javax.persistence</groupId>
          <artifactId>persistence-api</artifactId>
          <version>1.0</version>
        </dependency>
        
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.1.9</version>
        </dependency>
      </dependencies>
    
      <distributionManagement>
        <repository>
          <i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值