最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

教你用springboot連接mysql并實(shí)現(xiàn)增刪改查

 更新時(shí)間:2021年05月18日 14:53:23   作者:雪巖ding  
今天教各位小伙伴用springboot連接mysql并實(shí)現(xiàn)增刪改查功能,文中有非常詳細(xì)的步驟及代碼示例,對(duì)正在學(xué)習(xí)Java的小伙伴們有非常好的幫助,需要的朋友可以參考下

1.數(shù)據(jù)庫(kù)與數(shù)據(jù)表的創(chuàng)建

創(chuàng)建名為mybatis的數(shù)據(jù)庫(kù):

create database mybatis;

創(chuàng)建名為user2的數(shù)據(jù)表:

use mybatis;
create table user2(
id integer not null primary key,
name varchar(20) not null,
pwd varchar(10) not null,
perms varchar(100) null)

生成如下表結(jié)構(gòu):

在這里插入圖片描述

(已經(jīng)插入了兩行數(shù)據(jù)的)

2.數(shù)據(jù)庫(kù)的連接

注意點(diǎn):url要設(shè)置serverTimezone

比如:jdbc:mysql://localhost:3306?serverTimezone=UTC

連接成功后可以在idea中簡(jiǎn)單測(cè)試一下:

在這里插入圖片描述

3.結(jié)構(gòu):

在這里插入圖片描述

4.JDBCController.java

package com.kuang.controller;

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

import java.util.Map;
import java.util.List;
import java.lang.*;
import java.util.Scanner;


@RestController
public class JDBCController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    //查詢(xún)數(shù)據(jù)庫(kù)的所有信息
    //沒(méi)有實(shí)體類(lèi),數(shù)據(jù)庫(kù)里的東西,怎么獲?。?Map

    //查
    @GetMapping("/userList")
    public List<Map<String,Object> > userList(){
        String sql="select * from mybatis.user2";
        List<Map<String,Object>> list_maps=jdbcTemplate.queryForList(sql);
        return list_maps;
    }


    //增
    @GetMapping("/addUser")
    public String addUser(){
        String sql="insert into mybatis.user2(id,name,pwd,perms) values(3,'xiaoming','123456','NULL')";
        jdbcTemplate.update(sql);
        return "add-ok";
    }

    //改
    @GetMapping("updateUser/{id}/{name}/{pwd}")
    public String updateUser(@PathVariable("id") int id,@PathVariable("name") String name,@PathVariable("pwd") String pwd){
        String sql="update mybatis.user2 set  name=?,pwd=?  where  id="+id;

        //封裝Object
        Object[] objects = new Object[2];
        objects[0]=name;
        objects[1]=pwd;

        jdbcTemplate.update(sql,objects);
        return "update-ok";
    }

    //刪
    @GetMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id") int id){
        String sql="delete from mybatis.user2 where id=?";
        jdbcTemplate.update(sql,id);
        return "delete-ok";
    }
}

5.application.yml

spring:
  datasource:
    username: root
    password: liding
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver

6.Springboot04DataApplication.java

package com.kuang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Springboot04DataApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot04DataApplication.class, args);
    }

}

7.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.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.kuang</groupId>
    <artifactId>springboot-04-data</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-04-data</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- JDBC-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- MySQL-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.5</version>
            </plugin>
        </plugins>
    </build>
</project>

8.pom.xml文件中注意事項(xiàng)

1)<plugin> </plugin>之間的語(yǔ)句爆紅

加上與parent中相同的version號(hào)即可;

2)建立項(xiàng)目時(shí)勾選以下模塊
spring web
JDBC API
mysql Driver

9.查詢(xún)user

啟動(dòng)項(xiàng)目
輸入:localhost:8080/userList

在這里插入圖片描述
在這里插入圖片描述

10.修改user

輸入:localhost:8080/updateUser/5/hahahaha/1455

(說(shuō)明:修改id為5的user,名字改為hahahaha,密碼改為1455)

在這里插入圖片描述

修改后的數(shù)據(jù)表:

在這里插入圖片描述

11.增加user

輸入:localhost:8080/addUser

(這里先寫(xiě)一個(gè)簡(jiǎn)單靜態(tài)的addUser吧,寫(xiě)了半天類(lèi)似與updateUser的一直報(bào)錯(cuò))

在這里插入圖片描述
修改后的數(shù)據(jù)表:
在這里插入圖片描述

