`

Spring2.5整合JPA

阅读更多

在网上找了很多Spring整合JPA的文章,试着去写了很多但没有成功,主要原因可能是jar不正确导致的。花了一些时间自已写了一个小例子,Spring2.5整合JPA(Hibernate实现)。

所需要的Spring2.5的jar包如下:

 Spring2.5整合JAP所需要的Spring2.5的jar包

所需要的JPA的jar包如下:

 Spring2.5整合JAP所需要的JPA的jar包

Spring2.5整合JPA所需要的jar如下:

 Spring2.5整合JAP所需要的jar包

 文件太大javaeye上传不了,上面的jar下载地址:(http://download.csdn.net/source/1933969

 1,配置我们的Spring配置文件beans.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">

	<context:annotation-config  />

	<bean id="entityManager"
		class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="mengya"></property>
	</bean>

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

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

	<bean id="studentDAO"
		class="com.mengya.dao.imple.StudentDAOImple">
	</bean>

	<bean id="studentSerivce"
		class="com.mengya.service.imple.StudentServiceImple">
		<property name="studao" ref="studentDAO"></property>
	</bean>

</beans>

 如查以上xml在你的MyEclipse中出显了一个错误提示,请你自手在你的MyEclipse的XML配置中配置http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

 

2, 配置JPA的persistence.xml(在src/META-INF/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="mengya" transaction-type="RESOURCE_LOCAL">
		<properties>
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
			<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver" />
			<property name="hibernate.connection.username" value="root" />
			<property name="hibernate.connection.password" value="###" />
			<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mp?useUnicode=true&amp;characterEncoding=gbk" />
		</properties>
	</persistence-unit>	
</persistence>

 

3,构建我们的实体Bean如下:

@Entity
public class Student {

	private Integer stu_id;

	private String stu_name;

	private String stu_sex;

	private Integer stu_age;

	private String stu_info;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getStu_id() {
		return stu_id;
	}

	public void setStu_id(Integer stu_id) {
		this.stu_id = stu_id;
	}

	@Column(nullable = false)
	public String getStu_name() {
		return stu_name;
	}

	public void setStu_name(String stu_name) {
		this.stu_name = stu_name;
	}

	public Integer getStu_age() {
		return stu_age;
	}

	public void setStu_age(Integer stu_age) {
		this.stu_age = stu_age;
	}

	public String getStu_info() {
		return stu_info;
	}

	public void setStu_info(String stu_info) {
		this.stu_info = stu_info;
	}

	public String getStu_sex() {
		return stu_sex;
	}

	public void setStu_sex(String stu_sex) {
		this.stu_sex = stu_sex;
	}

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((stu_id == null) ? 0 : stu_id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Student other = (Student) obj;
		if (stu_id == null) {
			if (other.stu_id != null)
				return false;
		} else if (!stu_id.equals(other.stu_id))
			return false;
		return true;
	}

}

 4,构建我们的DAO接口及实现:

public interface StudentDao {
	public void save(Student stu);

	public void delete(Integer stu_id);

	public void update(Student stu);

	public Student getStudentByPK(Integer stu_id);

	public List<Student> queryAll();
}

 

public class StudentDAOImple implements StudentDao {
	@PersistenceContext
	EntityManager em;

	public void save(Student stu) {
		em.persist(stu);
	}

	public void delete(Integer stu_id) {
		em.remove(em.getReference(Student.class, stu_id));
	}

	public void update(Student stu) {
		em.merge(stu);
	}

	public Student getStudentByPK(Integer stu_id) {
		return em.find(Student.class, stu_id);
	}

	public List<Student> queryAll() {
		return em.createQuery("select s from Student s").getResultList();
	}

}

 5,service的接口及实现:

@Transactional
public interface StudentService {

	public void save(Student stu);

	public void delete(Integer stu_id);

	public void update(Student stu);

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Student getStudentByPK(Integer stu_id);

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<Student> queryAll();

}

 

public class StudentServiceImple implements StudentService {
	
	private StudentDao studao;
	
	public void delete(Integer stu_id) {
		studao.delete(stu_id);
	}

	public Student getStudentByPK(Integer stu_id) {
		return studao.getStudentByPK(stu_id);
	}

	public List<Student> queryAll() {
		return studao.queryAll();
	}

	public void save(Student stu) {
		studao.save(stu);
	}

	public void update(Student stu) {
		studao.update(stu);
	}

	public void setStudao(StudentDao studao) {
		this.studao = studao;
	}

}

 事务只需@Transactional及可,Spring2.5自动帮我们提供事务,事务配置在我们service中。

6,测试我们的service:

public class StudentServiceTest extends TestCase {

	public void testSave() {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"beans.xml");
		StudentService stuMght = (StudentService) context
				.getBean("studentSerivce");
		Student stu = new Student();
		stu.setStu_name("xiaobo");
		stu.setStu_age(22);
		stu.setStu_sex("男");
		stu.setStu_info("C++");
		stuMght.save(stu);
		System.out.println(stu);
	}

	public void testDelete() {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"beans.xml");
		StudentService stuMght = (StudentService) context
				.getBean("studentSerivce");
		stuMght.delete(3);
	}

	public void testUpdate() {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"beans.xml");
		StudentService stuMght = (StudentService) context
				.getBean("studentSerivce");
		Student stu = stuMght.getStudentByPK(4);
		stu.setStu_age(23);
		stuMght.update(stu);
	}

	public void testGetStudentByPK() {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"beans.xml");
		StudentService stuMght = (StudentService) context
				.getBean("studentSerivce");
		Student stu = stuMght.getStudentByPK(5);
		System.out.println(stu);
	}

	public void testQueryAll() {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"beans.xml");
		StudentService stuMght = (StudentService) context
				.getBean("studentSerivce");
		List<Student> stuList = stuMght.queryAll();
		for (Student stu : stuList) {
			System.out.println(stu);
		}
	}

}

 

 

  • SpringJPA.rar (15 KB)
  • 描述: Spring2.5整合JPA实例
  • 下载次数: 114
3
1
分享到:

相关推荐

    Spring2.5整合JPA(Hibernate实现)所需的JAR包

    Spring2.5整合JPA(Hibernate实现)所需的JAR包

    Struts2,spring2.5与jpa的整合示例

    Struts2,spring2.5与jpa的整合,包括精确到领域对象的权限管理

    struts2.0+spring2.5+JPA整合框架

    struts2.0+spring2.5+JPA整合框架,下载完成后导入eclipse,由于lib下jar较多,请自行导入相关jar包并修改数据库连接文件jdbc.properties。

    Struts1.3+spring2.5+JPA(hibernate) demo

    Struts1.3+spring2.5+JPA(hibernate) 实现的一个小示例,比较适合初学者和整合配置查询。

    jsf+spring2.5+jpa(hibernate)的jar包

    这是jsf+spring2.5+jpa(hibernate)的jar包,很多人为了jsj环境而配置半天,在此提供jar包共享。注:除了ajax4jsf和tomahawk-1.1.3.jar,因为csdn只让我上传20mb,大家自己可以下一下自己试试。

    struts2+spring2.5+jpa 注解开发框架

    spring注解(去掉了在XML文件中bean的配置) jpa注解(去掉了*.hbm.xml)文件的配置 该项目完全可以运行,包含了所有的JAR包,数据库默认使用ORACLE,MYSQL(需要改下jdbc.property配置文件,修改很方便) 该包绝对适用,...

    struts2.1.8+JPA3.0(hibernate实现)+spring2.5整合jar

    整合jar文件,直接下载后放到工程lib下面。这是我整理给我的中型BBS项目用的。

    spring2.5学习PPT 传智博客

    spring2.5学习PPT 传智博客 01_全面阐释Spring及其各项功能 02_搭建与测试Spring的开发环境 03_编码剖析Spring管理Bean的原理 04_Spring的三种实例化Bean的方式 05_配置Spring管理的bean的作用域 06_Spring...

    Struts2+Spring2.5+Hibernate3.3整合开发之Sring与Hibernate整合

    一、整合开发时Hibernate、Spring需要的JAR文件。 hibernate核心安装包下的(下载路径:http://www.hibernate.org/,点击“Hibernate Core”右边的“Downloads”): hibernate3.jar lib\bycode\cglib\hibernate-...

    struts1+spring+JPA+jstl的CRUD操作,包含分页

    使用struts1.2.7和spring2.5以及JPA(hibernate3.2)整合,加上jstl标签库的使用,做成的一个带有分页功能的模块,包含最基本的CRUD操作,绝对物有所值!!

    Struts2.1+Spring3.0+JPA1.0(Hibernate3.3实现)例子

    Struts2.1+Spring3.0+JPA1.0(Hibernate3.3实现)例子 文章分类:Java编程 本文代码是Spring2.5各种示例的综合,在此把所有框架升级到最新版本整合一下。

    spring mvc + hibernate4整合文档

    Spring2.5+Struts2+Hibernate3的整合跟Spring2.5+Struts2+Jpa的整合过程。

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

    中文名: 经典Java EE企业应用实战--基于WebLogic/JBoss的JSF+EJB 3+JPA整合开发 原名: 经典Java EE企业应用实战--基于WebLogic/JBoss的JSF+EJB 3+JPA整合开发 作者: 李刚 资源格式: PDF 版本: 第一版 出版社: 电子...

    Spring-Reference_zh_CN(Spring中文参考手册)

    12.6.1. 在Spring环境中建立JPA 12.6.1.1. LocalEntityManagerFactoryBean 12.6.1.2. LocalContainerEntityManagerFactoryBean 12.6.1.3. 处理多个持久化单元 12.6.2. JpaTemplate 和 JpaDaoSupport 12.6.3. 基于...

    Spring中文帮助文档

    2.7. 移植到Spring 2.5 2.7.1. 改变 2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. IoC(控制反转)容器 3.1. 简介 3.2. 基本原理 - 容器和bean 3.2.1. 容器 3.2.2. 实例化容器 3.2.3. 多种bean ...

    Spring API

    2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件...

    springboot知识点整理

    6.4 整合SpringData JPA 125 6.4.1 SpringData简介 125 6.4.2 整合 126 7 Spring Boot启动配置原理 128 7.1 启动流程(Springboot 1.50版本) 128 7.1.1 创建SpringApplication对象 129 7.1.2 运行run方法 130 7.1.3...

    Spring 2.0 开发参考手册

    12.6.1. 在Spring环境中建立JPA 12.6.2. JpaTemplate 和 JpaDaoSupport 12.6.3. 基于原生的JPA实现DAO 12.6.4. 异常转化 12.6.5. 事务管理 12.6.6. JpaDialect III. Web 13. Web框架 13.1. 介绍 13.1.1. 与...

Global site tag (gtag.js) - Google Analytics