Spring3 and JPA Integration(II)

本文介绍了一个使用Spring和JPA实现的简单管理层接口及其实现。通过定义PersonManager接口并提供其实现类PersonManagerImpl,实现了人员数据的基本CRUD操作,并通过Spring配置管理事务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring3 and JPA Integration(II)

My manager Layer, the interface is com.sillycat.easyjpa.manager.PersonManager
package com.sillycat.easyjpa.manager;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.sillycat.easyjpa.model.Person;
@Transactional
public interface PersonManager
{
public List<Person> getAll();
public Person get(Integer id);
public void save(Person person);
public void updateName(Person person);
public void delete(Integer id);
}

The implementation of the interface is com.sillycat.easyjpa.manager.PersonManagerImpl
package com.sillycat.easyjpa.manager;

import java.util.List;

import com.sillycat.easyjpa.dao.PersonDAO;
import com.sillycat.easyjpa.model.Person;

public class PersonManagerImpl implements PersonManager
{
PersonDAO personDAO;
public List<Person> getAll()
{
return personDAO.getAll();
}
public Person get(Integer id)
{
return personDAO.get(id);
}
public void save(Person person)
{
if (person != null && person.getId() != null)
{
personDAO.update(person);
}
else
{
personDAO.insert(person);
}
}
public void updateName(Person person)
{
if (person != null && person.getId() != null)
{
personDAO.updateName(person.getName(), person.getId());
}
}
public void delete(Integer id)
{
personDAO.delete(id);
}
public void setPersonDAO(PersonDAO personDAO)
{
this.personDAO = personDAO;
}
}

Nothing special, just the common spring bean. My manager layer configuration file is manager-context.xml:
<bean id="personManager" class="com.sillycat.easyjpa.manager.PersonManagerImpl" >
<property name="personDAO" ref="personDAO" />
</bean>

The transaction configuration file is core-context.xml:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
lazy-init="false">
<property name="locations">
<list>
<value>classpath*:config.properties
</value>
</list>
</property>
</bean>
<!-- transaction manager,user&manage entityManagerFactory -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>

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

right now, we have transaction configuration, we can do full test.
com.sillycat.easyjpa.manager.PersonManagerTest:
package com.sillycat.easyjpa.manager;

import java.util.Date;
import java.util.List;
import com.sillycat.core.BaseTestCase;
import com.sillycat.easyjpa.model.Person;
public class PersonManagerTest extends BaseTestCase
{
private PersonManager manager;
private Person item;
public void setUp() throws Exception
{
super.setUp();
manager = (PersonManager) ctx.getBean("personManager");
item = this.getItem();
}
public void tearDown() throws Exception
{
super.tearDown();
if (item != null && item.getId() != null)
{
manager.delete(item.getId());
}
}
public void testSave()
{
manager.save(item);
assertNotNull(item);
assertNotNull(item.getId());
}
public void testGetAll()
{
manager.save(item);
assertNotNull(item);
assertNotNull(item.getId());
List<Person> list = manager.getAll();
assertNotNull(list);
assertTrue(list.size() > 0);
}
public void testGet()
{
manager.save(item);
assertNotNull(item);
assertNotNull(item.getId());
Person t = manager.get(item.getId());
assertNotNull(t);
assertEquals(t.getName(),item.getName());
assertEquals(t.getId(),item.getId());
}
public void testUpdateName(){
manager.save(item);
assertNotNull(item);
assertNotNull(item.getId());
item.setName("haha");
item.setSex(false);
manager.updateName(item);
Person t = manager.get(item.getId());
assertNotNull(t);
assertEquals(t.getName(),"haha");
assertEquals(t.getSex(),true);
}
private Person getItem()
{
Person t = new Person();
t.setAge((short) 1);
t.setBirthday(new Date());
t.setName("testName");
t.setSex(true);
return t;
}
}

And I build this jar to the JBOSS_HOME/server/default/lib, my build.xml is as follow:
<project name="${app.name}" default="all" xmlns:ivy="antlib:org.apache.ivy.ant">
<!-- some variables used -->
<property file="build.properties" />
<property name%
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值