SpringBoot 链接数据库

本文详细介绍了如何在Spring Boot项目中配置并连接MySQL数据库,包括在pom.xml中添加依赖,以及在application.yml或application.properties中设置数据库连接参数。

1:在pom.xml中加入:

<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>

2:在application.yml中添加一下代码,没有的话可以自己新建,也可在application.properties中添加,

application.yml中添加一下代码
spring:
  datasource:
  #   第一个jdbc是以什么方式来连接数据库,   第二个jdbc是我数据库的名字.
  #    ?serverTimezone=UTC :  因情况而定,我不加这句代码会报出错:java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.
    url: jdbc:mysql://127.0.0.1:3306/jdbc?serverTimezone=UTC
  #    driverClassName: com.mysql.jdbc.Driver   
    username: root
    password: jhs123

 

也可在application.properties中添加

spring.datasource.url= jdbc:mysql://127.0.0.1:3306/jdbc?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=jhs123

application.yml  和 application.properties  只需要添加一个就可以   

 

 

这样就成功了 >>  下面我们来做一下测试!

@RunWith(SpringRunner.class)
@SpringBootTest
public class Jdbc11ApplicationTests {

    @Qualifier("dataSource")
    @Autowired  //自动注入
    DataSource dataSource;//数据源


    @Test
    public void contextLoads() throws SQLException {
        System.out.println("这是数据源:"+dataSource.getClass());
        Connection connection = dataSource.getConnection();
        System.out.println("链接方式"+connection);
        connection.close();//关闭连接
    }

}

运行  

  成功连上数据库了

 

 

 

 

 

 

 

 

 

 

 

 

 

Spring Boot有两种方法与数据库建立连接,一种是使用JdbcTemplate,另一种是集成Mybatis [^1]。 使用JdbcTemplate时,需要添加以下依赖: ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> ``` 可以通过以下代码在测试中查看数据源的信息: ```java package com.wlm; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; @SpringBootTest class Springboot04DataApplicationTests { @Autowired DataSource dataSource; @Test void contextLoads() throws SQLException { //查看默认的数据源: com.zaxxer.hikari.HikariDataSource System.out.println(dataSource.getClass()); //获得数据库连接 Connection connection = dataSource.getConnection(); System.out.println(connection); //我们以后会经常看见xxxxTemplate,这个是spring boot已经配置好的模板@bean,拿来即用 //关闭 connection.close(); } } ``` 还可以直在controller操作crud,示例代码如下: ```java package com.springboot.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.*; import java.util.List; import java.util.Map; @Controller public class JdbcController { @Autowired(required = false) JdbcTemplate jdbcTemplate; @GetMapping("/sql") @ResponseBody public List<Map<String,Object>> mapList(){ String sql="select * from user"; List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(sql); return queryForList; } } ``` 若使用MyBatis连接,虽未给出具体集成步骤,但可以参考配置数据源以及JPA的示例: ```yaml spring: datasource: username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis?setTimezone=UTC&useUnicode=true&characterEncoding=utf-8 jpa: hibernate: ddl-auto: update # 连接数据库时会与其进行比对,自动更新数据库的表结构(生产环境确定了之后改为none) show-sql: true # 控制台显示SQL ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值