【SpringBoot】16 数据访问P4 整合JPA

DAO面向SpringData操作

Spring Data 项目的目的是为了简化构建基于 Spring 框架应用的数据访问技术,

包括非关系数据库、Map-Reduce 框架、云数据服务等等;

另外也包含对关系数据库的访问支持。

Spring Data 包含多个子项目:

– Spring Data Commons

– Spring Data JPA

– Spring Data KeyValue

– Spring Data LDAP

– Spring Data MongoDB

– Spring Data Gemfire

– Spring Data REST

– Spring Data Redis

– Spring Data for Apache Cassandra

– Spring Data for Apache Solr

– Spring Data Couchbase (community module)

– Spring Data Elasticsearch (community module)

– Spring Data Neo4j (community module)

- 持久操作支持

- 大量的封装模板

- 各种对象映射

1、SpringData特点

SpringData为我们提供使用统一的API来对数据访问层进行操作;

这主要是Spring DataCommons项目来实现的。

Spring Data Commons让我们在使用关系型或者非关系型数据访问技术时都基于Spring提供的统一标准,

标准包含了CRUD(创建、获取、更新、删除)、查询、排序和分页的相关操作。

2、统一的Repository接口

统一接口:

Repository<T, ID extends Serializable>

基于乐观锁机制:

RevisionRepository<T, ID extends Serializable, N extends Number & Comparable<N>>

基本CRUD操作:

CrudRepository<T, ID extends Serializable>

基本CRUD及分页

PagingAndSortingRepository<T, ID extends Serializable>

3、提供数据访问模板类 xxxTemplate;

如:MongoTemplate、RedisTemplate等

4、JPA与Spring Data

1)、JpaRepository基本功能

  编写接口继承JpaRepository既有crud及分页等基本功能

2)、定义符合规范的方法命名

  在接口中只需要声明符合规范的方法,即拥有对应的功能

3)、@Query自定义查询,定制查询SQL

4)、Specifications查询(Spring Data JPA支持JPA2.0的Criteria查询)

持久层结构示意图:


整合JPA【Java持久化API】

操作步骤:

1、引入spring-boot-starter-data-jpa

2、配置文件打印SQL语句

3、创建Entity标注JPA注解

4、创建Repository接口继承JpaRepository

5、测试方法

导入依赖组件

SpringDataJPA是对JPA规范的再封装,所以用SpringDataJPA学习JPA是一样的

配置访问参数

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql:///mybatis?serveTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456

编写对应的JPA规范实体类

package cn.dai.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

/**
 * @author ArkD42
 * @file SpringBoot with JPA
 * @create 2020 - 06 - 01 - 15:35
 */
@Entity
@Table
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private Integer user_id;
    
    @Column
    private String user_name;
    
    @Column
    private String user_password;
}

编写Dao接口

【SpringData只要这个接口继承了Jpa持久化接口,并且注入了ORM实体类和主键类型,就可以了】

package cn.dai.repo;

import cn.dai.entity.User;

import org.springframework.data.jpa.repository.JpaRepository;


/**
 * @author ArkD42
 * @file SpringBoot with JPA
 * @create 2020 - 06 - 01 - 15:43
 */
public interface UserRepository extends JpaRepository<User,Integer> {
    
}

然后配置其他非主要设置

#JPA配置
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

测试类

被害惨了,getOne方法不行,findOne方法没有,

还是弹幕说的findById().get()才起效的

package cn.dai.controller;

import cn.dai.entity.User;
import cn.dai.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ArkD42
 * @file SpringBoot with JPA
 * @create 2020 - 06 - 01 - 16:06
 */
@RestController
public class UserController {

    @Autowired
    UserRepository userRepository;

    @GetMapping("/user/get_user_by_id/{user_id}")
    public User getUser(@PathVariable("user_id") Integer id){
        return userRepository.findById(id).get(); // 老版本是findOne
    }

    @GetMapping("/user/add_user")
    public User saveUser(User user){
        return userRepository.save(user); //打印一下保存的User
    }


}

原文地址:https://www.cnblogs.com/mindzone/p/13024752.html