简单的spring整合JPA

[url]http://zmx.iteye.com/blog/556452
[/url]
最近转载学习的一个实验,项目在附件,所需要的jar包也是在下面,在导入项目后自己把包加进去吧。

实体类:
package com.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

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

}


DAO接口类:
package com.dao;

import java.util.List;

import com.entity.Student;

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



DAO接口实现类:
package com.impl;

import java.util.List;

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

import com.dao.StudentDao;
import com.entity.Student;

public class StudentDaoImpl 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() {
		List resultList = em.createQuery("select s from Student s").getResultList();
		return resultList;
	}

}



服务接口类:
package com.service;

import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.entity.Student;

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

}


服务实现类:

package com.serviceImpl;

import java.util.List;

import com.dao.StudentDao;
import com.entity.Student;
import com.service.StudentService;

public class StudentServiceImpl 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;
	}

}



服务测试类:
package com.test;

import java.util.List;

import junit.framework.TestCase;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.entity.Student;
import com.service.StudentService;

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.getStu_name());
		}
	}

}



JPA配置文件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="panshao" 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="root" />
			<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/lee?useUnicode=true&amp;characterEncoding=utf-8" />
		</properties>
	</persistence-unit>	
</persistence>



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="panshao"></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.impl.StudentDaoImpl">
	</bean>

	<bean id="studentSerivce"
		class="com.serviceImpl.StudentServiceImpl">
		<property name="studao" ref="studentDAO"></property>
	</bean>

</beans>




猜你喜欢

转载自panshaobinsb.iteye.com/blog/1683979