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);
    }
}

运行结果:

数据库数据:

相关推荐
q***017725 分钟前
【MySQL】表的基本操作
数据库·mysql·oracle
budingxiaomoli28 分钟前
存储过程和触发器
数据库
q***125340 分钟前
PostgreSQL_安装部署
数据库·postgresql
q***48251 小时前
mysql用户名怎么看
数据库·mysql
夏日玲子1 小时前
【Redis】Redis常用命令合集
数据库·redis·缓存
r***12381 小时前
若依微服务中配置 MySQL + DM 多数据源
android·mysql·微服务
万邦科技Lafite1 小时前
1688图片搜索商品API接口(item_search_img)使用指南
java·前端·数据库·开放api·电商开放平台
自在极意功。1 小时前
SQL查询语句深度解析:从基础到进阶,写出高效优雅的SQL!
数据库·sql
north_eagle1 小时前
MySQL 业务数据,报表方案
大数据·数据库
ALex_zry2 小时前
MySQL连接数管理与优化实操经验分享
android·mysql·adb