springboot JDBC整合

1、建立一个springboot项目,并导包:JDBC API 与 MySQL Driver

springboot  JDBC整合

2、项目建好之后,发现自动帮我们导入了如下的启动器:

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <scope>runtimescope>
dependency>

3、编写yaml配置文件连接数据库;也可以使用properties配置文件

spring:
  datasource:
    username: mysql账号
    password: 密码
    #配置时区  serverTimezone=UTC&
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver

4、测试,查看是否成功

控制台打印出:class com.alibaba.druid.pool.DruidDataSource 成功

@SpringBootTest
class SpringbootDataJdbcApplicationTests {

    //注入数据源
    @Autowired
    DataSource dataSource;

    @Test
    public void contextLoads() throws SQLException {
        //默认数据源
        System.out.println(dataSource.getClass());
        //获得连接
        Connection connection =   dataSource.getConnection();
        System.out.println(connection);
        //关闭连接
        connection.close();
    }
}

扩展

JDBCTemplate

JdbcTemplate主要提供以下几类方法:

  • execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;
  • update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;
  • query方法及queryForXXX方法:用于执行查询相关语句;
  • call方法:用于执行存储过程、函数相关语句。
package com.company.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class TestController {

    @Autowired
    JdbcTemplate jdbcTemplate;

//    查询全部信息
//    没有实体类,使用万能的  Map
    @GetMapping("/user")
    public List> userAll(){

        String sql="select * from user";
        List> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }

    @RequestMapping("/update/{id}")
    public String update(@PathVariable("id") int id){
        //两种写法
        //String sql="update user set name=?,pwd=? where id="+id;  则不需要    o[2]=id;

        String sql="update user set name=?,pwd=? where id=?";
        Object[] o=new Object[3];
        o[0]="kkkkl";
        o[1]="12341131";
        o[2]=id;
        jdbcTemplate.update(sql,o);
        return "update-Ok";

    }

    @RequestMapping("/insert")
    public String insert(){
        String sql="insert into user(id,name,pwd) value(6,'ppppp','111111')";
        jdbcTemplate.update(sql);
        return "insert-OK";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id){
        String sql="delete from user where id="+id;
        int update = jdbcTemplate.update(sql);
        return "delete-Ok";
    }

    //测试项目是否搭建成功
    @RequestMapping("/u")
    public String all(Model model){
        return "jjjjj";
    }

}

Original: https://www.cnblogs.com/wdsjg/p/13599360.html
Author: 所有的都不在以往
Title: springboot JDBC整合

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/612297/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • TortoiseGit使用

    ​ 一:TortoiseGit安装以及配置 1.1、安装包 TortoiseGit以及配套安装包见链接: git下载链接 Download PuTTY TortoiseGit下载 …

    Linux 2023年6月13日
    085
  • Docker(47)- 一键安装 docker 的 shell 脚本

    背景 有多个自己的云服务器,都要安装 docker,所以就尝试将安装的步骤转换成 shell 脚本,回头可以一键执行~ 创建 shell 脚本 touch install_dock…

    Linux 2023年5月28日
    092
  • Java — 枚举

    枚举是 JDK5 中引入的特性,由 enum 关键字来定义一个枚举类。 格式: enum 枚举类名 { 枚举项1, 枚举项2, …; 成员变量; 构造方法 成员方法 } 说明:…

    Linux 2023年6月8日
    098
  • 函数指针的重要用途——回调函数

    什么是回调函数? 粗暴的说,如果一个函数作为另一个函数的参数传入,这种函数就可以称为回调函数(这句话并不严谨,但为了说明问题可以这么理解)。C语言里面,一般就是一个函数的参数列表中…

    Linux 2023年6月8日
    0103
  • 记一次 android 线上 oom 问题

    背景 公司的主打产品是一款跨平台的 App,我的部门负责为它提供底层的 sdk 用于数据传输,我负责的是 Adnroid 端的 sdk 开发。 sdk 并不直接加载在 App 主进…

    Linux 2023年6月6日
    0114
  • jenkins自动触发构建

    bash;gutter:true; 1. 安装jenkins cat /etc/yum.repos.d/jenkins.repo [jenkins] name=Jenkins ba…

    Linux 2023年6月7日
    080
  • [20220909]bbed关于删除记录恢复的问题.txt

    [20220909]bbed关于删除记录恢复的问题.txt –//快下班被别人问的关于删除记录使用bbed恢复的问题,我开始以为很快讲解完,删除记录oracle仅仅打上…

    Linux 2023年6月13日
    083
  • 编写一个简单的linux kernel rootkit

    一、前言 linux kernel rootkit跟普通的应用层rootkit个人感觉不大,个人感觉区别在于一个运行在用户空间中,一个运行在内核空间中;另一个则是编写时调用的API…

    Linux 2023年6月8日
    0116
  • linux命令之wget下载

    wget wget 是一个下载文件的工具。 格式 wget [&#x53C2;&#x6570;] [URL&#x5730;&#x5740;] 常用参…

    Linux 2023年5月27日
    089
  • C++ NFS挂载

    挂载NFS 挂载NFS时,常用的命令比如: #将远程目录挂载到本地/home/share目录下 mount -t nfs -o nolock 192.168.1.10:/tmp /…

    Linux 2023年6月8日
    082
  • 智能指针

    RAII(Resource Acquisition Is Initialization)是一种利用对象生命周期来控制程序资源(如内 存、文件句柄、网络连接、互斥量等等)的简单技术。…

    Linux 2023年6月13日
    085
  • 5.9 Linux Vim批量注释和自定义注释

    使用 Vim 编辑 Shell 脚本,在进行调试时,需要进行多行的注释,每次都要先切换到输入模式,在行首输入注释符 #再退回命令模式,非常麻烦。 连续行的注释其实可以用替换命令来完…

    Linux 2023年6月7日
    094
  • Redis-实现SpringBoot集成Redis多数据源

    背景​ 有些时候在一个项目里,由于业务问题,可能仅仅操作一个Redis数据源已经不能满足,比如某个运营系统,对接着多个不同的服务,处理数据时又不想通过远程调用,那只能增加一个数据源…

    Linux 2023年5月28日
    086
  • 介绍一种使用poweshell 监控的方式

    本篇将使用PowerShell脚本对SQL Server 的关键指标进行监控. 以下,用数据库空间容量为例,实现一下逻辑: 一、建表 为每台服务器创建一个表,用于记录服务器各个数据…

    Linux 2023年5月28日
    0104
  • [20220303]oracle如何定位使用library cache mutex 3.txt

    [20220303]oracle如何定位使用library cache mutex 3.txt –//这个问题实际上困扰我很久,我开始以为library cache b…

    Linux 2023年6月13日
    082
  • python学习

    python中的字符串以双引号或者单引号表示 长度为L:第一个字节索引为0或-L 最后一个字节索引为L-1或-1 in是二元关系操作,用来判断左侧内容是否在右侧的集合中 float…

    Linux 2023年6月6日
    0105
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球