栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【SpringData&JPA从入门到精通】00-SpringData 简介

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【SpringData&JPA从入门到精通】00-SpringData 简介

笔记来源:尚硅谷SpringData教程(springdata经典,spring data快速上手)

文章目录
  • SpringData 简介
    • 1、Spring Data 概述
    • 2、Spring Data JPA 概述
    • 3、HelloWorld
      • 3.1、开发准备
        • pom 依赖
        • 数据库配置
        • Spring 配置
      • 3.2、编写代码
        • 实体类
        • Repository 类
      • 3.3、测试代码

SpringData 简介 1、Spring Data 概述

Spring Data:Spring 的一个子项目。用于简化数据库访问,支持 NoSQL 和 关系数据存储。其主要目标是使数据库的访问变得方便快捷

SpringData 项目所支持 NoSQL 存储:

  • MongoDB(文档数据库)
  • Neo4j(图形数据库)
  • Redis(键/值存储)
  • Hbase(列族数据库)

SpringData 项目所支持的 关系数据存储技术:

  • JDBC
  • JPA
2、Spring Data JPA 概述

Spring Data JPA:致力于减少数据访问层(DAO)的开发量.开发者唯一要做的,就只是 声明持久层的接口,其他都交给 Spring Data JPA 来帮你完成!

框架怎么可能代替开发者实现业务逻辑呢?

比如:当有一个 UserDao.findUserById 这样一个方法声明,大致应该能判断出这是根据给定条件的 ID 查询出满足条件的 User 对象

Spring Data JPA 做的便是规范方法的名字,根据符合规范的名字来确定方法需要实现什么样的逻辑

public interface PersonRepository extends Repository{
    Person getByLastName(String lastName);
}
3、HelloWorld

使用 Spring Data JPA 进行持久层开发需要的四个步骤:

  1. 配置 Spring 整合 JPA
  2. 在 Spring 配置文件中配置 Spring Data:让 Spring 为声明的接口创建代理对象。配置了 后,Spring 初始化容器时将会扫描 base-package 指定的包目录及其子目录,为继承 Repository 或其子接囗的接口创建代理对象,并将代理对象注册为 Spring Bean,业务层便可以通过 Spring 自动封装的特性来直接使用该对象
  3. 声明持久层的接口,该接口继承 Repository:Repository 是一个标记型接口,它不包含任何方法。如必要,Spring Data 可实现 Repository 其他子接口,其中定义了一些常用的增删改查,以及分页相关的方法
  4. 在接口中声明需要的方法:Spring Data 将根据给定的策略(具体筛略稍后讲解)来为其生成实现代码
3.1、开发准备 pom 依赖


    4.0.0

    com.vectorx.springdata
    SpringData
    1.0-SNAPSHOT

    
        8
        8
        4.0.0.RELEASE
    

    
        
        
            net.sourceforge.cglib
            com.springsource.net.sf.cglib
            2.2.0
        
        
            org.aopalliance
            com.springsource.org.aopalliance
            1.0.0
        
        
            org.aspectj
            com.springsource.org.aspectj.weaver
            1.6.4.RELEASE
        
        
            commons-logging
            commons-logging
            1.1.3
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-expression
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-orm
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
        
            antlr
            antlr
            2.7.7
        
        
            dom4j
            dom4j
            1.6.1
        
        
            org.hibernate.common
            hibernate-commons-annotations
            4.0.2.Final
        
        
            org.hibernate
            hibernate-core
            4.2.4.Final
        
        
            org.javassist
            javassist
            3.15.0-GA
        
        
            org.jboss.logging
            jboss-logging
            3.1.0.GA
        
        
            org.jboss.spec.javax.transaction
            jboss-transaction-api_1.1_spec
            1.0.1.Final
        
        
        
            org.hibernate.javax.persistence
            hibernate-jpa-2.0-api
            1.0.1.Final
        
        
            org.hibernate
            hibernate-entitymanager
            4.2.4.Final
        
        
        
            com.mchange
            c3p0
            0.9.2.1
        
        
            org.hibernate
            hibernate-c3p0
            4.2.4.Final
        
        
            com.mchange
            mchange-commons-java
            0.2.3.4
        
        
        
            mysql
            mysql-connector-java
            8.0.28
        
        
        
            org.springframework.data
            spring-data-commons
            1.6.2.RELEASE
        
        
            org.springframework.data
            spring-data-jpa
            1.4.2.RELEASE
        
        
        
            junit
            junit
            4.13.2
            test
        
    

数据库配置
jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///jpa
Spring 配置


    
    
    
        
        
        
        
    
    
    
        
        
            
        
        
        
            
                
                org.hibernate.cfg.ImprovedNamingStrategy
                
                org.hibernate.dialect.MySQL5InnoDBDialect
                true
                true
                update
            
        
    
    
    
        
        
    
    
    
    
    

3.2、编写代码 实体类
@Table(name = "JPA_PERSONS")
@Entity
public class Person
{
    private Integer id;
    private String lastName;
    private String email;
    private Date birthDay;

    @GeneratedValue
    @Id
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(Date birthDay) {
        this.birthDay = birthDay;
    }

    @Override
    public String toString() {
        return "Person{" + "id=" + id + ", lastName='" + lastName + ''' + ", email='" + email + ''' + ", birthDay=" + birthDay + '}';
    }
}
Repository 类
public interface PersonRepository extends Repository
{
    Person getByLastName(String lastName);
}
3.3、测试代码

1)测试数据源是否可用

public class SpringDataTest
{
    private ApplicationContext context;
    {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void testDataSource() throws SQLException {
        DataSource dataSource = context.getBean(DataSource.class);
        System.out.println(dataSource.getConnection());
    }
}

日志信息

com.mchange.v2.c3p0.impl.NewProxyConnection@3419e23b

数据表生成情况

2)插入一条测试数据备用

3)测试 Repository 能否正常执行

@Test
public void testPersonRepository(){
    PersonRepository repository = context.getBean(PersonRepository.class);
    Person person = repository.getByLastName("AA");
    System.out.println(person);
}

日志信息

Hibernate: 
    select
        person0_.id as id1_0_,
        person0_.birth_day as birth_da2_0_,
        person0_.email as email3_0_,
        person0_.last_name as last_nam4_0_ 
    from
        jpa_persons person0_ 
    where
        person0_.last_name=?
Person{id=1, lastName='AA', email='AA@qq.com', birthDay=2022-05-21 22:48:34.0}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/922144.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号