JPA学习---第二节:JPA开发环境和思想介绍

一、下载相关 jar

http://hibernate.org/orm/ 下载 hibernate ,解压

http://www.slf4j.org/download.html 下载 slf4j,解压

http://www.apache.org/dyn/closer.cgi/logging/log4j/2.0.2/apache-log4j-2.0.2-bin.zip 下载 log4j, 解压

二、开发 JPA 依赖的jar

hibernate-release-4.3.6.Finallib equired*.jar

hibernate-release-4.3.6.Finallibjpahibernate-entitymanager-4.3.6.Final.jar

hibernate-release-4.3.6.Finalliboptionalehcacheslf4j-api-1.6.1.jar

三、 JPA 配置文件

JPA规范要求在类路径的META-INF目录下放置persistence.xml,文件的名称是固定的,配置模版如下:

<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
                http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
    version="1.0">
  <persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
         <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
         <property name="hibernate.connection.username" value="root"/>
         <property name="hibernate.connection.password" value="123456"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
      </properties>
  </persistence-unit>
</persistence>

比较:

1、先建表后根据表来编写配置文件和实体bean。这是传统的数据库建模方案。

2、先编写配置文件和实体类bean,然后再生成表。这是领域建模思想方案,这种思想相对于前一种更加的 OOP。

原文地址:https://www.cnblogs.com/hwlsniper/p/4077220.html