SpringBoot的开发(2)--Entity层的开发、Dao层的开发、Service层的开发、Controller层的开发...

Entity层的开发

在刚刚创建的Entity包中,新建一个Student类(类名和创建的表名一致,除了大小写)

import javax.persistence.*;

//通过注解,标注这里是Entity
@Entity
//通过注解,将MySQL数据库中创建的student表映射成这里定义的Student类
@Table(name = "student")
public class Student {
    //通过注解指定主键--id
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) // 描述id自增属性

    //这里定义私有的成员变量与MySQL数据库中创建的student表中的字段一一对应

    //这里int类型用Tnteger类型代替
    private Integer id;
    private String name;
    private Integer age;
    private String gender;
    private String clazz;
    // 属性名一般同数据库中表的列名保持一致,不一致时可以使用@Column注解
    @Column(name = "sum_score")
    private Integer sumScore;


    //因为我们定义的是私有的成员变量
    //所以要加上构造方法和get、set方法来传参
    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 Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public Integer getSumScore() {
        return sumScore;
    }

    public void setSumScore(Integer sumScore) {
        this.sumScore = sumScore;
    }
}

Dao层的开发

在刚刚创建的Dao包中,新建一个StudentRepository接口(接口名:上面定义的类名+Repository)

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import springboot.Entity.Student;

//这里需要继承JpaRepository接口,并且指定泛型<刚刚Entity中创建的类名,及其主键类型>
//并且加上Repository注解
@Repository
public interface StudentRepository extends JpaRepository<Student,Integer> {
    //常见的增删改查功能都是继承自JpaRepository接口,所以不需要我们自己定义
}

Service层的开发

在之前创建的Service包中,新建一个StudentService类(类名:Entity层中定义的类名+Service)

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import springboot.Dao.StudentRepository;
import springboot.Entity.Student;

import javax.annotation.Resource;
import java.util.List;

//通过注解,标注这是Service层
@Service
public class StudentService {
    //因为在Service层中我们需要调用Dao层
    //所以将Dao层作为一个资源将其加进来
    @Resource
    private StudentRepository studentRepository;

    //现在我们来做一下需求分析
    //可能我们需要找学生表中所以学生的信息
    //那么定义的findAll方法返回值应该是一个集合
    public List<Student> findAll() {
        //这里可以直接把studentRepository拉过来用
        //因为Dao层中StudentRepository有继承过来的功能
        return studentRepository.findAll();
    }

}

Controller层的开发

基于服务器向用户返回的(状态码+消息+数据)我们可以在Java中定义一个类来描述,这样服务器只需要返回一个该类的对象给用户即可

所以我们新建一个common包(用于存放通用的东西),在其中定义一个Result类来描述服务器向用户返回的结果

public class Result<T> {
    //状态码
    private String code;
    //消息
    private String msg;
    //数据:我们不确定数据的类型,所以用泛型
    private T data;

    public Result() {
    }

    public Result(T data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    // 请求成功 不返回数据
    public static <T> Result<T> success() {
        Result rs = new Result<>();
        rs.setCode("200");
        rs.setMsg("ok");
        return rs;
    }

    // 请求成功 返回数据
    public static <T> Result<T> success(T data) {
        Result<T> rs = new Result<T>(data);
        rs.setCode("200");
        rs.setMsg("ok");
        return rs;
    }

    // 请求失败 -- 因为失败的原因不确定,所以不能指定
    public static <T> Result<T> error(String code, String msg) {
        //失败了就不需要返回数据给用户
        Result rs = new Result<>();
        rs.setCode(code);
        rs.setMsg(msg);
        return rs;
    }
}

在之前创建的Controller包中,新建一个StudentController类(类名:Entity层中定义的类名+Controller)

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springboot.Entity.Student;
import springboot.Service.StudentService;
import springboot.common.Result;

import javax.annotation.Resource;
import java.util.List;

//通过RestController、RequestMapping注解来标注控制层

@RestController//表示一种规范,因为现在都是前后端分离,中间通过接口(API)来连接,API返回的是json格式的数据
@RequestMapping("/stu")//将url中的路径(该路径就是括号中的/stu)与下面的Controller,形成映射关系
public class StudentController {
    //在我们对Student表进行操作的时候,就比如说查找所有--findAll方法
    //而findAll方法是在Service层中,所以StudentService也需要作为资源传进来

    @Resource
    private StudentService studentService;


    //一般我们向服务器发起请求都是通过get方式
    //那么这边我们也用GetMapping
    @GetMapping("/all")//这个 ("/all") 可不加,因为我这边默认向服务器发起请求的方式是get(获取所有消息)
    public Result<List<Student>> findAll(){
        List<Student> list = studentService.findAll();
        return Result.success(list);
    }

}

当我们写完Controller层之后就可以运行SpringBoot的项目,在浏览器中通过 localhost:8080/stu/all 看一下

这里因为MySQL表中没有数据,所以没有返回数据,返回的格式是json格式

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值