SpringBoot连接mysql数据,写入数据

x33g5p2x  于2022-02-07 转载在 Spring  
字(3.9k)|赞(0)|评价(0)|浏览(580)

(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:

  1. create database db_example;

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

  1. show databases;

mysql控制台输出结果:

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | db_example |
  6. | information_schema |
  7. | mysql |
  8. | performance_schema |
  9. | sys |
  10. +--------------------+
  11. 5 rows in set (0.00 sec)

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

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

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

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

  1. Query OK, 0 rows affected (0.26 sec)

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

  1. SELECT user FROM mysql.user;

mysql控制台输出:

  1. mysql> SELECT user FROM mysql.user;
  2. +------------------+
  3. | user |
  4. +------------------+
  5. | springuser |
  6. | mysql.infoschema |
  7. | mysql.session |
  8. | mysql.sys |
  9. | root |
  10. +------------------+
  11. 5 rows in set (0.00 sec)

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

授权springuser所有权限:

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

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

上层Java代码:

  1. package com.example.spring_mysql;
  2. import javax.persistence.Entity;
  3. import javax.persistence.GeneratedValue;
  4. import javax.persistence.GenerationType;
  5. import javax.persistence.Id;
  6. @Entity
  7. public class User {
  8. @Id
  9. @GeneratedValue(strategy = GenerationType.AUTO)
  10. private Integer id;
  11. private String name;
  12. private String addr;
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16. public Integer getId() {
  17. return id;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public String getName() {
  23. return name;
  24. }
  25. public void setAddr(String addr) {
  26. this.addr = addr;
  27. }
  28. public String getAddr() {
  29. return addr;
  30. }
  31. }

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

访问的数据接口类:

  1. package com.example.spring_mysql;
  2. import org.springframework.data.repository.CrudRepository;
  3. public interface UserRepository extends CrudRepository<User, Integer> {
  4. }

控制器:

  1. package com.example.spring_mysql;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. @Controller
  9. @RequestMapping(path = "/demo")
  10. public class MainController {
  11. @Autowired // This means to get the bean called userRepository
  12. // Which is auto-generated by Spring, we will use it to handle the data
  13. private UserRepository userRepository;
  14. @GetMapping(path = "/add") // Map ONLY POST Requests
  15. public @ResponseBody
  16. String addNewUser(@RequestParam(value = "name", defaultValue = "null") String name,
  17. @RequestParam(value = "addr", defaultValue = "null") String addr) {
  18. // @ResponseBody means the returned String is the response, not a view name
  19. // @RequestParam means it is a parameter from the GET or POST request
  20. User n = new User();
  21. n.setName(name);
  22. n.setAddr(addr);
  23. userRepository.save(n);
  24. return "已保存";
  25. }
  26. @GetMapping(path = "/all")
  27. public @ResponseBody
  28. Iterable<User> getAllUsers() {
  29. // This returns a JSON or XML with the users
  30. return userRepository.findAll();
  31. }
  32. }

应用:

  1. package com.example.spring_mysql;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class SpringMysqlApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(SpringMysqlApplication.class, args);
  8. }
  9. }

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

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

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

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

然后输入连接地址:

http://localhost:8080/demo/all

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

相关文章