Spring Boot使用JDBC方式连接MySQL

Posted <・)))><<

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot使用JDBC方式连接MySQL相关的知识,希望对你有一定的参考价值。

首先去spring官网下载一个名为test的Spring Boot项目模板:https://start.spring.io/

然后在mysql中的testdb数据库中新建一张名为test_user的表:

drop table if exists `test_user`;
create table `test_user` (
    `id` integer not null auto_increment primary key,
    `name` varchar(20),
    `password` varchar(20)
);
insert into test_user (`name`, `password`) values (‘zifeiy‘, ‘123456‘), (‘apple‘, ‘654321‘);

项目中,首先在pom.xml中引入依赖:

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

在application.properties中填写MySQL连接相关的信息:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testdb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

然后新建一个Java Bean:TestUser.java:

package com.zifeiy.test.po;

public class TestUser {
    private Integer id;
    private String name;
    private String password;
    
    // getters & setters
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

然后在测试类中编辑如下代码进行测试:

package com.zifeiy.test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.junit4.SpringRunner;

import com.zifeiy.test.po.TestUser;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestApplicationTests {
    
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Test
    public void contextLoads() {
        String sql = "select * from test_user";
        List<TestUser> userList = (List<TestUser>) jdbcTemplate.query(sql, new RowMapper<TestUser>() {

            @Override
            public TestUser mapRow(ResultSet rs, int rowNum) throws SQLException {
                TestUser user = new TestUser();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
                return user;
            }
            
        });
        System.out.println("查询成功");
        for (TestUser user : userList) {
            System.out.println("id = " + user.getId() + " , name = " + user.getName() + " , password = " + user.getPassword());
        }
    }

}

在命令行中得到测试结果如下:

查询成功
id = 1 , name = zifeiy , password = 123456
id = 2 , name = apple , password = 654321

以上是关于Spring Boot使用JDBC方式连接MySQL的主要内容,如果未能解决你的问题,请参考以下文章

(转) Spring Boot JDBC 连接数据库

spring Boot构建的Web应用中,基于MySQL数据库的几种数据库连接方式进行介绍

Spring Boot高版本配置数据库连接驱动问题

如何使用 Spring Boot 设置 Spring JDBC 连接池?

spring-boot:数据库中断后jdbc重新连接

Spring Boot JPA 连接数据库