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

原文地址:https://www.cnblogs.com/zifeiy/p/9904244.html

时间: 2024-08-04 12:36:23

Spring Boot使用JDBC方式连接MySQL的相关文章

jmeter中通过jdbc方式连接mysql数据库的配置参考

jmeter中通过jdbc方式连接mysql数据库的配置参考: Database URL=jdbc:mysql://ip:port/dbname?useUnicode=true&characterEncoding=UTF-8 JDBC Driver class=com.mysql.jdbc.Driver jmeter中配置截图:

Spring Boot的数据源与连接池

? Create by [email protected] 2018-8-2 一:依赖 使用Spring Boot的默认数据源spring.datasource只需要导入如下依赖: <dependency> <groupId>org.springframework.boot</groupId> ??????????? <artifactId>spring‐boot‐starter‐jdbc</artifactId> ??????????? <

Spring Boot 整合JDBC 实现后端项目开发

一.前言 前后端分离开发是将项目开发工作前后端交互工作拆分,使前端开发人员能够专注于页面开发或APP 开发,后端开发人员专注与接口开发.业务逻辑开发. 此处开发后端项目,给前端或者APP 端提供接口.不涉及复杂的业务逻辑,通过简单的增删改查来演示后端开发项目. 环境介绍: 开发工具:IDEA JDK: 1.7 及以上 Spring Boot: 2.0 及以上 Maven: 3.0 及以上 二.新建Spring Boot 项目 通过功能菜单File - New - Project 新建Spring

C++ ADO方式连接mysql数据库

对于软件开发其实说白了就是在不停地和数据打交道, 所以数据库的操作是必不可少的, 接下来介绍VC开发中利用ADO建立ODBC数据源来访问MySQL数据库. 从我接触的数据库编程方式来说, 我觉得在vc开发连接数据库是比较难的, 也是很容易出错. 在android中, 系统自带sqlite数据库,只需要使用SQLiteOpenHelper抽象类即可完成与数据库的操作. 在java中, 使用jdbc连接mysql数据库, 下载相应jar调用相应接口,传入数据库类型与用户名密码进行数据库的操作. 但是

Ubuntu jsp平台使用JDBC来连接MySQL数据库

Ubuntu 7.04 搭建Ubuntu jsp平台开发环境MySQL+tomcat+apache+j2sdk1.6在所有安装开始前先在Terminal中输入 rpm -q -a查看是否安装过rpm 和 rpm包的所需软件如果没有安装rpm在Terminal中输入 sudo apt-get install rpm. AD:2014WOT全球软件技术峰会北京站 课程视频发布 你们知道什么是Ubuntu jsp平台么这个非常高深的运用技术将由我来非常讲解,Ubuntu jsp平台NB在哪呢,下面我来

【FunnyBear的Java之旅 - Spring篇】7步连接MySQL

准备工作: a) 启动MySQL服务器, 使用MySQL Workbench新建数据库 b) 下载Eclipse EE, 并安装Maven和Spring Framework插件 c) 由于默认的Maven服务器可能被墙,所以需要为Maven配置mirror server,此处不展开解释 最终的项目结构将如图所示,具体每个文件的作用将在下文中逐步介绍 第一步:在Eclipse中新建Maven project. 主要是为了方便jar的导入和项目结构的管理 第二步:导入相关jar包.修改Maven p

Spring Boot项目搭建(Spring Boot 2.2.4 + MyBatis + MySql)

创建Spring Boot项目 1.New Project 2)取名为sb2 (springboot2.x) 3.勾选Spring Web 4.勾选MyBatis framework 5.下一步,然后点击完成 6.pom.xml增加jar和插件 jar dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1

spark连接jdbc,连接mysql

1 最直接的方式 scala> val jdbcDF = spark.read.format("jdbc") .option("url", "jdbc:mysql://hadoop1:3306/rdd")-------mysql 接口和库名 .option("dbtable", "rddtable")-----两张表名 .option("user", "root"

Spring Boot使用配置文件方式整合MyBatis

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4