mysql - spring boot + JPA + mysql,无法检索结果集
问题描述
我是spring boot的新手,对java有基本的了解。我正在尝试构建一个微服务来从 mySQL 数据库中检索数据。以下是代码详细信息。
我能够使用独立的 JDBC 类从同一个表中检索数据。请求帮助。
- 实体类
package com.studentProject.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.stereotype.Component;
@Entity
@Component
@Table(name="students", schema="test_schema")
public class StudentEntity implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name="id")
private Integer id;
@Column(name="name")
private String name;
@Column(name="course")
private String course;
@Column(name="country")
private String country;
@Column(name="phone")
private String phone;
public StudentEntity(Integer id, String name, String course, String country, String phone) {
super();
this.id = id;
this.name = name;
this.course = course;
this.country = country;
this.phone = phone;
}
public StudentEntity() {
super();
// TODO Auto-generated constructor stub
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
-存储库类
package com.studentProject.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.studentProject.entity.StudentEntity;
@Repository
public interface StudentRepository extends CrudRepository<StudentEntity, Integer> {
StudentEntity findByName(String name);
StudentEntity findByCourse(String course);
}
-控制器类
package com.studentProject.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.studentProject.entity.StudentEntity;
import com.studentProject.repository.StudentRepository;
@RestController
public class StudentController {
@Autowired
private StudentRepository studentRepository;
@Autowired
List<StudentEntity> studentList;
public StudentController(StudentRepository studentRepository, StudentEntity studentEntity,
List<StudentEntity> studentList) {
super();
this.studentRepository = studentRepository;
this.studentList = studentList;
}
@GetMapping("/getStudentDetails")
public Iterable<StudentEntity> getAllStudents(){
return studentRepository.findAll();
}
@GetMapping("/getStudentDetails/getByName")
public StudentEntity findByName(@RequestParam(value="name") String name) {
return studentRepository.findByName(name);
}
@GetMapping("/getStudentDetails/getById/{id}")
public Optional<StudentEntity> findById(@PathVariable(value="id") Integer id) {
return studentRepository.findById(id);
}
@GetMapping("/getStudentDetails/welcome")
public String welcome() {
return "hi brother welcome";
}
}
- 属性文件
server.port = 8080
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/
spring.datasource.username = root
spring.datasource.password = root
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.data.jpa.repositories.enabled=true
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = none
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
--Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.StudentProject</groupId>
<artifactId>StudentProject-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>StudentProject-1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 主要应用
package com.studentProject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StudentProject1Application {
public static void main(String[] args) {
SpringApplication.run(StudentProject1Application.class, args);
}
}
-mysql 表详细信息 schema= test_schema和 table= students
错误详情
解决方案
Caused by: java.sql.SQLException: No database selected at
这告诉您必须选择数据库名称,因此您必须将其添加到您的配置中。我看到你已经有答案了!
spring.datasource.url = jdbc:mysql://localhost:3306/YOUR_DB_SCHEMA
推荐阅读
- ios - 如何在 iOS 上的 Swift 中以编程方式获取 UDID
- c# - 复合表存储查询
- javascript - 更新 PartialView 只工作一次
- php - PHP 访问由其他命名空间元素生成的对象
- reactjs - 反应应用程序的生产版本缺少几个文件夹
- applescript - Applescript 将附件从邮件保存到外部高清
- powershell - 点源导致 PS 控制台忽略 PS 脚本文件中的更改
- javascript - 如何从比较数组创建数组?
- java - INNER Join 查询上的 Java 错误
- python - 使用python生成具有限制和一定长度的给定数字的随机列表