maven怎么连接mysql数据库配置_springboot+idea+mysql+maven 使用JDBC连接数据库实例快速入门(一)...

本文介绍了如何使用SpringBoot、IDEA和Maven配置JDBC连接MySQL数据库。首先通过Spring Initializr创建项目,添加相关依赖,然后配置数据库连接信息。接着,通过编写测试代码验证数据库连接,并创建HelloController展示查询数据库数据。这是一个适合初学者的快速入门教程。
摘要由CSDN通过智能技术生成

JDBC实例(springboot+idea+mysql+maven)spring Initializr链接数据库小demo(一)

上来就做一个完整的项目对于一个没有基础的人来说无疑说是有一些困难的,把一个项目拆分开慢慢学习,可能这也是所谓的慢慢来比较快?

创建一个spring Initializr项目

8daad0863a82261c4d364dceaedfdab3.png

OK,new >> project >> spring initializr >>next

1a4f85e48e6f4b7398511814824b6dd0.png

给自己的项目起个名字吧

OK,起名字之后,添加依赖。(关于springboot添加依赖我会在另一篇学习笔记中讲述~)

OK,需要最关键的jdbc(当然,先学jdbc然后再学mybatis等等之类的个人感觉比较好),当然还有mysqlc06bbe2619e447ccf4c3b6fb086cebb4.png

OK, next >> 完成

选择enable auto

初始目录结构

pom文件

可以看到我们之前添加的pom依赖,已经自动导入了

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0modelVersion>

org.springframework.bootgroupId>

spring-boot-starter-parentartifactId>

1.5.10.RELEASEversion>

parent>

com.jdbcgroupId>

demoartifactId>

0.0.1-SNAPSHOTversion>

demoname>

Demo project for Spring Bootdescription>

1.8java.version>

properties>

org.springframework.bootgroupId>

spring-boot-starter-jdbcartifactId>

dependency>

mysqlgroupId>

mysql-connector-javaartifactId>

runtimescope>

dependency>

org.springframework.bootgroupId>

spring-boot-starter-webartifactId>

dependency>

org.springframework.bootgroupId>

spring-boot-starter-testartifactId>

testscope>

dependency>

dependencies>

org.springframework.bootgroupId>

spring-boot-maven-pluginartifactId>

plugin>

plugins>

build>

project>

之后如果需要用到Mybatis等等,也是需要在pom中注册依赖,这里不再赘述,以后还会对这些进行详细得学习介绍

需求

使用JDBC连接数据库并使用sql语句进行crud操作

1.创建一个User数据库

7263d65adc74452662463b55844fcc65.png

当然,我之前新建过连接了,localhost:3306

9b58906748d9a8f16f8e25fefd82039c.png

OK,数据库创建好了

2.配置JDBC连接数据库

新建一个application.yml文件,当然使用properties也可以,语法略有不同

f9c8bf685545efb4074846f2a68330c1.png

Spring:

datasource:

username: root

password: root

url: jdbc:mysql://localhost:3306/User?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT

driver-class-name: com.mysql.jdbc.Driver

3.进行测试

跑一下这个text程序

3d26eed31ba1fd5d5ac94f404174c71b.png

3af7172d875db0c149bb052db50cd919.png

好了,测试连接数据库成功了

a9edb5ffc9cdb49eaddca28746a701a7.png

package com.jdbc.demo;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import javax.sql.DataSource;

import java.sql.Connection;

import java.sql.SQLException;

@RunWith(SpringRunner.class)

@SpringBootTest

public class DemoApplicationTests {

@Autowired

DataSource dataSource;

@Test

public void contextLoads() throws SQLException {

System.out.println(dataSource.getClass());

Connection connection = dataSource.getConnection();

System.out.println(connection);

connection.close();

}

}

测试连接成功

4.新建一个hellocontroller查询数据库数据

996bae095e0e4f648758826c89ca1e70.png

package com.jdbc.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import java.util.Map;

@Controller

public class HelloController {

@Autowired

JdbcTemplate jdbcTemplate;

@ResponseBody

@GetMapping("/query")

public Map map(){

List> list = jdbcTemplate.queryForList("select * FROM User");

return list.get(0);

}

}

在浏览器中输入localhost:8080/query,可以看到我们已经请求到了数据库中的数据

5ffe92f44cfe46f90faa011d3c5816b2.png

博客持续更新,各种遇到得坑,自己练习小实例。希望大佬可以指导~感谢。

有什么错误还请大家指正,查证后一定修改,谢谢~

分享springboot学习网课,点赞评论留下邮箱我发给你百度分享链接(免费!免费!!)留下邮箱直接发~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值