无法使用存储库findAll()读取数据,也没有收到错误

时间:2018-07-06 14:35:33

标签: java mysql spring spring-boot

我按照本文https://www.callicoder.com/spring-boot-rest-api-tutorial-with-mysql-jpa-hibernate/的要求将MySql与SpringBoot应用程序集成在一起。我将文件Note.java和NoteRepository.java添加到了一个新包-

1。 note.java-模型更改-

package com.citruspay.prepaid.internal.manager.mysql;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.validator.constraints.NotBlank;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "Note")
// @EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = { "createdAt", "updatedAt" }, allowGetters = true)
public class Note implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank
    private String title;

    @Column(nullable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    // @CreatedDate
    private Date createdAt;

    @Column(nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    // @LastModifiedDate
    private Date updatedAt;

    // Getters and Setters ... (Omitted for brevity)
}

2。 NoteRepository.java-

package com.citruspay.prepaid.internal.manager.mysql;

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

@Repository
public interface NoteRepository extends JpaRepository<Note, Long> {

}

3。 Application.properties更改-

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username = root
spring.datasource.password = root


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = none
spring.jpa.hibernate.show-sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

4。将以下内容添加到App.java-

@EnableJpaRepositories("com.citruspay.prepaid.internal.manager.mysql")
@EntityScan("com.citruspay.prepaid.internal.manager.mysql")
@EnableTransactionManagement

5。在pom.xml中添加了以下内容-

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

6。以下是我手动插入的表名和架构-

CREATE TABLE `note` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(60) NOT NULL,
  `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `updatedAt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;

正在运行的应用程序

在调试模式下运行应用程序,并命中添加了findAll()调用的端点时,在存储库上的调用中我没有得到任何错误,但是得到了一个空结果集-

List<Note> note = noteRepository.findAll();

我添加了NoteNoteRepository;的导入,并自动连接了NoteRepository

我尝试调试mysql查询,以查看查询是否真的发生了。将它们添加到application.properties中,但不打印查询-

spring.jpa.hibernate.show-sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

如果我的理解错误,请更正。

我读了JpaRepository findAll() returns empty result,但无法解决我的问题。

顺便说一句,我主要是来自PHP背景的开发人员,并且在SpringBoot的配置,注释等方面工作很辛苦。请为您推荐一些免费或付费的教程视频,以便快速掌握。

更新

日志中没有打印查询。我看到此日志行是针对findAll()语句执行生成的-

2018-07-06 19:32:08,723 84821 [XNIO-3 task-1] INFO  o.h.h.i.QueryTranslatorFactoryInitiator [QueryTranslatorFactoryInitiator.java:47] - HHH000397: Using ASTQueryTranslatorFactory

更新2

样本数据-

INSERT INTO `note` (`id`, `title`, `createdAt`, `updatedAt`) VALUES
    (1, 'test', '2018-07-06 19:01:50', '0000-00-00 00:00:00'),
    (2, 'hello', '2018-07-06 19:01:58', '0000-00-00 00:00:00');

0 个答案:

没有答案