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>
<?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>
相关文章推荐
- java security接口和类的分布
- 常用的JavaXML解析技术
- JSch - Java实现的SFTP(文件上传详解篇)
- Java中使用jcraft进行SFTP的上传下载
- java 日期相减时分秒
- CRC32之JAVA实现
- [转]Spring Security 可动态授权RBAC权限模块实践
- spring监听器ContextLoaderListener的疑问
- Java IO体系中常用的流分类
- 如何使用Struts 2防止表单重复提交?
- 用Eclipse开发JSF时,是标签出现自动提醒
- java集合中的Iterable接口的简介与运用
- java静态代理和动态代理(JDK&cglib)
- 重温java基础
- Spring之bean
- spring-InitializingBean init-method postConstruct
- openJDK开源项目列表
- Struts2中的两种输入校验方法
- 华为上机测试题(协议数据解析-java)
- 怎样从SpringMVC返回json数据