gpt4 book ai didi

SpringBoot连接mysql数据,写入数据

转载 作者:知者 更新时间:2024-03-13 08:50:18 34 4
gpt4 key购买 nike

(1)先准备好mysql数据,作为springboot的数据存储服务器。

安装和启动mysql服务器的介绍:https://zhangphil.blog.csdn.net/article/details/122414377

https://zhangphil.blog.csdn.net/article/details/122414377

上面只是搭建和启动了mysql数据库,需要为springboot创建一个在mysql里面的用户,假设该用户名为springuser,密码是123456,登录mysql,执行mysql命令完成。

先创建一个专有数据库db_example:

create database db_example;

在mysql里面创建一个名为db_example的数据库。执行create命令后,查看db_example创建时候成功:

show databases;

mysql控制台输出结果:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db_example         |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

输出表明db_example创建成功。开始在mysql里面创建springuser用户。执行mysql命令:

CREATE USER springuser@'%' IDENTIFIED BY '123456';

@后面的'%'通配符,表示允许来自所有位置的主机均可连接访问springuser,'123456'即为springuser的密码。

执行成功后,mysql控制台会输出:

Query OK, 0 rows affected (0.26 sec)

再次确认是否在mysql创建成功springuser用户,需要在mysql里面执行命令,查询当前mysql数据的所有用户,在mysql里面执行命令:

SELECT user FROM mysql.user;

mysql控制台输出:

mysql> SELECT user FROM mysql.user;
+------------------+
| user             |
+------------------+
| springuser       |
| mysql.infoschema |
| mysql.session    |
| mysql.sys        |
| root             |
+------------------+
5 rows in set (0.00 sec)

里面已经有springuser这个用户,表明创建成功。

授权springuser所有权限:

grant all on db_example.* to 'springuser'@'%';

(2)开始springboot连接mysql并写入数据。

上层Java代码:

package com.example.spring_mysql;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String name;
    private String addr;

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public String getAddr() {
        return addr;
    }
}

User即为往数据库存储的表user。

访问的数据接口类:

package com.example.spring_mysql;

import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<User, Integer> {

}

控制器:

package com.example.spring_mysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(path = "/demo")
public class MainController {
    @Autowired // This means to get the bean called userRepository
    // Which is auto-generated by Spring, we will use it to handle the data
    private UserRepository userRepository;

    @GetMapping(path = "/add") // Map ONLY POST Requests
    public @ResponseBody
    String addNewUser(@RequestParam(value = "name", defaultValue = "null") String name,
                      @RequestParam(value = "addr", defaultValue = "null") String addr) {
        // @ResponseBody means the returned String is the response, not a view name
        // @RequestParam means it is a parameter from the GET or POST request
        User n = new User();
        n.setName(name);
        n.setAddr(addr);
        userRepository.save(n);
        return "已保存";
    }

    @GetMapping(path = "/all")
    public @ResponseBody
    Iterable<User> getAllUsers() {
        // This returns a JSON or XML with the users
        return userRepository.findAll();
    }
}

应用:

package com.example.spring_mysql;

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

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

最后,需要配置后端数据连接访问的源,在application.properties里面配置:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql:true

(3)以上均完成后,打开浏览器,输入:http://localhost:8080/demo/add?name=zhangphil&addr=chengdu

即可把zhangphil和chengdu存入到mysql里面。

然后输入连接地址:

http://localhost:8080/demo/all

即可把后端mysql里面的全部数据读取展现在浏览器里面。

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com