`

JPA 与 Hibernate 整合的Spring配置

阅读更多

1. 先配置 PersistenceUnit ,在 persistence.xml 中

 

    <?xml version="1.0" encoding="UTF-8"?>
<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="CallBackUnit" transaction-type="RESOURCE_LOCAL">
       
        <!-- 配置JPA的实现提供者 -->
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
       
        <class>com.aaa.callback.dao.entity.TPredialQueue</class>
        <class>com.aaa.callback.dao.entity.TPredialQueueHistory</class>
    </persistence-unit>
   
</persistence>

 

 

2. 在 applicationContext.xml 中配置entityManagerFactory

 

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans.xsd
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring-tx.xsd
                     http://www.springframework.org/schema/aop
                     http://www.springframework.org/schema/aop/spring-aop.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context.xsd ">
        <!-- 配置JPA实体管理器工厂 -->            
       <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">                   
               <property name="persistenceUnitName" value="CallBackUnit" />
               <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
       </bean>

 

</beans>

 

 

3. 配置事务管理器,在 2的配置文件中加入

 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
               <property name="entityManagerFactory">
                   <ref bean="entityManagerFactory" />
               </property>
       </bean>

 

 

上面是配置好了事务管理器,但是要让其起作用还得配置以下的内容

 

4. 配置advice

 

<tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
                <tx:method name="*" propagation="REQUIRED"/>
            </tx:attributes>
        </tx:advice>

 注:这里的 get*是指以get为开头的方法的事件为只读事务,

 

5. 配置aop

 

<aop:config>
            <aop:pointcut expression="execution(public * com.aaa.callback.service..*.*(..))" id="txPointcut"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
        </aop:config>

 

 

 

通过以上步骤之后,基本功能就实现了。

 

 

当然,还要加入 hibernate.properties的配置

 

 

#####################################################################
####Hibernate 属性参数,也可以删除该文件在persistence.xml文件中定义
#####################################################################

#是否显示SQL语句
hibernate.show_sql=true

#是否格式化SQL语句
hibernate.format_sql=true

#数据库更新策略(none,validate,create,create-drop,update)
hibernate.hbm2ddl.auto=update

#自动探测实体(class,hbm)
hibernate.archive.autodetection=class

#Hibernate 连接设置
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost/test?useUnicode=true&amp;characterEncoding=UTF-8
hibernate.connection.username=root
hibernate.connection.password=123456

#C3P0连接池配置
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=300
hibernate.c3p0.max_statements=50
hibernate.c3p0.acquire_increment=1
hibernate.c3p0.idle_test_period=3000

#是否使用查询缓存
hibernate.cache.use_query_cache=true

#是否使用二级缓存
hibernate.cache.use_second_level_cache=false

#二级缓存实现提供者
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

#Hibernate方言设置,生成特定数据库和优化的SQL语句
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

 

分享到:
评论

相关推荐

    spring mvc 与JPA/Hibernate的整合示例

    总之,这个示例将指导开发者如何将Spring MVC、JPA和Hibernate整合到同一个项目中,实现一个功能完善的Web应用,同时提供了一种组织和管理代码的方式。通过理解这些组件的协同工作,开发者可以更好地掌握Java Web...

    Spring + JPA + Hibernate配置

    标题“Spring + JPA + Hibernate配置”涉及到的是Java开发中常用的三个框架——Spring、Java Persistence API (JPA) 和Hibernate的集成与配置。这是一份关于如何将这些框架结合使用的教程或参考资料,可能包含了实现...

    SpringMVC+Spring+SpringDataJPA+Hibernate整合登录的效果

    这是整合SpringMVC+Spring+SpringDataJPA+Hibernate简单的实现登录的功能,用的是mysql数据库,这是一个web Project 如果你用的是JavaEE6那么你要注意bean-validator.jar和weld-osgi-bundle.jar与slf4j的jar包冲突。...

    spring+springMVC+jpa+hibernate框架整合

    在IT领域,构建高效、可扩展的Web应用是至关重要的,而"spring+springMVC+jpa+hibernate框架整合"就是一个常见的解决方案。这个整合涉及到四个关键的技术栈:Spring框架、SpringMVC、JPA(Java Persistence API)...

    Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA

    在现代Java Web开发中,"Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA"是一个常见的架构组合,被广泛应用于构建企业级应用程序。这个组合通常被称为"SSM",其中"M"代表Maven,"S"代表Spring,包括Spring核心...

    Struts+Spring+Jpa(hibernate)整合

    在进行整合时,开发者需要配置Struts2的配置文件struts.xml,Spring的配置文件applicationContext.xml,以及JPA的配置文件persistence.xml。同时,还需要确保所有依赖库正确导入,以避免类加载冲突。通过这样的整合...

    hibernate与spring整合demo

    将Hibernate与Spring整合可以充分利用两者的优点,提高开发效率并降低复杂性。 一、Hibernate概述 Hibernate是Java世界中领先的ORM框架之一,它允许开发者用Java对象来操作数据库记录,而无需编写SQL语句。通过配置...

    Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA+JdbcTemplate

    在"Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA+JdbcTemplate"的DEMO中,开发者通常会创建一个Maven工程,配置相关依赖,然后分别设置Spring的配置文件(如`applicationContext.xml`)、SpringMVC的配置文件...

    springmvc+springjpa+hibernate整合简例

    总结来说,"springmvc+springjpa+hibernate整合简例"涉及到的主要知识点包括: 1. SpringMVC的请求处理流程和配置。 2. SpringJPA的使用,包括实体和数据访问接口的定义。 3. Hibernate作为JPA提供者的配置和特性...

    velocity spring jpa hibernate 整合

    在这个项目中,"velocity spring jpa hibernate 整合"涉及到的是四个关键组件:Velocity、Spring、JPA(Java Persistence API)和Hibernate。让我们逐一深入理解这些技术并探讨它们如何协同工作。 1. **Velocity**...

    Spring+Jersey+JPA+Hibernate+MySQL整合

    开发者可以通过这些源码学习如何将Spring、Jersey、JPA和Hibernate整合在一起,实现前后端交互,以及如何进行单元测试。例如,Jersey的单元测试可能涉及使用`@Test`注解的JUnit测试,模拟HTTP请求,并验证返回的响应...

    Spring集成Jpa(Hibernate实现)

    这个"Spring集成JPA(Hibernate实现)"的例子,涵盖了从基础配置到实际使用的完整流程,对于理解Spring与JPA的集成非常有帮助。通过这个示例,开发者能够学习如何避免因版本不兼容而引发的问题,更好地掌握Spring与...

    maven+springjpa+hibernate

    【 Maven + Spring Data JPA + Hibernate 整合详解】 在现代Java开发中,Maven、Spring Data JPA和Hibernate是三个非常重要的工具和技术。Maven是项目管理工具,Spring Data JPA提供了对数据库操作的高级抽象,而...

    将_Shiro_作为应用的权限基础_五:SpringMVC+Apache_Shiro+JPA(hibernate)整合配置

    将_Shiro_作为应用的权限基础_五:SpringMVC+Apache_Shiro+JPA(hibernate)整合配置

    spring+springmvc+jpa(hibernate)框架整合

    整合Spring、SpringMVC和Hibernate,首先需要在项目中引入相关依赖,通常通过Maven或Gradle来管理。在pom.xml或build.gradle文件中,需要添加Spring、SpringMVC和Hibernate的依赖库。 接着,配置Spring的...

    Spring+hibernate整合源代码

    这个“Spring+hibernate整合源代码”应该包含了实现上述整合步骤的示例代码,可以作为学习和参考的资源。通过学习和实践这些代码,你可以更好地理解和掌握 Spring 和 Hibernate 整合的细节,提升你的 Java Web 开发...

    spring_jpa_hibernate_shiro整合(maven)

    本项目整合spring_jpa_hibernate_shiro,spring_jpa_hibernate_shiro.zip 里有代码,spring_jpa_hibernate常用的小弟就没有单独说明了,在项目配置文件里写得比较清楚,小弟在主要是shiro权限控制做了单独说明,如有...

    Spring+JPA(hibernate)整合 一对多及继承关系配置

    NULL 博文链接:https://prowl.iteye.com/blog/519618

    springmvc4+hibernate4 jpa实现整合(含jar包)

    通过参考和引用传智播客的免费教程,将springmvc4.1.6与hibernate4.3.10提供的JPA实现整合,使用mysql5.6.25,在MyEclipse2014中测试通过。可以作为web开发的基础框架使用。 使用说明: 1.需要安装mysql,并创建名为...

    springmvc spring hibernate jpa maven 整合

    在项目中,Spring与Hibernate或JPA的结合使用,使得数据操作变得简单,如保存用户数据到数据库。 Maven是项目管理和构建工具,它管理项目的依赖关系,自动化构建过程,包括编译、测试、打包和部署。在"ajie-wechat...

Global site tag (gtag.js) - Google Analytics