Добавить параметр дженерика с указанием типа:
package com.example.ordersbackend.controllers;
import org.springframework.data.repository.CrudRepository;
import com.example.ordersbackend.models.Post;
public interface PostRepository extends CrudRepository {
}
Проверить настройки соединения
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/posts
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql: true
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
package com.example.ordersbackend.controllers
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ResponseBody
@Controller
class MainController {
@Autowired
private lateinit var postRepository: PostRepository
@GetMapping("/")
@ResponseBody
open fun getAllUsers(): MutableIterable<Any?> {
// This returns a JSON or XML with the users
return postRepository.findAll()
}
}
PostRepository
package com.example.ordersbackend.controllers;
import org.springframework.data.repository.CrudRepository;
public interface PostRepository extends CrudRepository {
}
Post
package com.example.ordersbackend.models
import jakarta.persistence.*
@Entity
@Table(name = "posts")
class Post {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private var id: Short? = null
var title: String = ""
fun getId(): Short? {
return id
}
fun setId(id: Short) {
this.id = id
}
fun getTitle(): String {
return title
}
fun setTitle(title: String) {
this.title = title
}
}
application.properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/posts
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql: true
Ошибки
java.lang.IllegalArgumentException: Could not resolve domain type of interface com.example.ordersbackend.controllers.PostRepository