SpringBoot集成mysql-connector-java
Posted Jack魏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot集成mysql-connector-java相关的知识,希望对你有一定的参考价值。
官方文档:https://dev.mysql.com/doc/connector-j/8.0/en/
接下来我们使用SpringBoot集成一下mysql驱动,操作数据库 :
1. 准备好MySQL
MySQL安装教程:
CentOS7 安装 MySQL8.0
MySQL8.0和5.7安装教程(Windows)
2. 准备数据
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `authors`;
CREATE TABLE `authors` (
`id` int NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`last_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`birthdate` date NOT NULL,
`added` timestamp NOT NULL DEFAULT current_timestamp,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `email`(`email`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 10001 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (1, Valentina, Beer, gwalker@example.net, 2005-02-13, 1981-08-25 05:47:00);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (2, Stewart, Funk, jgerhold@example.com, 1998-12-12, 1986-10-21 11:13:42);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (3, Anne, Turner, cortney82@example.com, 1985-03-05, 1977-01-12 23:58:18);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (4, Skyla, Rowe, hgleichner@example.net, 2004-10-25, 1984-12-25 10:26:02);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (5, Merritt, Bergstrom, modesto96@example.org, 1971-10-23, 1982-06-01 15:29:50);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (6, Maymie, Keeling, wiza.augustine@example.org, 1989-07-17, 1998-09-18 09:55:21);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (7, Myrtis, Senger, gunner.smith@example.net, 1977-02-08, 2004-10-23 01:26:00);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (8, Darrick, Goldner, wkeebler@example.com, 1990-12-17, 1982-03-02 03:59:42);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (9, Rosendo, Prosacco, gratke@example.com, 1989-10-04, 1995-02-20 11:20:35);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (10, Jamal, Hartmann, bartoletti.kaden@example.org, 1984-09-05, 2008-12-31 16:10:04);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (11, Milton, Spencer, laury36@example.com, 1978-05-30, 2011-12-30 08:53:03);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (12, Garfield, Wehner, cecelia95@example.com, 1974-12-23, 1971-08-30 13:19:42);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (13, Marquise, Quigley, abahringer@example.com, 1990-11-04, 2005-07-31 07:37:44);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (14, Bailee, Hodkiewicz, samanta06@example.org, 1996-10-08, 1971-03-29 12:40:43);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (15, Jaylon, Runolfsson, qgrant@example.org, 2019-08-23, 2001-11-04 15:15:09);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (16, Gino, Nicolas, cummerata.meaghan@example.net, 1981-07-03, 2003-04-04 09:13:53);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (17, Evan, Hoeger, shermann@example.com, 1997-10-25, 2003-07-08 17:06:33);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (18, Lillian, Kautzer, qsimonis@example.com, 2006-08-13, 2013-03-15 14:01:22);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (19, Emory, Wilderman, dschaefer@example.net, 1988-08-01, 1997-07-12 11:59:25);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (20, Enoch, Hackett, auer.missouri@example.org, 2010-12-13, 2020-04-22 21:13:08);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (21, Edwin, Kuhic, ubauch@example.net, 1979-02-12, 2009-07-27 10:25:37);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (22, Alena, Marks, isom.metz@example.net, 2021-12-19, 1984-07-31 19:24:14);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (23, Rosa, Denesik, angus.pfeffer@example.net, 1975-11-12, 1980-10-09 20:57:54);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (24, Brooks, Runolfsson, bblanda@example.com, 1993-09-03, 2006-05-05 12:06:02);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (25, Ardella, Howe, geovanny88@example.net, 1991-01-29, 1970-10-17 07:55:52);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (26, Zackery, Kiehn, teagan21@example.net, 2008-11-13, 2006-10-10 18:12:09);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (27, Sidney, Kunde, brown.alejandra@example.com, 2021-05-21, 1992-09-28 01:48:40);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (28, Buford, Eichmann, anna18@example.com, 2015-06-13, 1985-12-28 18:00:20);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (29, Marta, Huels, rebeka03@example.net, 1981-11-23, 1979-01-23 01:57:11);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (30, Delta, Fisher, sidney88@example.net, 1998-11-05, 1984-04-29 20:16:07);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (31, Allison, Greenholt, akerluke@example.com, 1984-07-26, 2006-09-12 06:27:37);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (32, Gabriella, Beatty, julio.blick@example.org, 2011-05-03, 1982-08-15 12:01:30);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (33, Myles, Beahan, smith.alexa@example.org, 1992-12-14, 2004-12-22 08:39:47);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (34, Thad, Cronin, kessler.barney@example.org, 2011-03-09, 1980-03-12 22:51:13);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (35, Vaughn, Bins, bleffler@example.org, 1987-07-23, 1985-05-04 21:25:35);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (36, Lilian, Bahringer, berenice.king@example.org, 1992-05-03, 2017-10-06 05:09:31);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (37, Vivian, Tremblay, blaise12@example.com, 2014-02-08, 1993-07-22 11:45:51);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (38, Tia, Medhurst, ressie.dickens@example.com, 2007-10-13, 2002-08-28 14:53:49);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (39, Warren, Konopelski, lhintz@example.org, 1978-06-12, 2020-05-25 06:15:25);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (40, Janessa, Abernathy, rrunte@example.com, 1985-12-26, 1987-09-27 00:02:44);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (41, Natalie, Schumm, janae.zulauf@example.org, 1971-10-22, 2012-11-04 01:55:50);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (42, Christopher, Swaniawski, julie35@example.net, 1985-12-04, 1995-01-15 13:45:33);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (43, Turner, Ebert, pedro55@example.com, 1975-06-13, 1970-10-19 00:41:04);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (44, Lizeth, Gleichner, kilback.nathaniel@example.com, 1977-02-19, 1995-06-20 20:31:14);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (45, Shakira, Stokes, elyse.nienow@example.org, 1981-12-24, 2008-01-28 16:25:12);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (46, Jewel, Roob, alivia.waters@example.org, 2000-09-21, 1979-02-06 20:33:51);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (47, Imelda, Metz, cortez92@example.net, 1984-05-18, 1977-09-10 19:31:05);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (48, Deanna, Hoeger, kemmer.johanna@example.com, 2004-01-29, 1972-10-12 05:39:50);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (49, Rachelle, Hayes, corkery.nathen@example.org, 2015-08-23, 2009-02-15 01:41:20);
INSERT INTO `test`.`authors`(`id`, `first_name`, `last_name`, `email`, `birthdate`, `added`) VALUES (50, Dolly, Hand, christopher.conroy@example.net, 1999-01-26, 2014-11-04 05:52:57);
3. SpringBoot代码
源码地址:
https://gitee.com/jack0240/spring-boot-demo.git
3.1 配置Maven
<!--MySql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
3.2 表的实体类
/*
* Copyright © Jack魏 2022 - 2022 , All Rights Reserved.
*/
package com.jack.demo.bean;
import lombok.Data;
import java.util.Date;
/**
* 作者实体类
*
* @author Jack魏
* @since 2022-08-21
*/
@Data
public class Authors
private Integer id;
private String firstName;
private String lastName;
private String email;
private Date birthdate;
private Date added;
3.3 测试代码
/*
* Copyright © Jack魏 2022 - 2022 , All Rights Reserved.
*/
package com.jack.demo.db;
import com.jack.demo.bean.Authors;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* MySQL数据库操作测试
*
* @author Jack魏
* @since 2022-08-21
*/
public class JdbcTest
/**
* 连接地址
*/
public static final String URL = "jdbc:mysql://127.0.0.1:3306/test";
/**
* 用户名
*/
public static final String USER = "root";
/**
* 密码
*/
public static final String PASSWORD = "123456";
/**
* 连接
*/
private static Connection conn = null;
static
try
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2. 获得数据库连接
conn = DriverManager.getConnection(URL, USER, PASSWORD);
catch (SQLException | ClassNotFoundException e)
e.printStackTrace();
/**
* 维护常量连接,不用每次都创建
*/
public static Connection getConnection()
return conn;
/**
* 测试查询
*
* @throws SQLException
*/
@Test
void testQuery() throws SQLException
Connection conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM authors limit 10");
List<Authors> authorsList = new ArrayList<Authors>();
Authors author = null;
while (rs.next())
author = new Authors();
author.setId(rs.getInt("id"));
author.setFirstName(rs.getString("first_name"));
author.setLastName(rs.getString("last_name"));
author.setEmail(rs.getString("email"));
author.setBirthdate(rs.getDate("birthdate"));
author.setAdded(rs.getDate("added"));
System.out.println(author);
authorsList.add(author);
Assertions.assertNotNull(author, "expected not null");
以上是关于SpringBoot集成mysql-connector-java的主要内容,如果未能解决你的问题,请参考以下文章