Spring Boot R2DB C с MySQL - Исключение: таблица не найдена - PullRequest
3 голосов
/ 22 марта 2020

Я чрезвычайно новичок в String Boot и бэкэнд-разработке (возможно, три дня или меньше), и у меня есть желание построить REST API для использования от разных клиентов.

Итак, я начал с простого демонстрационного приложения, конечная точка которого называется /register. Мы публикуем строку JSON с username и password для создания нового пользователя, если он не существует.

Я использовал JPA с HSQLDB, и он отлично работал, сохраняя память. Но недавно я хотел использовать RxJava, так как я знаком с Android, поэтому я переключился на R2DBC с MySQL.

MySQL сервер работает нормально на порту 3306 и приложение было протестировано с использованием PostMan на localhost:8080

Проблема возникает, когда я пытаюсь запросить таблицу пользователей или вставить сущности, и это выглядит так:

{
    "timestamp": "2020-03-22T11:54:43.466+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "execute; bad SQL grammar [UPDATE user_entity SET username = $1, password = $2 WHERE user_entity.id = $3]; nested exception is io.r2dbc.spi.R2dbcBadGrammarException: [42102] [42S02] Table \"USER_ENTITY\" not found; SQL statement:\nUPDATE user_entity SET username = $1, password = $2 WHERE user_entity.id = $3 [42102-200]",
    "path": "/register"
}

Вот полный logfile для исключения.

Я искал решение в течение нескольких часов, и мне кажется, что я его нигде не нашел, поэтому я надеюсь, что найду его здесь.

Давайте разбить проект, чтобы было легче найти решение:

1. база данных:

enter image description here

2. application.properties:

logging.level.org.springframework.data.r2dbc=DEBUG
spring.datasource.url=jdbc:mysql://localhost:3306/demodb
spring.datasource.username=root
spring.datasource.password=root

3. Конфигурация базы данных:

@Configuration
@EnableR2dbcRepositories
class DatabaseConfiguration : AbstractR2dbcConfiguration() {

    override fun connectionFactory(): ConnectionFactory
         = ConnectionFactories.get(
                builder().option(DRIVER, "mysql")
                    .option(HOST, "localhost")
                    .option(USER, "root")
                    .option(PASSWORD, "root") 
                    .option(DATABASE, "demodb")
                    .build()
        )


}

4. Контроллер регистрации:

@RequestMapping("/register")
@RestController
class RegistrationController @Autowired constructor(private val userService: UserService) {

    @PostMapping
    fun login(@RequestBody registrationRequest: RegistrationRequest): Single<ResponseEntity<String>>
        = userService.userExists(registrationRequest.username)
            .flatMap { exists -> handleUserExistance(exists, registrationRequest) }

    private fun handleUserExistance(exists: Boolean, registrationRequest: RegistrationRequest): Single<ResponseEntity<String>> 
        = if (exists) Single.just(ResponseEntity("Username already exists. Please try an other one", HttpStatus.CONFLICT))
            else userService.insert(User(registrationRequest.username, registrationRequest.password)).map { user ->
                ResponseEntity("User was successfully created with the id: ${user.id}", HttpStatus.CREATED)
            }

}

5. UserService:

@Service
class UserService @Autowired constructor(override val repository: IRxUserRepository) : RxSimpleService<User, UserEntity>(repository) {

    override val converter: EntityConverter<User, UserEntity> = UserEntity.Converter

    fun userExists(username: String): Single<Boolean>
        = repository.existsByUsername(username)

}

6. RxSimpleService:

abstract class RxSimpleService<T, E>(protected open val repository: RxJava2CrudRepository<E, Long>)  {

    protected abstract val converter: EntityConverter<T, E>

    open fun insert(model: T): Single<T>
        = repository.save(converter.fromModel(model))
            .map(converter::toModel)

    open fun get(id: Long): Maybe<T>
        = repository.findById(id)
            .map(converter::toModel)

    open fun getAll(): Single<ArrayList<T>>
        = repository.findAll()
            .toList()
            .map(converter::toModels)

    open fun delete(model: T): Completable
        = repository.delete(converter.fromModel(model))

}

7. RxUserRepository:

@Repository
interface IRxUserRepository : RxJava2CrudRepository<UserEntity, Long> {

    @Query("SELECT CASE WHEN EXISTS ( SELECT * FROM ${UserEntity.TABLE_NAME} WHERE username = :username) THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END")
    fun existsByUsername(username: String): Single<Boolean>

}

8. И, наконец, вот мои UserEntity

@Table(TABLE_NAME)
data class UserEntity(
        @Id
        val id: Long,
        val username: String,
        val password: String
)  {

        companion object {
            const val TABLE_NAME = "user_entity"
        }

        object Converter : EntityConverter<User, UserEntity> {

            override fun fromModel(model: User): UserEntity
                   = with(model) { UserEntity(id, username, password) }

            override fun toModel(entity: UserEntity): User
                   = with(entity) { User(id, username, password) }

        }
}

User и RegistrationRequest - простые объекты с именем пользователя и паролем. Что я пропустил? Пожалуйста, оставьте комментарий, если вам нужно больше кода.

Ответы [ 3 ]

1 голос
/ 28 марта 2020

Мне наконец-то удалось решить эту ошибку!

Проблемы были настолько простыми, но настолько хитрыми для начинающего:

  1. Я использовал JDBC в своем URL вместо R2DBC
  2. Я использовал реализацию H2 во время выполнения, поэтому ожидал, что H2 база данных в памяти
  3. Моя ConnectionFactory была не очень правильной

Итак, я сделал следующее:

  1. Обновил мои build.gradle:
    • Добавлено: implementation("io.r2dbc:r2dbc-pool"), implementation("dev.miku:r2dbc-mysql:0.8.1.RELEASE") и runtimeOnly("mysql:mysql-connector-java")
    • Удалено: runtimeOnly("io.r2dbc:r2dbc-h2")

Теперь это выглядит так:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.2.5.RELEASE"
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
    kotlin("jvm") version "1.3.61"
    kotlin("plugin.spring") version "1.3.61"
}

group = "com.tamimattafi.backend"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
    maven(url = "https://repo.spring.io/milestone")
}

dependencies {
    //SPRING BOOT
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot.experimental:spring-boot-starter-data-r2dbc")

    //KOTLIN
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    //RX JAVA
    implementation("io.reactivex.rxjava2:rxjava:2.2.0")
    implementation("io.reactivex:rxjava-reactive-streams:1.2.1")

    //MYSQL
    implementation("dev.miku:r2dbc-mysql:0.8.1.RELEASE")
    implementation("io.r2dbc:r2dbc-pool")
    runtimeOnly("mysql:mysql-connector-java")

    //TEST
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
    testImplementation("org.springframework.security:spring-security-test")
    testImplementation("io.projectreactor:reactor-test")
    testImplementation("org.springframework.boot.experimental:spring-boot-test-autoconfigure-r2dbc")
}

dependencyManagement {
    imports {
        mavenBom("org.springframework.boot.experimental:spring-boot-bom-r2dbc:0.1.0.M3")
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    }
}

Обновил мой application.properties до этого:

spring.r2dbc.url=r2dbc:pool:mysql://127.0.0.1:3306/demodb
spring.r2dbc.username=root
spring.r2dbc.password=root

Обновил мой DatabaseConfiguration до этого (Обратите внимание, что я удалил @EnableR2dbcRepositories, потому что он должен быть в другом месте ):

@Configuration
class DatabaseConfiguration : AbstractR2dbcConfiguration() {

override fun connectionFactory(): ConnectionFactory
    = MySqlConnectionFactory.from(
        MySqlConnectionConfiguration.builder()
                .host("127.0.0.1")
                .username("root")
                .port(3306)
                .password("root")
                .database("demodb")
                .connectTimeout(Duration.ofSeconds(3))
                .useServerPrepareStatement()
                .build()
    )

}

Обновлен мой Application класс (я привел здесь аннотацию):

@SpringBootApplication
@EnableR2dbcRepositories
class DemoApplication

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}

ЭТО РАБОТАЕТ СЕЙЧАС! Я надеюсь, что кто-то найдет это полезным, Happy Coding!

0 голосов
/ 27 марта 2020

В application.properties необходимо установить свойство spring.jpa.hibernate.ddl-auto.

Возможны следующие варианты:

validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema when the SessionFactory is closed explicitly, typically when the application is stopped.
none: does nothing with the schema, makes no changes to the database
0 голосов
/ 26 марта 2020

Вы должны разрешить Hibernate Создать таблицу и столбцы для вашей сущности, так как она будет отображаться, поэтому пометьте вашу userEntity аннотациями и повторите попытку.

@Table(TABLE_NAME)
data class UserEntity(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        val id: Long,
        @Column(name = "")
        val username: String,
        @Column(name = "")
        val password: String
)  
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...