您的位置:首页 > 编程语言 > Java开发

spring data jpa 配置

2015-08-31 15:48 453 查看
项目是面向JPA编程,使用了spring data框架。

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

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context"

    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"

    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    <!-- DataSource   数据源的配置 -->

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"

        p:driverClassName="${jdbc.driverClassName}"

        p:url="${jdbc.url}"

        p:username="${jdbc.username}"

        p:password="${jdbc.password}"

        p:initialSize="${jdbc.pool.initialSize}"

        p:minIdle="${jdbc.pool.minIdle}"

        p:maxActive="${jdbc.pool.maxActive}"

        p:maxWait="${jdbc.pool.maxWait}"

        p:timeBetweenEvictionRunsMillis="${jdbc.pool.timeBetweenEvictionRunsMillis}"

        p:minEvictableIdleTimeMillis="${jdbc.pool.minEvictableIdleTimeMillis}"

        p:validationQuery="SELECT 1 FROM DUAL"

        p:testWhileIdle="true"

        p:testOnBorrow="false"

        p:testOnReturn="false"

        p:poolPreparedStatements="${jdbc.pool.poolPreparedStatements}"

        p:maxPoolPreparedStatementPerConnectionSize="${jdbc.pool.maxPoolPreparedStatementPerConnectionSize}"

        p:filters="stat" />

    <!-- Jpa Entity Manager 配置 -->

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"

        p:dataSource-ref="dataSource"

        p:jpaVendorAdapter-ref="hibernateJpaVendorAdapter"

        p:packagesToScan="com.coamctech.sample.demo.entity"

        p:jpaProperties-ref="jpaProperties" />

    

    <util:map id="jpaProperties">

        <entry key="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" /> <!-- 命名规则 myName -> my_name -->
        <entry key="hibernate.hbm2ddl.auto" value="update" />
<!-- 根据实体生成数据库表 -->          //这里是根据JPA生成数据表,有四种方法,update,create,validate,create-drop.create—hibernate加载时删除原有的数据表结构,然后重新生成数据表,容易导致数据表丢失。

validate—加载hibernate时,验证创建的数据表结构,只会和有的数据表进行比较,但不创建,会插入新值。

create-drop—加载hibernate时,更具model类生成表,但sessionFactory一关闭,表就自动删除。

upadate—加载hibernate时更新数据表结构。

    </util:map>

    <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"

        p:databasePlatform-ref="databasePlatform" />

    

    <!-- 根据数据源获取具体的数据库方言 -->

    <bean id="databasePlatform" factory-method="getDialect" class="org.springside.modules.persistence.Hibernates"

        c:dataSource-ref="dataSource" />

    <!-- Spring Data Jpa配置 -->

    <jpa:repositories base-package="com.coamctech.sample.demo.repository"
        transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory" />

    <!-- 声明一个Spring提供的JPA事务管理器,传入的参数是Spring中的实体管理器工厂 -->

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"

        p:entityManagerFactory-ref="entityManagerFactory" />

    <!-- 使用annotation定义事务 -->

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: