标签:jpa spring整合jpa
虽然JPA开发大多使用hibernate规范来进行,但是和spring整合这一块却存在明显的差异,先来看下spring整合jpa的重要配置
<context:annotation-config /> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="false" /></bean> </property> <property name="jpaProperties"> <props> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <!-- 如果没有数据源可以使用如下配置 --> <!-- <bean id="entityManager" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="mengya"></property> </bean> --> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <tx:annotation-driven transaction-manager="txManager" />
<?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"> <!-- name属性随意--> <persistence-unit name="springmvc" /> </persistence>
接下来就是数据源了,我习惯使用dbcp的数据源配置大致如下:
<?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: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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.gjt.mm.mysql.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/exercise?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="123456" /> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1" /> <!-- 连接池的最大值 --> <property name="maxActive" value="500" /> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2" /> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1" /> </bean> </beans>
@PersistenceContext private EntityManager entityManager = null; @Transactional(propagation=Propagation.REQUIRED) public void save(User u) { entityManager.persist(u); } @Transactional(propagation=Propagation.REQUIRED) public void updateUser(User u) { entityManager.merge(u); }
# # Log4J Settings for log4j 1.2.x (via jakarta-commons-logging) # # The five logging levels used by Log are (in order): # # 1. DEBUG (the least serious) # 2. INFO # 3. WARN # 4. ERROR # 5. FATAL (the most serious) # Set root logger level to WARN and append to stdout log4j.rootLogger=DEBUG, stdout #info #DEBUG #ERROR log4j.appender.stdout=org.apache.log4j.ConsoleAppender #log4j.appender.stdout=org.apache.log4j.FileAppender #log4j.appender.stdout.File=d:\\logs.log log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%d %5p (%c:%L) - %m%n # Print only messages of level ERROR or above in the package noModule. log4j.logger.noModule=ERROR # OpenSymphony Stuff log4j.logger.com.opensymphony=ERROR log4j.logger.freemarker=ERROR log4j.logger.org.hibernate.jdbc=DEBUG #org.hibernate.SQL #org.hibernate.type #org.hibernate.tool.hbm2ddl #org.hibernate.pretty #org.hibernate.cache #org.hibernate.transaction #org.hibernate.jdbc #org.hibernate.hql.AST #org.hibernate.secure # Spring Stuff log4j.logger.org.springframework=ERROR
标签:jpa spring整合jpa
原文地址:http://blog.csdn.net/walkcode/article/details/39267783