spring 整合 jpa

jpa 整合spring

一、导入jar包

antlr-2.7.7.jar
dom4j-1.6.1.jar
ehcache-core-2.4.3.jar
hamcrest-core-1.3.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.4.Final.jar
hibernate-ehcache-4.2.4.Final.jar
hibernate-entitymanager-4.2.4.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
junit-4.10.jar
mysql-connector-java-5.1.7-bin.jar
slf4j-api-1.6.1.jar

二、配置spring 配置文件application.xml

1、导入数据源

<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="driverClass" value="${jdbc.driver}"/>
bean>

2、配置jpaFactionBean

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

    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    property>

    <property name="packagesToScan" value="com.kuang"/>

    

    <property name="jpaProperties">
        <props>
            <prop key="hibernate.show_sql">trueprop>
            <prop key="hibernate.format_sql">trueprop
             key="hibernate.hbm2ddl.auto">updateprop>
        props>
    property>
bean>

3、配置事务

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

完整配置


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        ">

    <context:property-placeholder location="jdbc.properties"/>

    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
    bean>

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

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        property>

        <property name="packagesToScan" value="com.kuang"/>

        

        <property name="jpaProperties">
            <props>
                <prop key="hibernate.show_sql">trueprop>
                <prop key="hibernate.format_sql">trueprop
                 key="hibernate.hbm2ddl.auto">updateprop>
            props>
        property>
    bean>

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

    <tx:annotation-driven transaction-manager="transactionManager"/>

beans>

你可能感兴趣的:(学习记录,spring,jpa,hibernate)