0.前言

利用  SpringBoot ,串接 MySQL 實作API 資料串接 .

1.準備

MAMP – Apache,MySQL Server :  https://www.mamp.info/en/

IntelliJ CE plugin : Spring Assistant

Spring Doc : https://spring.io/guides/gs/accessing-data-mysql/

API Test tool : https://www.getpostman.com/downloads/

phpMyAdmin url :  http://localhost/phpmyadmin (use MAMP Set your mysql port & web port)

 

2.Code

select “Gradle Project”

Dependencies “web”

JPA , use SpringDoc Url

Remember click “Use auto-import”

 

Copy SpringDoc Gradle , Paste to your Gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.4.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'gs-accessing-data-mysql'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")

    // JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'

    // Use MySQL Connector-J
    compile 'mysql:mysql-connector-java'

    testCompile('org.springframework.boot:spring-boot-starter-test')
}

add some code to properties ,

hibernate.ddl-auto  = none (when server started  , if create db will drop , other action google it).

MyServer port is 3306.

Username , Password , need add from mysql.user table .

And Create a Interface extend CrudRepository.

“UserRepository.java” (interface)

public interface UserRepository extends CrudRepository<User,Integer> { }

Create Model “User.java”.

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    int id ;
    String name ;
    String email;

    public User() {
    }

    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int 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;
    }
}

Create Controller “UserController.java”.

if Post Action -> @PostMapping()

if Get Action ->  @GetMapping() , avoid wrong code.

All functions  CrudRepository have . We just need create interface extend CrudRepository.

@RequestMapping("/demo2User")
@RestController
public class UserController {


    @Autowired
    private UserRepository userRepository ;


    @PostMapping("/add")
    @ResponseBody
    public String addUser(
            @RequestParam("name") String name ,
            @RequestParam("email") String email

    ){
        userRepository.save(new User(name, email));

        return "add Done !";
    }

    @GetMapping("/allUsers")
    @ResponseBody
    public Iterable<User> allUsers(){
        return userRepository.findAll();
    }

    @PostMapping("/delete")
    @ResponseBody
    public String deleteUser(
            @RequestBody User user){
        userRepository.delete(user);

        return "Delete Done !";
    }


}

Use PostMan Test API .

Add

Select

Delete , need set Header

Content-Type , application/json

3.Git

https://github.com/encorex32268/blogthing/tree/master/demo2

4.Reference

https://spring.io/guides/gs/accessing-data-mysql/

 

 

最後修改日期: 2019-05-06 11:36:21

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料