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