SpringBoot整合MySql

系统要求

Java 8+

springBoot2.5 +

创建springBoot项目工程

导入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
编写application.yml
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3307/webapp1
    username: webapp1
    password: webapp1
进行测试
package com.xiang.springbootdatabase;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

@SpringBootTest
class SpringBootDatabaseApplicationTests {
    /*该对象是SpringBoot创建的可以对数据进行操作*/
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Test
    /**
     * 查询所有
     */
    void contextLoads() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from user;");
        for (Map<String, Object> map : list) {
            System.out.println(map);
        }
    }

}

运行结果

SpringBoot整合MySql

相关文章:

  • 2022-12-23
  • 2021-11-30
  • 2021-12-12
  • 2021-12-09
  • 2021-09-03
  • 2021-07-04
  • 2022-03-02
  • 2022-12-23
猜你喜欢
  • 2021-06-20
  • 2022-12-23
  • 2021-04-19
  • 2021-09-20
  • 2022-01-17
  • 2021-12-30
相关资源
相似解决方案