chenfeng11 / TDD

0 stars 0 forks source link

spring boot 初学 #5

Open chenfeng11 opened 3 years ago

chenfeng11 commented 3 years ago

作为一个初学者学习springboot我们选择使用ide image 勾选spring initializr image 选择java8作为开发环境 image springboot我用的是最新的2.4.3 然后勾选上springweb Thymeleaf spring data jpa Mysql Driver image 名字就叫MyJava image 首先我们会看到一个main函数的class这就是他的程序入口 SpringBootApplication是一个很重要的注解,此地因为我们不使用数据库所以xclude = DataSourceAutoConfiguration.class 这个注解会自动扫描当前目录下以及其所有子文件夹的所有内容面的所有的@RequestMapping请求地址 image 我们将这个部分贴出来

package com.example.demo;

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

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class MyFirstJavaApplication {

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

}

然后新建一个实体类 image

package entity;

public class User {
    private  Integer id;
    private  String userName;
    private  String password;

    public Integer getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

然后在controller

package com.example.demo.controller;

import entity.User;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/login")
public class Controller {

    @GetMapping("/select")
    public User getLogin(@Param("userName") String userName) {

        User user = new User();
        user.setId(15);
        user.setUserName("邓兄");
        user.setPassword("zs123456");
        return user;

    }
}

运行起来就是这样 image 然后访问http://localhost:8080/login/select?userName=??? 就能拿到接口返回的数据了

KkevinK commented 3 years ago

强啊!

djk3000 commented 3 years ago

强啊!