springboot教程二:Accessing data with MySQL

本系列教程使用idea完成
idea安装springboot插件


image.png

需求:
jdk1.8及以上
maven3.2+

第一步
新建项目,输入项目名gs-accessing-data-mysql


image.png

第二步
勾选Spring Web, Spring Data JPA, and MySQL Driver 点击完成


image.png

image.png

第三步
创建数据库

mysql> create database db_example; --创建数据库
mysql> create user 'springuser'@'%' identified by 'ThePassword'; -- 创建用户
mysql> grant all on db_example.* to 'springuser'@'%'; -- 给数据库授权

第四步
在如下文件加入数据库连接配置
src/main/resources/application.properties

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword

第五步
创建实体类 User.java文件
src/main/java/com/example/accessingdatamysql/User.java

package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Integer id;

  private String name;

  private String email;

  public Integer getId() {
    return id;
  }

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

  public String getName() {
    return name;
  }

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

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }
}

第六步
创建数据创库UserRepository

package com.example.accessingdatamysql;

import org.springframework.data.repository.CrudRepository;

import com.example.accessingdatamysql.User;

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete

public interface UserRepository extends CrudRepository {

}

第七步
创建控制器
src/main/java/com/example/accessingdatamysql/MainController.java

package com.example.accessingdatamysql;

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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
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;

  @PostMapping(path="/add") // Map ONLY POST Requests
  public @ResponseBody String addNewUser (@RequestParam String name
      , @RequestParam String email) {
    // @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.setEmail(email);
    userRepository.save(n);
    return "Saved";
  }

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

第八步
使用post访问localhost:8080/demo/add

$ curl localhost:8080/demo/add -d name=First -d [email protected]

或者使用postman模拟post访问
程序返回Saved

使用浏览器访问localhost:8080/demo/all 或者使用

$ curl 'localhost:8080/demo/all'

返回
[{"id":1,"name":"First","email":"[email protected]"}]

你可能感兴趣的:(springboot教程二:Accessing data with MySQL)