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

jpa+spring文件配置

2013-01-06 01:44 399 查看
<!-- 1.把数据库的配置和实体类的配置放在persistence.xml文件中,之后再spring配置文件中引用它的实例 -->
<!-- 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="rightsmanagement3PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- 实体配置在这里 -->
<class>com.dada.user.domain.Userinfo</class>
<class>com.dada.role.domain.Role</class>
<class>com.dada.realtionship.domain.RoleFunction</class>
<class>com.dada.realtionship.domain.UserRole</class>
<class>com.dada.module.domain.Module</class>
<class>com.dada.functions.domain.Function</class>
<!-- 数据库配置信息在这里 -->
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="test" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>

</persistence>

<!-- applicationContext.xml的配置文件 -->
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "
xmlns:tx="http://www.springframework.org/schema/tx">

<!-- 这里引用了persistence.xml的配置文件中的 rightsmanagement3PU -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="rightsmanagement3PU" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

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

<!-- 2.把数据库的配置和实体类的配置直接放在applicationContext.xml文件中 -->
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <aop:aspectj-autoproxy/>
<context:component-scan base-package="com.test"/>
<!--  使用数据源和指定persistence.xml位置的方式创建entityManagerFactory,如果使用的不是hibernate JPA实现,
需要在tomcat作一些特殊配置.具体参考手册
注意:使用该方式需要把persistence.xml中的
hibernate.connection.driver_class,hibernate.connection.username,hibernate.connection.password,hibernate.connection.url配置删除
-->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"/>
<property name="user" value="test"/>
<property name="password" value="test"/>
<!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3  -->
<property name="initialPoolSize" value="1"/>
<!-- 连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="1"/>
<!-- 连接池中保留的最大连接数。Default: 15  -->
<property name="maxPoolSize" value="300"/>
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0  -->
<property name="maxIdleTime" value="60"/>
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3  -->
<property name="acquireIncrement" value="5"/>
<!-- 每60秒检查所有连接池中的空闲连接。Default: 0  -->
<property name="idleConnectionTestPeriod" value="60"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>

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

<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置实体的参数 -->
<bean id="compass" class="org.compass.spring.LocalCompassBean">
<property name="classMappings">
<list>
<value>com.test.bean.product.ProductInfo</value>
<value>com.test.bean.product.Brand</value>
<value>com.test.bean.product.ProductStyle</value>
<value>com.test.bean.product.ProductType</value>
</list>
</property>
</bean>
</beans>

<!-- 3.如果数据库配置信息配置在.properties文件中放在并且放在src目录下比如起名字为jdbc.properties -->

<!-- 内容如下-->
driverClass=org.gjt.mm.mysql.Driver
jdbcUrl=jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8
user=root
password=123456

<!-- 那么2中所述的配置文件可以写成如下格式 -->
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <aop:aspectj-autoproxy/>
<context:component-scan base-package="com.test"/>
<!-- 这里配置的是数据库配置文件的位置,下面就可以通过${键}来获取配置文件中的值了 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3  -->
<property name="initialPoolSize" value="1"/>
<!-- 连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="1"/>
<!-- 连接池中保留的最大连接数。Default: 15  -->
<property name="maxPoolSize" value="300"/>
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0  -->
<property name="maxIdleTime" value="60"/>
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3  -->
<property name="acquireIncrement" value="5"/>
<!-- 每60秒检查所有连接池中的空闲连接。Default: 0  -->
<property name="idleConnectionTestPeriod" value="60"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>

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

<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置实体的参数 -->
<bean id="compass" class="org.compass.spring.LocalCompassBean">
<property name="classMappings">
<list>
<value>com.test.bean.product.ProductInfo</value>
<value>com.test.bean.product.Brand</value>
<value>com.test.bean.product.ProductStyle</value>
<value>com.test.bean.product.ProductType</value>
</list>
</property>
</bean>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: