Zoeken…


Invoering

Spring Boot kan worden gebruikt om een SQL Relational DataBase te bouwen en te behouden. U kunt ervoor kiezen om verbinding te maken met een H2 in geheugen DataBase met behulp van Spring Boot, of misschien kiezen om verbinding te maken met MySql DataBase, het is helemaal uw keuze. Als u CRUD-bewerkingen tegen uw DB wilt uitvoeren, kunt u dit doen met JdbcTemplate bean, deze bean wordt automatisch geleverd door Spring Boot. Spring Boot helpt u door een automatische configuratie van enkele veelgebruikte bonen met betrekking tot JDBC te bieden.

Opmerkingen

Om te beginnen, ga je in je st eclips naar nieuw -> Spring Starter Project -> vul je Maven coördinaten in -> en voeg de volgende afhankelijkheden toe:

Op het tabblad SQL -> voeg JDBC toe + voeg MySql toe (als MySql uw keuze is).

Voor Mysql moet u ook de MySql Java Connector toevoegen.

In uw Spring Boot application.properties-bestand (uw Spring Boot-configuratiebestand) moet u uw gegevensbronreferenties configureren voor MySql DB:

  1. spring.datasource.url
  2. spring.datasource.username
  3. spring.datasource.password
  4. spring.datasource.driver-class-naam

bijvoorbeeld:

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Voeg onder de map resources de volgende twee bestanden toe:

  1. schema.sql -> Telkens wanneer u uw toepassing uitvoert, wordt dit bestand uitgevoerd door Spring Boot. Binnenin denkt u dat u uw DB-schema moet schrijven, tabellen en hun relaties moet definiëren.

  2. data.sql -> elke keer dat u uw applicatie uitvoert, zal Spring Boot dit bestand uitvoeren, u veronderstelt gegevens te schrijven die als initiële initialisatie in uw tabel worden ingevoegd.

Spring Boot geeft je automatisch JdbcTemplate bean zodat je het meteen kunt gebruiken:

@Autowired
private JdbcTemplate template;

zonder andere configuraties.

schema.sql-bestand

CREATE SCHEMA IF NOT EXISTS `backgammon`;
USE `backgammon`;

DROP TABLE IF EXISTS `user_in_game_room`;
DROP TABLE IF EXISTS `game_users`;
DROP TABLE IF EXISTS `user_in_game_room`;

CREATE TABLE `game_users`
(
    `user_id` BIGINT NOT NULL AUTO_INCREMENT,
    `first_name` VARCHAR(255) NOT NULL,
    `last_name` VARCHAR(255) NOT NULL,
    `email` VARCHAR(255) NOT NULL UNIQUE,
    `user_name` VARCHAR(255) NOT NULL UNIQUE,
    `password` VARCHAR(255) NOT NULL,
    `role` VARCHAR(255) NOT NULL,
    `last_updated_date` DATETIME NOT NULL,
    `last_updated_by` BIGINT NOT NULL,
    `created_date` DATETIME NOT NULL,
    `created_by` BIGINT NOT NULL,
    PRIMARY KEY(`user_id`)
);

DROP TABLE IF EXISTS `game_rooms`;

CREATE TABLE `game_rooms`
(
    `game_room_id` BIGINT NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL,
    `private` BIT(1) NOT NULL,
    `white` BIGINT DEFAULT NULL,
    `black` BIGINT DEFAULT NULL,
    `opened_by` BIGINT NOT NULL,
    `speed` BIT(3) NOT NULL,
    `last_updated_date` DATETIME NOT NULL,
    `last_updated_by` BIGINT NOT NULL,
    `created_date` DATETIME NOT NULL,
    `created_by` BIGINT NOT NULL,
    `token` VARCHAR(255) AS (SHA1(CONCAT(`name`, "This is a qwe secret 123", `created_by`, `created_date`))),
    PRIMARY KEY(`game_room_id`)
);

CREATE TABLE `user_in_game_room`
(
    `user_id` BIGINT NOT NULL,
    `game_room_id` BIGINT NOT NULL,
    `last_updated_date` DATETIME NOT NULL,
    `last_updated_by` BIGINT NOT NULL,
    `created_date` DATETIME NOT NULL,
    `created_by` BIGINT NOT NULL,
    PRIMARY KEY(`user_id`, `game_room_id`),
    FOREIGN KEY (`user_id`) REFERENCES `game_users`(`user_id`),
    FOREIGN KEY (`game_room_id`) REFERENCES `game_rooms`(`game_room_id`)
);

Eerste JdbcTemplate Boot-app

@SpringBootApplication
@RestController
public class SpringBootJdbcApplication {

    @Autowired
    private JdbcTemplate template;
    
    @RequestMapping("/cars")
    public List<Map<String,Object>> stocks(){
        return template.queryForList("select * from car");
    }
    
    public static void main(String[] args) {
        SpringApplication.run(SpringBootJdbcApplication.class, args);
    }
}

data.sql

insert into game_users values(..., ..., ..., ...);
insert into game_users values(..., ..., ..., ...);
insert into game_users values(..., ..., ..., ...);


Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow