首页 > 代码库 > Spring整合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=http://www.mamicode.com/"false" />>
看到这里相信不用我多解释大家应该能明白了,如果使用这种方式要注意src下面必须存在META-INF文件夹且下面还应该放一个persistence.xml,这个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">
	<!-- 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=http://www.mamicode.com/"org.gjt.mm.mysql.Driver" />>
spring整合了jpa之后相应的dao的写法也会发生变化,来看下这两个方法


    @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);
   		
    }

相比hibernate只是把sessionfactory换成了EntityManager,相应的crud操作就使用entityManager调用相应的方法就OK了。为了调试方便顺道把日志的配置贴出来


#
# 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



Spring整合JPA