- 浏览: 2542146 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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%
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%
发表评论
-
Stop Update Here
2020-04-28 09:00 310I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 468NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 361Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 364Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 328Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 422Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 364Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 444VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 376Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 466NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 413Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 330Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 242GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 443GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 320GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 285Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 302Serverless with NodeJS and Tenc ...
相关推荐
6. **Integration with Spring MVC**:Spring Data JPA 可以无缝集成到 Spring MVC 应用中,方便在 Web 层进行数据操作。通过 ModelMapper 和 Controller 方法,可以直接将 Repository 中的数据转换为视图模型并返回...
6. **Integration with Spring Transaction Management**:Spring Data JPA与Spring的事务管理无缝集成,可以方便地进行事务控制。 在实际使用中,我们需要配置Spring Data JPA,这通常涉及到以下步骤: 1. 添加...
《Introduction to Spring 2.0 and JPA》这篇文章源自IBM Developerworks,主要探讨了Spring框架2.0版本以及Java Persistence API(JPA)的相关技术。本文将深入解析这两个关键的Java开发工具,帮助读者理解它们的...
9. **Integration with Spring Boot**:Spring JPA与Spring Boot的集成使得配置变得更加简单,通常只需要在application.properties或application.yml中添加几行配置,就可以快速启动数据库连接。 10. **性能优化**...
3. **配置JPA**:添加JPA和持久化提供者(如Hibernate)的依赖,创建persistence.xml配置文件,定义实体类和数据源。 4. **整合步骤**:配置Struts2的Spring插件,使Struts2能够从Spring容器中获取Action实例。在...
3. **Specifications**: Spring Data JPA 提供了 `Specification` 接口,用于构建复杂的动态查询。这使得在运行时可以根据需要构建灵活的查询条件,无需硬编码到 repository 方法中。 4. **JPA Entity Mapping**: ...
9. **Integration with Spring Boot**:Spring Boot提供了开箱即用的Spring Data JPA配置,只需在pom.xml或build.gradle中添加依赖,然后在application.properties或application.yml中配置数据源,即可快速启动JPA...
- **Integration with Spring**:与Spring的其他模块如Spring MVC无缝集成,提供事务管理等服务。 2. **Hibernate**:Hibernate是JPA的一个实现,它是一个对象关系映射(ORM)框架。Hibernate允许开发者用面向对象...
Spring MVC、Spring 和 Spring Data JPA 是 Java 企业级开发中的三大核心框架,它们共同构建了一个强大、灵活且高效的后端应用架构。本教程将深入探讨这三个组件的整合及其核心功能,帮助你理解和掌握现代Java Web...
5. **Integration with ORMs**:Spring Data JPA 可以与各种 JPA 实现(如 Hibernate、EclipseLink 等)配合使用,为不同的 ORM 解决方案提供统一的编程模型。 6. **Custom Query Methods**:开发者可以定义自己的...
《Spring Data JPA 1.1.0.RC1:深度解析与实战指南》 Spring Data JPA 是 Spring 框架的一个重要模块,它旨在简化数据访问层的开发工作,尤其是在使用 Java Persistence API (JPA) 的情况下。1.1.0.RC1 版本是该...
- **Integration测试**:使用Spring Boot的`@DataJpaTest`或`@SpringBootTest`注解进行集成测试,确保JPA与Spring的整合功能正常工作。 - **Mock测试**:对于复杂的业务逻辑,可以利用`@MockBean`模拟数据访问层,...
### Spring Integration概述与新特性详解 #### 一、Spring Integration框架简介 **Spring Integration**是Spring家族中的一个重要成员,它提供了构建企业集成应用所需的多种工具和技术。该框架基于消息传递模型,...
4. **Integration with Hibernate**:虽然JPA是一个标准,但Spring Data JPA默认集成了Hibernate作为JPA供应商,可以无缝使用Hibernate的功能,如二级缓存、懒加载等。 **标签解析** "jpa":指Java Persistence ...
* Simplifying data access with Spring (JDBC, Hibernate, and JPA) and managing transactions both programmatically and declaratively. * Spring’s support for remoting technologies (RMI, Hessian, ...
Part I. Background 1. The Spring Data Project ...13. Creating Big Data Pipelines with Spring Batch and Spring Integration Part VI. Data Grids 14. GemFire: A Distributed Data Grid Bibliography Index
Spring框架包含多个模块,如Core Container、Data Access/Integration、Web等,可以用于事务管理、数据访问、安全性等多个方面。在本Demo中,Spring可能被用来管理Bean、处理事务,并与Struts2集成,实现业务逻辑和...
Explore Spring's comprehensive transaction support for declarative Transaction Management and its integration with Spring's data access abstractions Investigate Spring Data access mechanisms with ...