集成JPA环境

来源:互联网 发布:java编程艺术 编辑:程序博客网 时间:2024/06/07 01:21

Struts+JPA+spring2.5

集成JPA环境:

1.根据不同的JAP实现产品导入不同的下包到lib,目前使用的Hibernate用到两个压缩包:

Hibernate-3.2.6.ga.zip

Hibernate核心包(包括第三方类库,在lib下)

Hibernate-entitymanager-3.3.2CT1.zip(实现包)

Hibernater关于对JPA规范的一个实现(包括第三方类库,在lib下)

 

2.导入数据库使用下包(实体bean要通过OR映射跟数据库互动)

JPA独立于数据库产品 (更换数据下包+数据库连接地址)

   Mysql下包:mysql-connector-java-3.1.13-bin.jar 放到lib

 

3.JPA配置(jpa规范实现产品会在所在目录寻找配置文件,初始化环境配置)

src目录(类路径webRoot--WEB-INF--class)新建文件夹META-INF

加配置文件:persistence.xml(所在目录跟文件名都不可以改变,

可在hibernate-entitymanager-3.3.1.GA/hibernate-entitymanager-3.3.1.GA/Hibernate

-entitymanager-3.3.1.GA/test-resources/excludehbmpar/META-INF目录下找到)

 

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- example of reference to a cfg.xml file -->

<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(持久化单元:实体bean的集合),-->

    <persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">

       <!-- Provider(JPA实现驱动使用是hibernate) -->

       <provider>org.hibernate.ejb.HibernatePersistence</provider>

       <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.max_fetch_depth" value="3"/>

         <property name="hibernate.hbm2ddl.auto" value="update"/>

            <property name="hibernate.jdbc.fetch_size" value="18"/>

            <property name="hibernate.jdbc.batch_size" value="10"/>

            <property name="hibernate.show_sql" value="false"/>

            <property name="hibernate.format_sql" value="false"/>

       </properties>

    </persistence-unit>

</persistence>

 

Persistence-unit(持久化单元:实体bean的集合)

Provider(JPA实现驱动使用是hibernate)

Dialect(方言) 

原创粉丝点击