SptingBoot——绑定MySQL数据库

一、创建数据库test,创建一个表user,表中含user_id user_name age三项数据

在这里插入图片描述

二、创建SpringBoot项目,参考SpringBoot——简单项目的创建

三、配置pom.xml文件

        <!--引入jdbc支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!--引入MySQL连接依赖包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

四、src/main/resources/application.properties中配置数据源信息

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=**********
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

五、创建UserService接口


public interface UserService {
    
    
    void create(String name, Integer age); //创建数据项
    void deleteByName(String name); //根据名称删除数据
    Integer getAllUsers(); //获取用户数目
    void deleteAllUsers();	//删除所有用户
}

六、实现UserService接口

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    
    
    @Autowired
    private JdbcTemplate jdbcTemplate; //JDBC对象用于对数据库的操作

    @Override
    public void create(String name, Integer age) {
    
    
        jdbcTemplate.update("insert into USER(NAME, AGE ) values(?, ?)", name, age);
    }

    @Override
    public void deleteByName(String name) {
    
    
        jdbcTemplate.update("delete from USER where NAME = ?", name);
    }

    @Override
    public Integer getAllUsers() {
    
    
        return jdbcTemplate.queryForObject("select count(1) from USER", Integer.class);
    }

    @Override
    public void deleteAllUsers() {
    
    
        jdbcTemplate.update("delete from USER");
    }
}

在JdbcTemplate中执行SQL语句的方法大致分为3类:
execute:可以执行所有SQL语句,因为没有返回值,一般用于执行DDL语句。
update:用于执行INSERT、UPDATE、DELETE等DML语句。
queryXxx:用于DQL数据查询语句。

SQL 分类:
SQL 语句主要可以划分为以下 3 个类别。
DDL(Data Definition Languages)语句:数据定义语言,这些语句定义了不同的数据段、数据库、表、列、索引等数据库对象的定义。常用的语句关键字主要包括 create、drop、alter等。
DML(Data Manipulation Language)语句:数据操纵语句,用于添加、删除、更新和查询数据库记录,并检查数据完整性,常用的语句关键字主要包括 insert、delete、udpate 和select 等。(增添改查)
DCL(Data Control Language)语句:数据控制语句,用于控制不同数据段直接的许可和访问级别的语句。这些语句定义了数据库、表、字段、用户的访问权限和安全级别。主要的语句关键字包括 grant、revoke 等。

DDL 语句:
DDL 是数据定义语言的缩写,简单来说,就是对数据库内部的对象进行创建、删除、修改的操作语言。它和 DML 语言的最大区别是 DML 只是对表内部数据的操作,而不涉及到表的定义、结构的修改,更不会涉及到其他对象。DDL 语句更多的被数据库管理员(DBA)所使用,一般的开发人员很少使用。
下面通过一些例子来介绍 MySQL 中常用 DDL 语句的使用方法。

七、UserController类中对数据库数据进行操作


@RestController
class UserController {
    
    

    @Autowired
    private UserService userService;

    @RequestMapping("/demo3")
    public String getUserList() {
    
    

        userService.deleteAllUsers();
        userService.create("John", 15);
        userService.create("Alice", 16);
        userService.create("Tom", 18);
        userService.deleteByName("John");


        return "user num is " + userService.getAllUsers();
    }
}

猜你喜欢

转载自blog.csdn.net/peixin_huang/article/details/109150608
今日推荐