12.刪除user

輸入:localhost:8080/deleteUser/3

(刪除id為3的user)

在這里插入圖片描述

修改后的數(shù)據(jù)表:

在這里插入圖片描述

到此這篇關(guān)于教你用springboot連接mysql并實(shí)現(xiàn)增刪改查的文章就介紹到這了,更多相關(guān)springboot連接mysql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Action訪(fǎng)問(wèn)Servlet的API的簡(jiǎn)單實(shí)例

    Action訪(fǎng)問(wèn)Servlet的API的簡(jiǎn)單實(shí)例

    下面小編就為大家?guī)?lái)一篇Action訪(fǎng)問(wèn)Servlet的API的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06
  • 探究Android系統(tǒng)中解析JSON數(shù)據(jù)的方式

    探究Android系統(tǒng)中解析JSON數(shù)據(jù)的方式

    這篇文章主要介紹了探究Android系統(tǒng)中解析JSON數(shù)據(jù)的方式,文中講到了使用Java代碼實(shí)現(xiàn)的處理JSON的一些主要方法,需要的朋友可以參考下
    2015-07-07
  • Go Java 算法之迷你語(yǔ)法分析器示例詳解

    Go Java 算法之迷你語(yǔ)法分析器示例詳解

    這篇文章主要為大家介紹了Go Java 算法之迷你語(yǔ)法分析器示例詳解,
    有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Java基本類(lèi)型與byte數(shù)組之間相互轉(zhuǎn)換方法

    Java基本類(lèi)型與byte數(shù)組之間相互轉(zhuǎn)換方法

    下面小編就為大家?guī)?lái)一篇Java基本類(lèi)型與byte數(shù)組之間相互轉(zhuǎn)換方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-08-08
  • Java中的Comparable和Comparator接口

    Java中的Comparable和Comparator接口

    這篇文章主要介紹了Java中的Comparable和Comparator接口,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • java中的instanceof關(guān)鍵字詳細(xì)解讀

    java中的instanceof關(guān)鍵字詳細(xì)解讀

    這篇文章主要介紹了java中的instanceof關(guān)鍵字詳細(xì)解讀,instanceof 是 Java 的保留關(guān)鍵字,它的作用是測(cè)試它左邊的對(duì)象是否是它右邊的類(lèi)的實(shí)例,返回 boolean 的數(shù)據(jù)類(lèi)型,需要的朋友可以參考下
    2024-01-01
  • Web容器啟動(dòng)過(guò)程中如何執(zhí)行Java類(lèi)

    Web容器啟動(dòng)過(guò)程中如何執(zhí)行Java類(lèi)

    這篇文章主要介紹了Web容器啟動(dòng)過(guò)程中如何執(zhí)行Java類(lèi),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Socket與ServerSocket類(lèi)構(gòu)造方法與API

    Socket與ServerSocket類(lèi)構(gòu)造方法與API

    今天小編為大家整理了Socket與ServerSocket類(lèi)構(gòu)造方法與API,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值。需要的朋友可以收藏下,方便下次瀏覽觀看
    2021-12-12
  • 使用aop實(shí)現(xiàn)全局異常處理

    使用aop實(shí)現(xiàn)全局異常處理

    這篇文章主要為大家詳細(xì)介紹了使用aop實(shí)現(xiàn)全局異常處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Java完美實(shí)現(xiàn)2048小游戲

    Java完美實(shí)現(xiàn)2048小游戲

    本文給大家分享的是一則根據(jù)網(wǎng)友的代碼改編的2048小游戲的源碼,個(gè)人認(rèn)為已經(jīng)非常完美了,推薦給大家,有需要的小伙伴可以參考下。
    2015-03-03

最新評(píng)論

惠水县| 广饶县| 天柱县| 柯坪县| 灵台县| 莱西市| 四子王旗| 武安市| 澄迈县| 海丰县| 大田县| 商水县| 金秀| 阜南县| 临夏县| 田东县| 西盟| 万荣县| 康平县| 龙岩市| 镇平县| 崇礼县| 汝城县| 仙桃市| 宾川县| 河北省| 新乐市| 正安县| 天等县| 新兴县| 筠连县| 红安县| 梨树县| 柯坪县| 洛隆县| 墨竹工卡县| 清水河县| 舒城县| 林周县| 富阳市| 凤阳县|