Springboot配置MySQL数据库

Springboot配置MySQL数据库

一、创建springboot项目,并添加如下依赖

xml 复制代码
<dependency>
       <groupId>com.mysql</groupId>
       <artifactId>mysql-connector-j</artifactId>
       <scope>runtime</scope>
</dependency>

二、在application.properties中配置连接数据库信息

properties 复制代码
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名称
spring.datasource.username=root
spring.datasource.password=密码

三、在启动类中创建代码,并运行测试:

java 复制代码
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context =SpringApplication.run(DemoApplication.class, args);
        JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
        List<Map<String, Object>> result =
                jdbcTemplate.queryForList("SELECT * FROM student");
        System.out.println(result);
    }
}

运行结果:

数据库数据:

相关推荐
不光头强41 分钟前
Spring框架的事务管理
数据库·spring·oracle
百***46451 小时前
Java进阶-在Ubuntu上部署SpringBoot应用
java·spring boot·ubuntu
百***92023 小时前
【MySQL】MySQL库的操作
android·数据库·mysql
q***76663 小时前
Spring Boot 从 2.7.x 升级到 3.3注意事项
数据库·hive·spring boot
信仰_2739932433 小时前
Redis红锁
数据库·redis·缓存
人间打气筒(Ada)3 小时前
Centos7 搭建hadoop2.7.2、hbase伪分布式集群
数据库·分布式·hbase
心灵宝贝3 小时前
如何在 Mac 上安装 MySQL 8.0.20.dmg(从下载到使用全流程)
数据库·mysql·macos
想睡hhh3 小时前
mysql索引——理解索引机制及操作
mysql
剑动山河3 小时前
ubuntu 升级mysql由mysql5.7.42 升级到8.4.0
mysql·ubuntu·adb
奋斗的牛马4 小时前
OFDM理解
网络·数据库·单片机·嵌入式硬件·fpga开发·信息与通信