【发布时间】:2019-05-15 17:26:24
【问题描述】:
我是 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
错误详情
【问题讨论】:
-
你能发布你的例外吗?
-
你的错误是什么?
-
出现意外错误(类型=内部服务器错误,状态=500)。无法提取结果集;嵌套异常是 org.hibernate.exception.GenericJDBCException:无法提取结果集 org.springframework.orm.jpa.JpaSystemException:无法提取结果集;嵌套异常是 org.hibernate.exception.GenericJDBCException: could not extract ResultSet
-
引起:java.sql.SQLException:在 com.mysql.cj.jdbc 的 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) 没有选择数据库。 exceptions.SQLError.createSQLException(SQLError.java:97) 在 com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) 在 com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:第974章)
-
您忘记在属性中添加数据库名称`"jdbc:mysql://localhost/databasename";`
标签: mysql spring spring-boot jpa microservices