spring boot+jpa+mysql,无法检索结果集

jfewjypa  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(442)

我是springboot新手,对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 table details schema=test\u schema和table=students
错误详细信息

hi3rlvi2

hi3rlvi21#

从错误日志
原因:java.sql.sqlexception:未选择数据库
很明显,spring无法找到数据库,所以请按照下面的方法选择数据库。
更改 spring.datasource.url 通过添加数据库名称将值添加到下面。

spring.datasource.url = jdbc:mysql://localhost:3306/test_schema
2q5ifsrm

2q5ifsrm2#

Caused by: java.sql.SQLException: No database selected at

这说明必须选择数据库名称,因此必须将其添加到配置中。我看到你已经有答案了!

spring.datasource.url = jdbc:mysql://localhost:3306/YOUR_DB_SCHEMA

相关问题