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的更多相关文章
- jmeter中通过jdbc方式连接mysql数据库的配置参考
jmeter中通过jdbc方式连接mysql数据库的配置参考: Database URL=jdbc:mysql://ip:port/dbname?useUnicode=true&allowMu ...
- Spark JDBC方式连接MySQL数据库
Spark JDBC方式连接MySQL数据库 一.JDBC connection properties(属性名称和含义) 二.spark jdbc read MySQL 三.jdbc(url: Str ...
- spring boot下JedisCluster方式连接Redis集群的配置
最近在使用springboot做项目,使用redis做缓存.在外网开发的时候redis服务器没有使用集群配置,所有就是用了RedisTemplate的方式进行连接redis服务器.但是项目代码挪到内网 ...
- 在 Spring Boot 中使用 HikariCP 连接池
上次帮小王解决了如何在 Spring Boot 中使用 JDBC 连接 MySQL 后,我就一直在等,等他问我第三个问题,比如说如何在 Spring Boot 中使用 HikariCP 连接池.但我等 ...
- 【docker】centOS7上部署的mysql和spring boot服务,要求,mysql的时间、java程序服务的时间和宿主机的时间完全保持一致【修改mysql时区,临时和永久】【修改spring boot配置文件时区】【修改docker启动spring boot实例程序时区】
要求:centOS7上部署的mysql和spring boot服务,要求,mysql的时间.java程序服务的时间和宿主机的时间完全保持一致: ============================ ...
- Spring Boot 整合JDBC 实现后端项目开发
一.前言 二.新建Spring Boot 项目 三.Spring Boot 整合JDBC 与MySQL 交互 3.1 新建数据表skr_user 3.2 Jdbcproject 项目结构如下 3.3 ...
- Spring Boot系列(三) Spring Boot 之 JDBC
数据源 类型 javax.sql.DataSource javax.sql.XADataSource org.springframework.jdbc.datasource.embedded,Enbe ...
- Spring boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
- Ubuntu jsp平台使用JDBC来连接MySQL数据库
Ubuntu 7.04 搭建Ubuntu jsp平台开发环境MySQL+tomcat+apache+j2sdk1.6在所有安装开始前先在Terminal中输入 rpm -q -a查看是否安装过rpm ...
随机推荐
- grafna如何用新的dashbord覆盖旧的dashbord
方式一.import一个和之前不一样的名字,然后删除旧的方式二.浏览器json页面复制粘贴,覆盖旧的dashbord 1.记录旧dashbord的var参数,从旧dashbord的json页面复制全部 ...
- go语言-变量与常量
变量 一.变量注意事项 变量名首字母大写,可以被其他包访问调用(公有),变量名首字母小写,其他包不能访问和调用(私有) 在同一个域里一个变量只能定义一次,不可重复定义 二.变量的声明的种方式 1.先声 ...
- LeetCode 269. Alien Dictionary
原题链接在这里:https://leetcode.com/problems/alien-dictionary/ 题目: There is a new alien language which uses ...
- 转,异常好的sql 基础知识整理
转载自:http://blog.csdn.net/u011001084/article/details/51318434 最近从图书馆借了本介绍SQL的书,打算复习一下基本语法,记录一下笔记,整理一下 ...
- 通过反射获取DataSource里数据源的账号密码
public Connection getDbConnection() { Connection conn = null; // 获得连接 DataSource ds = null; try { Co ...
- learning scala view collection
The view method will create a non-strict version of the collection which means that the elements of ...
- exam8.29
咕了好几篇后... 我终于开始重新写了 T1: 不会,没思路,暴搜还可能会(一开始我以为暴搜时间复杂度为$\Theta (mn ^ k)$) 于是码出了暴搜... 跑一遍$(4,4,5)$,然后... ...
- 对拍——>bat
为了凸显对拍滴重要性.就拿来当置顶啦! ——本来是那样想的 ---------------------------------------------------------------------- ...
- Linux find,grep 命令
使用实验楼Linux环境开发,部分内容有所参考,link:https://www.shiyanlou.com/ 概述: find: 在目录中搜索文件,它的使用权限是所有用户 命令格式: find [路 ...
- 自动化运维工具pssh、pdsh、pscp
pssh命令是一个python编写可以在多台服务器上执行命令的工具,同时支持拷贝文件,是同类工具中很出色的,类似pdsh,个人认为相对pdsh更为简便,使用必须在各个服务器上配置好密钥认证访问. 以下 ...