日期:2014-05-16  浏览次数:20435 次

JSF1.2+JPA2.0+Spring3.1整合开发

导入必须的Jar包

?

JSF1.2的依赖包

${jsf_home}\mojarra-1.2_15-b01-FCS\lib
jsf-api.jar
jsf-impl.jar
额外
jstl.jar

?

JPA用Hibenrate3.6.0实现的依赖包

?

${hibernate_home}\hibernate-distribution-3.6.0.Final\hibernate3.jar
${hibernate_home}\hibernate-distribution-3.6.0.Final\lib\required\*
${hibernate_home}\hibernate-distribution-3.6.0.Final\lib\jpa\*
${hibernate_home}\hibernate-distribution-3.6.0.Final\lib\optional\c3p0\* (加入C3P0)
${hibernate_home}\hibernate-distribution-3.6.0.Final\lib\optional\ehcache\* (加入缓存)

slf4j-nop-1.6.1.jar(可选)
backport-util-concurrent.jar (缓存依赖包)


Spring3.1.0的依赖包

?

${spring_home}\spring-framework-3.1.0.RELEASE\dist\
org.springframework.aop-3.1.0.RELEASE.jar
org.springframework.asm-3.1.0.RELEASE.jar
org.springframework.aspects-3.1.0.RELEASE.jar
org.springframework.beans-3.1.0.RELEASE.jar
org.springframework.context-3.1.0.RELEASE.jar
org.springframework.core-3.1.0.RELEASE.jar
org.springframework.expression-3.1.0.RELEASE.jar
org.springframework.jdbc-3.1.0.RELEASE.jar
org.springframework.orm-3.1.0.RELEASE.jar
org.springframework.test-3.1.0.RELEASE.jar
org.springframework.transaction-3.1.0.RELEASE.jar
org.springframework.web-3.1.0.RELEASE.jar

com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.junit-4.7.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

?

MySQL驱动
mysql-connector-java-5.1.7-bin.jar

?

因为开发很简单,就是CRUD...

所以能省略的就尽量省略了。。。

?

package org.monday.app.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "user_table")
public class User {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;

	@Column(name = "name")
	private String name;

	@Column(name = "phone")
	private String phone;

	@Temporal(TemporalType.DATE)
	@Column(name = "birthday")
	private Date birthday;

	//......

}

??

3. Dao实现类

?

package org.monday.app.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.monday.app.dao.UserDao;
import org.monday.app.domain.User;
import org.springframework.stereotype.Repository;

@Repository(value = "userDao")
public class UserDaoImpl implements UserDao {

	/**
	 * Spring3.1推荐使用原生的EntityManager 
	 * 而JpaDaoSupport和JpaTemplate被标记为过时
	 */
	@PersistenceContext(unitName = "jpa")
	private EntityManager entityManager;

	@Override
	public void save(User user) {
		entityManager.persist(user);
	}

	@Override
	public void update(User user) {
		entityManager.merge(user);
	}

	@Override
	public void delete(Integer id) {
		entityManager.remove(findById(id));
	}

	@Override
	public User findById(Integer id) {
		return entityManager.find(User.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAll() {
		Query query = entityManager.createQuery("from User");
		// 转换为HibernateQuery
		// if (query instanceof org.hibernate.ejb.QueryImpl) {
		// ((org.hibernate.ejb.QueryImpl) query).getHibernateQuery().setCacheable(true);
		// }
		return query.getResultList();
	}

}

?

?

?

3.Service实现类

?

就到对Dao的简单调用

?

?

现在,除了Web层组件,其他的组件都准比完毕。接下来,整合JPA+Spring

?

在“源文件”的目录下建立一个META-INF文件夹,里面建立一个persistence.xml文件。这个就不解释了。

?

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xm