分享

springboot整合jdbc

 若生安饶 2023-09-03 发布于河南

application.yml配置

  datasource:

    url: jdbc:mysql://localhost:3306/mydb?useSSL=false

    username: root

    password: root

    driver-class-name: com.mysql.jdbc.Driver


pom.xml 依赖配置

  <!--springboot整合jdbc框架-->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-jdbc</artifactId>

        </dependency>

        <!--springboot整合mysql驱动类-->

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>8.0.26</version>

        </dependency>


jdbc service类

package com.mydemo.service;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class UserService {

    @Autowired

    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/insertUser")

    public String insertUser(String name,Integer age){

        int update = jdbcTemplate.update("INSERT into user(name,age) VALUES(?,?)",name,age);

        if(update>0){

            return "sucess";

        }else{

            return "error";

        }

    }

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多