Spring?Boot?3.2.5集成mysql的詳細步驟記錄
更新時間: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的詳細步驟,需要的朋友可以參考下
版本
Spring Boot 3.2.5
第一步,添加必要依賴
// mysql jdbc 及 驅(qū)動 <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
第三步,測試連接
demo項目結(jié)構(gòu):
User實體
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 實現(xiàn):
# TServiceImpl 文件里的代碼實現(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring項目集成RabbitMQ及自動創(chuàng)建隊列
這篇文章主要介紹了Spring項目集成RabbitMQ及自動創(chuàng)建隊列,本文內(nèi)容分別在Spring(V5.2.6)和Spring Boot(V2.5.14)兩個項目中經(jīng)過了驗證,需要的朋友可以參考下2024-02-02idea沒有services窗口、沒有springboot啟動項問題
這篇文章主要介紹了idea沒有services窗口、沒有springboot啟動項問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05