SpringData JPA oliverxu

SpringData JPA

  • Introduction
    1. JPA:

Spring Data JPA, part of the larger Spring Data family, makes it easy to easily implement JPA based repositories. This module deals with enhanced support for JPA based data access layers. It makes it easier to build Spring-powered applications that use data access technologies.

 

Implementing a data access layer of an application has been cumbersome for quite a while. Too much boilerplate code has to be written to execute simple queries as well as perform pagination, and auditing. Spring Data JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that’s actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.

 

Features:

  • Sophisticated support to build repositories based on Spring and JPA
  • Support for Querydsl predicates and thus type-safe JPA queries
  • Transparent auditing of domain class
  • Pagination support, dynamic query execution, ability to integrate custom data access code
  • Validation of @Query annotated queries at bootstrap time
  • Support for XML based entity mapping
  • JavaConfig based repository configuration by introducing @EnableJpaRepositories.
    1. Hebernet

 

Due to the different inception dates of individual Spring Data modules, most of them carry different major and minor version numbers. The easiest way to find compatible ones is to rely on the Spring Data Release Train BOM that we ship with the compatible versions defined. In a Maven project, you would declare this dependency in the <dependencyManagement /> section of your POM, as follows:

Example 1. Using the Spring Data release train BOM

<dependencyManagement>

  <dependencies>

    <dependency>

      <groupId>org.springframework.data</groupId>

      <artifactId>spring-data-releasetrain</artifactId>

      <version>Lovelace-SR4</version>

      <scope>import</scope>

      <type>pom</type>

    </dependency>

  </dependencies>

</dependencyManagement>

A working example of using the BOMs can be found in our Spring Data examples repository. With that in place, you can declare the Spring Data modules you would like to use without a version in the <dependencies /> block, as follows:

Example 2. Declaring a dependency to a Spring Data module

<dependencies>
  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
  </dependency>
<dependencies>
    1. Spring Framework

The current version of Spring Data modules require Spring Framework in version 5.1.4.RELEASE or better.

    1. Dependency Management with Spring Boot

Spring Boot selects a recent version of Spring Data modules for you. If you still want to upgrade to a newer version, configure the property spring-data-releasetrain.version to the train name and iteration you would like to use.

 

    1. Configuration when creating a project

Example 3

<!—for mysql -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<!—jpa dependence include spring-data-jpa、spring-orm and Hibernate for JPA -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>    

 

    1. Database Configuration(yml )
    2. Mysql:

The simplest:

server:

 #port

 port: 8080

spring:

  datasource:

    driver-class-name: com.mysql.jdbc.Driver

    url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&characterEncoding=utf8

    username: root

    password: root

  jpa:

    hibernate:

      ddl-auto: update

    show-sql: true

Complete configuration:

server:

  #port

  port: 8088

spring:

  application:

    #servr name

    name: cms-dept

spring:

#data source and jpa

  datasource:

    #database url  and encoding

    url: jdbc:mysql://localhost:3306/crm?characterEncoding=utf8

    #username

    username: ***

    #password

    password: ***

spring:

    #Connection pool

    dbcp2:

      # Initialize connection pool size

      initial-size: 10

      #Minimum number of connection pools

      min-idle: 10     

      # maximum number of connection pools

      max-idle: 30

      # Configure the waiting time for a timeout connection

      max-wait-millis: 30000

      # Configure how often to perform a check to detect database connections that need to be closed

      time-between-eviction-runs-millis: 200000

      # Configure the minimum lifetime of the connection in the connection pool

      remove-abandoned-on-maintenance: 200000

  spring:

  jpa:

    # Configure the database type

    database: MYSQL

    # Configure whether to print SQL

    show-sql: true

    #Hibernate

    hibernate:

      # Configuration Cascade Level

      ddl-auto: update

      naming:

        # Naming strategy

        strategy: org.hibernate.cfg.ImprovedNamingStrategy

    properties:

      hibernate:

        dialect: org.hibernate.dialect.MySQL5Dialect

 

  • ORACLE

 

  • Working with Spring Data Repositories
    1. CrudRepository

The central interface in the Spring Data repository abstraction is Repository. This interface acts primarily as a marker interface to capture the types to work with and to help you to discover interfaces that extend this one.

Example 3. CrudRepository interface

public interface CrudRepository<T, ID extends Serializable>

  extends Repository<T, ID> {

 

  <S extends T> S save(S entity);     

 

  Optional<T> findById(ID primaryKey);

 

  Iterable<T> findAll();              

 

  long count();                       

 

  void delete(T entity);              

 

  boolean existsById(ID primaryKey);  

 

  // … more functionality omitted.

}

 

Saves the given entity.

 

Returns the entity identified by the given ID.

 

Returns all entities.

 

Returns the number of entities.

 

Deletes the given entity.

 

Indicates whether an entity with the given ID exists.

    1. PagingAndSortingRepository

On top of the CrudRepository, there is a PagingAndSortingRepository abstraction that adds additional methods to ease paginated access to entities:

Example 4. PagingAndSortingRepository interface

public interface PagingAndSortingRepository<T, ID extends Serializable>

  extends CrudRepository<T, ID> {

 

  Iterable<T> findAll(Sort sort);

 

  Page<T> findAll(Pageable pageable);

}

To access the second page of User by a page size of 20, you could do something like the following:

PagingAndSortingRepository<User, Long> repository = // … get access to a bean
Page<User> users = repository.findAll(new PageRequest(1, 20));

 

 

    1. Query methods

Standard CRUD functionality repositories usually have queries on the underlying datastore. With Spring Data, declaring those queries becomes a four-step process:

  1. define a entity.

@Table(name = "usertable")
@Entity
public class usertable {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)

 

  1. Declare an interface extending Repository or one of its subinterfaces and type it to the domain class and ID type that it should handle, as shown in the following example:

interface PersonRepository extends Repository<Person, Long> { }

  1. Declare query methods on the interface.

interface PersonRepository extends Repository<Person, Long> {

  List<Person> findByLastname(String lastname);

}

  1. Call the corresponding method
@RunWith(SpringRunner.class)

@SpringBootTest

public class testuser {

    @Autowired

    usertableIMP usertableimp;

    @Test

    public void TestUsertable() {
usertableimp.save(user1);
}

The sections that follow explain each step in detail:

Defining Repository Interfaces

Defining Query Methods

Creating Repository Instances

Custom Implementations for Spring Data Repositories

names

Example 16. Query creation from method names

interface PersonRepository extends Repository<User, Long> {
 
  List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);
 
  // Enables the distinct flag for the query
  List<Person> findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname);
  List<Person> findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname);
 
  // Enabling ignoring case for an individual property
  List<Person> findByLastnameIgnoreCase(String lastname);
  // Enabling ignoring case for all suitable properties
  List<Person> findByLastnameAndFirstnameAllIgnoreCase(String lastname, String firstname);
 
  // Enabling static ORDER BY for a query
  List<Person> findByLastnameOrderByFirstnameAsc(String lastname);
  List<Person> findByLastnameOrderByFirstnameDesc(String lastname);
}

 

 Special parameter handling

Example 17. Using PageableSlice, and Sort in query methods

Page<User> findByLastname(String lastname, Pageable pageable);
 
Slice<User> findByLastname(String lastname, Pageable pageable);
 
List<User> findByLastname(String lastname, Sort sort);
 
List<User> findByLastname(String lastname, Pageable pageable);

 

Limiting Query Results

User findFirstByOrderByLastnameAsc();
 
User findTopByOrderByAgeDesc();
 
Page<User> queryFirst10ByLastname(String lastname, Pageable pageable);
 
Slice<User> findTop3ByLastname(String lastname, Pageable pageable);
 
List<User> findFirst10ByLastname(String lastname, Sort sort);
 
List<User> findTop10ByLastname(String lastname, Pageable pageable);

 

Streaming query results

The results of query methods can be processed incrementally by using a Java 8 Stream<T> as return type. Instead of wrapping the query results in a Stream data store-specific methods are used to perform the streaming, as shown in the following example:

Example 19. Stream the result of a query with Java 8 Stream<T>

@Query("select u from User u")

Stream<User> findAllByCustomQueryAndStream();

 

Stream<User> readAllByFirstnameNotNull();

 

@Query("select u from User u")

Stream<User> streamAllPaged(Pageable pageable);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值