Spring+jpa实现简单的CRUD

我练习的步骤是先为项目添加jpa的支持 然后在添加spring 呵呵 刚开始学 怕出错。。。
我的实体Bean
package com.lee.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "person")
public class Person implements Serializable{


private static final long serialVersionUID = 7133047436636257323L;
private Integer id;
private String name;
private Date birthday;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(length = 25,nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Temporal(TemporalType.DATE)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}



}
在来就是dao接口
package com.lee.dao;

import java.util.List;

import com.lee.entity.Person;

public interface PersonDao {
/**
* 对Person的持久化
* @param person
*/
public void save(Person person);
/**
* spring的依赖注入的测试方法
*/
public void say();
/**
* 通过id获取一条person记录
* @param id
* @return
*/
public Person get(Integer id);
/**
* 更新一条记录
* @param person
*/
public void update(Person person);
/**
* 获取所有的person记录
* @return
*/
public List<Person> getAll();
/**
* 通过id删除一条记录
* @param id
*/
public void delete(Integer id);
}
还有就是dao的实现类。。。(这大家都了解的 我就不赘言了。。。。)
package com.lee.dao.impl;

import java.util.List;

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

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

import com.lee.dao.PersonDao;
import com.lee.entity.Person;
@Repository("dao")
@Transactional
public class PersonDaoImpl implements PersonDao{

@PersistenceContext
private EntityManager entityManagerFactory;


public void setEntityManagerFactory(EntityManager entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}

@Override
public void save(Person person) {
if(entityManagerFactory == null)
System.out.println("em is null");
entityManagerFactory.persist(person);
}
public void say(){
System.out.println("wo xiang shuo");
}

@Override
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public Person get(Integer id) {

return entityManagerFactory.find(Person.class, id);
}

@Override
public void update(Person person) {
entityManagerFactory.merge(person);

}

@SuppressWarnings("unchecked")
@Override
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public List<Person> getAll() {
// TODO Auto-generated method stub
return entityManagerFactory.createQuery("select p from Person p").getResultList();
}

@Override
public void delete(Integer id) {
entityManagerFactory.remove(get(id));

}
}
嗯 还有什么呢。。。。那就是jpa的配置文件
<?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="lee" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name = "hibernate.connection.driver_class" value = "com.mysql.jdbc.Driver"/>
<property name = "hibernate.connection.url" value = "jdbc:mysql://localhost:3306/test"/>
<property name = "hibernate.connection.username" value = "root"/>
<property name = "hibernate.connection.password" value = "123456"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>

</persistence>
再来就是spring的配置文件了 就是这个文件搞的我很是头疼。。。我用的方式是完全注解的模式。。。。

<?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:p="http://www.springframework.org/schema/p"
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 />
<context:component-scan base-package="com.lee" />
<tx:annotation-driven />

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="lee" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!--<bean id="personDao" class="com.lee.dao.impl.PersonDaoImpl">
</bean>
--><tx:annotation-driven transaction-manager="transactionManager" />
</beans>
开始测试了
package com.lee.test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

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

import com.lee.dao.PersonDao;
import com.lee.entity.Person;


public class JunitTest {

/**
* 这是测试JPA的
*/
@Test
public void test(){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("lee");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Person person = new Person();
person.setName("ssss");
person.setBirthday(new Date());
em.persist(person);
em.getTransaction().commit();
em.close();
factory.close();
}
@Test
public void testSpring(){
ApplicationContext ac =new ClassPathXmlApplicationContext("beans.xml");
PersonDao pd = (PersonDao) ac.getBean("dao");
System.out.println(pd.toString());
Person person = new Person();
person.setName("nnnnnnn");
person.setBirthday(new Date());
pd.save(person);
//pd.say();
}
@Test
public void get(){
ApplicationContext ac =new ClassPathXmlApplicationContext("beans.xml");
PersonDao pd = (PersonDao) ac.getBean("dao");
Person person = pd.get(2);
System.out.println(person.getName());
}
@Test
public void update(){
ApplicationContext ac =new ClassPathXmlApplicationContext("beans.xml");
PersonDao pd = (PersonDao) ac.getBean("dao");
Person person = pd.get(2);
person.setName("wo kao");
pd.update(person);
Person person2 = pd.get(2);
System.out.println(person2.getName());

}
@Test
public void findAll(){
ApplicationContext ac =new ClassPathXmlApplicationContext("beans.xml");
PersonDao pd = (PersonDao) ac.getBean("dao");
List<Person> persons = new ArrayList<Person>();
persons = pd.getAll();
for (Person person : persons) {
System.out.println(person.getName());
}
}
@Test
public void delete(){
ApplicationContext ac =new ClassPathXmlApplicationContext("beans.xml");
PersonDao pd = (PersonDao) ac.getBean("dao");
pd.delete(3);
System.out.println(pd.getAll().size());
}
}
因为是练习项目 我对他就没有优化 你如果嫌太长可以优化一下测试类。。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值