`
sillycat
  • 浏览: 2542118 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring3 and JPA Integration(I)

阅读更多
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 prPuO[POST /admin/blogs HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/
分享到:
评论

相关推荐

    Spring Data JPA中文文档[1.4.3]_springdatajpa_erlang_waitxpf_

    6. **Integration with Spring MVC**:Spring Data JPA 可以无缝集成到 Spring MVC 应用中,方便在 Web 层进行数据操作。通过 ModelMapper 和 Controller 方法,可以直接将 Repository 中的数据转换为视图模型并返回...

    spring学习:spring data jpa

    6. **Integration with Spring Transaction Management**:Spring Data JPA与Spring的事务管理无缝集成,可以方便地进行事务控制。 在实际使用中,我们需要配置Spring Data JPA,这通常涉及到以下步骤: 1. 添加...

    Introduction to Spring2. and JPA

    《Introduction to Spring 2.0 and JPA》这篇文章源自IBM Developerworks,主要探讨了Spring框架2.0版本以及Java Persistence API(JPA)的相关技术。本文将深入解析这两个关键的Java开发工具,帮助读者理解它们的...

    Spring JPA 配置类包集合 方便大家使用

    9. **Integration with Spring Boot**:Spring JPA与Spring Boot的集成使得配置变得更加简单,通常只需要在application.properties或application.yml中添加几行配置,就可以快速启动数据库连接。 10. **性能优化**...

    Spring+Struts2+JPA

    3. **配置JPA**:添加JPA和持久化提供者(如Hibernate)的依赖,创建persistence.xml配置文件,定义实体类和数据源。 4. **整合步骤**:配置Struts2的Spring插件,使Struts2能够从Spring容器中获取Action实例。在...

    Spring-data-jpa示例源码

    3. **Specifications**: Spring Data JPA 提供了 `Specification` 接口,用于构建复杂的动态查询。这使得在运行时可以根据需要构建灵活的查询条件,无需硬编码到 repository 方法中。 4. **JPA Entity Mapping**: ...

    SpringDataJpa.rar

    9. **Integration with Spring Boot**:Spring Boot提供了开箱即用的Spring Data JPA配置,只需在pom.xml或build.gradle中添加依赖,然后在application.properties或application.yml中配置数据源,即可快速启动JPA...

    spring-data-jpa hibernate

    - **Integration with Spring**:与Spring的其他模块如Spring MVC无缝集成,提供事务管理等服务。 2. **Hibernate**:Hibernate是JPA的一个实现,它是一个对象关系映射(ORM)框架。Hibernate允许开发者用面向对象...

    SprinMVC+Spring+Spring-Data-JPA

    Spring MVC、Spring 和 Spring Data JPA 是 Java 企业级开发中的三大核心框架,它们共同构建了一个强大、灵活且高效的后端应用架构。本教程将深入探讨这三个组件的整合及其核心功能,帮助你理解和掌握现代Java Web...

    spring-data-commons-1.7.2.RELEASEspring-data-jpa-1.5.2.RELEASE-java datajpa

    5. **Integration with ORMs**:Spring Data JPA 可以与各种 JPA 实现(如 Hibernate、EclipseLink 等)配合使用,为不同的 ORM 解决方案提供统一的编程模型。 6. **Custom Query Methods**:开发者可以定义自己的...

    spring-data-jpa-1.1.0.RC1.zip

    《Spring Data JPA 1.1.0.RC1:深度解析与实战指南》 Spring Data JPA 是 Spring 框架的一个重要模块,它旨在简化数据访问层的开发工作,尤其是在使用 Java Persistence API (JPA) 的情况下。1.1.0.RC1 版本是该...

    spring整合jpa

    - **Integration测试**:使用Spring Boot的`@DataJpaTest`或`@SpringBootTest`注解进行集成测试,确保JPA与Spring的整合功能正常工作。 - **Mock测试**:对于复杂的业务逻辑,可以利用`@MockBean`模拟数据访问层,...

    spring-integration

    ### Spring Integration概述与新特性详解 #### 一、Spring Integration框架简介 **Spring Integration**是Spring家族中的一个重要成员,它提供了构建企业集成应用所需的多种工具和技术。该框架基于消息传递模型,...

    JPA相关jar包

    4. **Integration with Hibernate**:虽然JPA是一个标准,但Spring Data JPA默认集成了Hibernate作为JPA供应商,可以无缝使用Hibernate的功能,如二级缓存、懒加载等。 **标签解析** "jpa":指Java Persistence ...

    Spring Recipes: A Problem-Solution Approach, Second Edition

    * Simplifying data access with Spring (JDBC, Hibernate, and JPA) and managing transactions both programmatically and declaratively. * Spring’s support for remoting technologies (RMI, Hessian, ...

    Spring Data:Spring Data Modern Data Access for Enterprise Java

    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

    struts2+spring+jpa+extjs4 Demo

    Spring框架包含多个模块,如Core Container、Data Access/Integration、Web等,可以用于事务管理、数据访问、安全性等多个方面。在本Demo中,Spring可能被用来管理Bean、处理事务,并与Struts2集成,实现业务逻辑和...

    Spring.Essentials.178398

    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 ...

Global site tag (gtag.js) - Google Analytics