亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring?Boot?3.2.5集成mysql的詳細(xì)步驟記錄

 更新時(shí)間:2024年04月27日 10:06:44   作者:羅_三金  
作為一名Java開發(fā)者,我們經(jīng)常需要在我們的應(yīng)用程序中使用數(shù)據(jù)庫,在Spring Boot中集成數(shù)據(jù)庫是非常容易的,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot?3.2.5集成mysql的詳細(xì)步驟,需要的朋友可以參考下

版本

Spring Boot 3.2.5

第一步,添加必要依賴

// mysql jdbc 及 驅(qū)動(dòng)
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
   <groupId>com.mysql</groupId>
   <artifactId>mysql-connector-j</artifactId>
   <scope>runtime</scope>
</dependency>

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 https://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>3.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>
    <properties>
        <java.version>17</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-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</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>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>3.0.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

第二步,yml配置數(shù)據(jù)庫連接

server:
  port: 8079

spring:
  application:
    name: demo
  # 數(shù)據(jù)庫連接
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/arrow_smart_toilet_database?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

第三步,測(cè)試連接

demo項(xiàng)目結(jié)構(gòu):

User實(shí)體

package com.example.demo.entity;

import lombok.Data;

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

controller代碼:

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.TService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/t")
public class T {
    @Autowired
    private TService tService;

    @GetMapping("/getUser")
    public List<User> getUserList() {
        return tService.getUsers();
    }
}

service層代碼:

# TService 文件里的代碼
package com.example.demo.service;

import com.example.demo.entity.User;

import java.util.List;

public interface TService {
    List<User> getUsers();
}

TServiceImpl 實(shí)現(xiàn):

# TServiceImpl 文件里的代碼實(shí)現(xiàn)
package com.example.demo.service.impl;

import com.example.demo.entity.User;
import com.example.demo.service.TService;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

@Service
public class TServiceImpl implements TService {
    @Autowired
    private DataSource dataSource;

    @Override
    public List<User> getUsers() {
        try (Connection connection = dataSource.getConnection()){
            System.out.println("數(shù)據(jù)庫連接成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return List.of();
    }
}

訪問API:

總結(jié) 

到此這篇關(guān)于Spring Boot 3.2.5集成mysql的文章就介紹到這了,更多相關(guān)SpringBoot3.2.5集成mysql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java序列化常見的三個(gè)問題

    Java序列化常見的三個(gè)問題

    這篇文章主要介紹了Java序列化常見的三個(gè)問題,幫助大家更好的理解和學(xué)習(xí)JAVA,感興趣的朋友可以了解下
    2020-08-08
  • Spring項(xiàng)目集成RabbitMQ及自動(dòng)創(chuàng)建隊(duì)列

    Spring項(xiàng)目集成RabbitMQ及自動(dòng)創(chuàng)建隊(duì)列

    這篇文章主要介紹了Spring項(xiàng)目集成RabbitMQ及自動(dòng)創(chuàng)建隊(duì)列,本文內(nèi)容分別在Spring(V5.2.6)和Spring Boot(V2.5.14)兩個(gè)項(xiàng)目中經(jīng)過了驗(yàn)證,需要的朋友可以參考下
    2024-02-02
  • JAVA遍歷map的幾種實(shí)現(xiàn)方法代碼

    JAVA遍歷map的幾種實(shí)現(xiàn)方法代碼

    這篇文章主要介紹了JAVA遍歷map的幾種實(shí)現(xiàn)方法,有需要的朋友可以參考一下
    2014-01-01
  • 深入同步訪問共享的可變數(shù)據(jù)分析

    深入同步訪問共享的可變數(shù)據(jù)分析

    本篇文章是對(duì)同步訪問共享的可變數(shù)據(jù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Spring框架基于xml實(shí)現(xiàn)自動(dòng)裝配流程詳解

    Spring框架基于xml實(shí)現(xiàn)自動(dòng)裝配流程詳解

    自動(dòng)裝配就是指?Spring?容器在不使用?<constructor-arg>?和<property>?標(biāo)簽的情況下,可以自動(dòng)裝配(autowire)相互協(xié)作的?Bean?之間的關(guān)聯(lián)關(guān)系,將一個(gè)?Bean?注入其他?Bean?的?Property?中
    2022-11-11
  • Maven dependency中的scope案例講解

    Maven dependency中的scope案例講解

    Maven的一個(gè)哲學(xué)是慣例優(yōu)于配置(Convention Over Configuration), Maven默認(rèn)的依賴配置項(xiàng)中,scope的默認(rèn)值是compile,本文給大家介紹Maven dependency中的scope案例講解,感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • Maven中央倉庫地址配置大全

    Maven中央倉庫地址配置大全

    這篇文章主要介紹了Maven中央倉庫地址配置大全,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 如何將maven源改為國內(nèi)阿里云鏡像

    如何將maven源改為國內(nèi)阿里云鏡像

    在使用Maven打包Scala程序時(shí),默認(rèn)是從位于國外的Maven中央倉庫下載相關(guān)的依賴,造成我們從國內(nèi)下載依賴時(shí)速度很慢,下面這篇文章主要給大家介紹了關(guān)于如何將maven源改為國內(nèi)阿里云鏡像的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • idea沒有services窗口、沒有springboot啟動(dòng)項(xiàng)問題

    idea沒有services窗口、沒有springboot啟動(dòng)項(xiàng)問題

    這篇文章主要介紹了idea沒有services窗口、沒有springboot啟動(dòng)項(xiàng)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java自定義枚舉的toString方法以展示其字段信息

    Java自定義枚舉的toString方法以展示其字段信息

    這篇文章主要為大家詳細(xì)介紹了Java如何自定義枚舉的?toString?方法以展示其字段信息,并提供一個(gè)完整且可以直接運(yùn)行的代碼示例,需要的可以參考下
    2024-12-12

最新評(píng)論