Dao层系列-5-Hibernate JPA

本示例完整代码请见:
http://git.oschina.net/yaolifei/test/tree/master/test-hibernate-jpa

首先在classpath下新建META-INF目录,并新建文件persistence.xml

<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">
	<!--事务类型:local的还是global(JTA)的事务 -->
	<persistence-unit name="entityManagerFactory"
		transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<class>com.yaolifei.test.user.domain.User</class>
		<properties>
			<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
			<property name="hibernate.connection.url"
				value="jdbc:mysql://localhost/test" />
			<property name="hibernate.connection.username" value="root" />
			<property name="hibernate.connection.password" value="880716" />
			<property name="hibernate.show_sql" value="true" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
		</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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.2.xsd">
 
	<context:component-scan base-package="com.yaolifei.test"/>
 	
	<context:property-placeholder location="jdbc.properties" />
	
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
		<property name="driverClassName" value="${jdbc.driverClassName}"/> 
		<property name="url" value="${jdbc.url}" /> 
		<property name="username" value= "${jdbc.username}" /> 
		<property name="password" value="${jdbc.password}" /> 
		<property name="maxActive" value="${jdbc.maxActive}" /> 
		<property name="validationQuery" value="${jdbc.validationQuery}" /> 
	</bean> 
	
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="entityManagerFactory" />
	</bean>
	
	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
	
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>
	
</beans>
UserDaoImpl.java
package com.yaolifei.test.user.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.yaolifei.test.user.domain.User;

@Repository("userDao")
public class UserDaoImpl implements UserDao {
	
	@Autowired
	private EntityManagerFactory entityManagerFactory;

	@Override
	public User getById(Integer id) {
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		entityManager.getTransaction().begin();
		User user = (User) entityManager.find(User.class, id);
		entityManager.getTransaction().commit();
		return user;
	}

	@Override
	public List<User> getAll() {
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		entityManager.getTransaction().begin();
		Query query = entityManager.createQuery("from User");
		List<User> list = query.getResultList();
		entityManager.getTransaction().commit();
		return list;
	}

	@Override
	public void save(User user) {
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		entityManager.getTransaction().begin();
		entityManager.persist(user);
		entityManager.getTransaction().commit();
	}

	@Override
	public void update(User user) {
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		entityManager.getTransaction().begin();
		entityManager.merge(user);
		entityManager.getTransaction().commit();
	}

	@Override
	public void remove(User user) {
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		entityManager.getTransaction().begin();
		entityManager.remove(user);
		entityManager.getTransaction().commit();
	}

}

你可能感兴趣的:(Hibernate,jpa)