- 浏览: 2542133 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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(I)
JPA POJO just stay the same com.sillycat.easyjpa.model.
We have the same interface for DAO layer com.sillycat.easyjpa.dao.PersonDAO:
package com.sillycat.easyjpa.dao;
import java.util.List;
import com.sillycat.easyjpa.model.Person;
public interface PersonDAO
{
public void insert(Person person);
public void updateName(String name, Integer id);
public void update(Person person);
public void delete(Integer id);
public Person get(Integer id);
public List<Person> getAll();
}
The implementation of the interface changes, we use jpaTempate to deal with entitymanager
com.sillycat.easyjpa.dao.PersonDAOImpl.java:
package com.sillycat.easyjpa.dao;
import java.util.List;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import com.sillycat.easyjpa.model.Person;
public class PersonDAOImpl extends JpaDaoSupport implements PersonDAO
{
public void delete(Integer id)
{
Person person = getJpaTemplate().find(Person.class, id);
if (person != null)
{
getJpaTemplate().remove(person);
}
}
public Person get(Integer id)
{
return getJpaTemplate().find(Person.class, id);
}
@SuppressWarnings("unchecked")
public List<Person> getAll()
{
List<Person> list = getJpaTemplate().find("select o from Person o order by o.id asc");
return list;
}
public void insert(Person person)
{
getJpaTemplate().persist(person);
}
public void update(Person person)
{
getJpaTemplate().merge(person);
}
public void updateName(String name, Integer id)
{
Person person = getJpaTemplate().find(Person.class, id);
if (person != null)
{
person.setName(name);
}
}
}
We have not start transaction in this layer, so we can not insert/update data in my unit test
com.sillycat.easyjpa.dao.PersonDAOTest.java:
package com.sillycat.easyjpa.dao;
import java.util.List;
import com.sillycat.core.BaseTestCase;
import com.sillycat.easyjpa.model.Person;
public class PersonDAOTest extends BaseTestCase
{
private PersonDAO dao;
public void setUp() throws Exception
{
super.setUp();
dao = (PersonDAO) ctx.getBean("personDAO");
}
public void tearDown() throws Exception
{
super.tearDown();
}
public void testGetAll()
{
List<Person> list = dao.getAll();
assertNotNull(list);
assertTrue(list.size() > 0);
}
public void testGet()
{
Person t = dao.get(Integer.valueOf(1));
assertNotNull(t);
}
}
And this is the datasource configuration in resource-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${dspool.maxactive}" />
<property name="maxIdle" value="${dspool.maxidle}" />
<property name="minIdle" value="1" />
<property name="maxWait" value="30000" />
<property name="defaultAutoCommit" value="true" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="300" />
<property name="logAbandoned" value="true" />
</bean>
<!-- entity manager factory,provided by spring-->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation"
value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
</beans>
my persistence.xml file is placed conf/META-INF/persistence.xml:
<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="demoTest" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>
</persistence>
spring configuration file is dao-context.xml:
<bean id="personDAO" class="com.sillycat.easyjpa.dao.PersonDAOImpl" >
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
my sample pr PuO[ POST /admin/blogs HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/
JPA POJO just stay the same com.sillycat.easyjpa.model.
We have the same interface for DAO layer com.sillycat.easyjpa.dao.PersonDAO:
package com.sillycat.easyjpa.dao;
import java.util.List;
import com.sillycat.easyjpa.model.Person;
public interface PersonDAO
{
public void insert(Person person);
public void updateName(String name, Integer id);
public void update(Person person);
public void delete(Integer id);
public Person get(Integer id);
public List<Person> getAll();
}
The implementation of the interface changes, we use jpaTempate to deal with entitymanager
com.sillycat.easyjpa.dao.PersonDAOImpl.java:
package com.sillycat.easyjpa.dao;
import java.util.List;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import com.sillycat.easyjpa.model.Person;
public class PersonDAOImpl extends JpaDaoSupport implements PersonDAO
{
public void delete(Integer id)
{
Person person = getJpaTemplate().find(Person.class, id);
if (person != null)
{
getJpaTemplate().remove(person);
}
}
public Person get(Integer id)
{
return getJpaTemplate().find(Person.class, id);
}
@SuppressWarnings("unchecked")
public List<Person> getAll()
{
List<Person> list = getJpaTemplate().find("select o from Person o order by o.id asc");
return list;
}
public void insert(Person person)
{
getJpaTemplate().persist(person);
}
public void update(Person person)
{
getJpaTemplate().merge(person);
}
public void updateName(String name, Integer id)
{
Person person = getJpaTemplate().find(Person.class, id);
if (person != null)
{
person.setName(name);
}
}
}
We have not start transaction in this layer, so we can not insert/update data in my unit test
com.sillycat.easyjpa.dao.PersonDAOTest.java:
package com.sillycat.easyjpa.dao;
import java.util.List;
import com.sillycat.core.BaseTestCase;
import com.sillycat.easyjpa.model.Person;
public class PersonDAOTest extends BaseTestCase
{
private PersonDAO dao;
public void setUp() throws Exception
{
super.setUp();
dao = (PersonDAO) ctx.getBean("personDAO");
}
public void tearDown() throws Exception
{
super.tearDown();
}
public void testGetAll()
{
List<Person> list = dao.getAll();
assertNotNull(list);
assertTrue(list.size() > 0);
}
public void testGet()
{
Person t = dao.get(Integer.valueOf(1));
assertNotNull(t);
}
}
And this is the datasource configuration in resource-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${dspool.maxactive}" />
<property name="maxIdle" value="${dspool.maxidle}" />
<property name="minIdle" value="1" />
<property name="maxWait" value="30000" />
<property name="defaultAutoCommit" value="true" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="300" />
<property name="logAbandoned" value="true" />
</bean>
<!-- entity manager factory,provided by spring-->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation"
value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
</beans>
my persistence.xml file is placed conf/META-INF/persistence.xml:
<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="demoTest" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>
</persistence>
spring configuration file is dao-context.xml:
<bean id="personDAO" class="com.sillycat.easyjpa.dao.PersonDAOImpl" >
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
my sample pr PuO[ POST /admin/blogs HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/
发表评论
-
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 ...