Spring Boot 3.2.5集成mysql的详细步骤记录
作者:罗_三金
作为一名Java开发者,我们经常需要在我们的应用程序中使用数据库,在Spring Boot中集成数据库是非常容易的,下面这篇文章主要给大家介绍了关于Spring Boot 3.2.5集成mysql的详细步骤,需要的朋友可以参考下
版本
Spring Boot 3.2.5
第一步,添加必要依赖
// mysql jdbc 及 驱动 <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配置数据库连接
server: port: 8079 spring: application: name: demo # 数据库连接 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项目结构:
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 实现:
# TServiceImpl 文件里的代码实现 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("数据库连接成功!"); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } return List.of(); } }
访问API:
总结
到此这篇关于Spring Boot 3.2.5集成mysql的文章就介绍到这了,更多相关SpringBoot3.2.5集成mysql内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!