博客
关于我
【平台开发】— 5.后端:代码分层
阅读量:450 次
发布时间:2019-03-06

本文共 3015 字,大约阅读时间需要 10 分钟。

数据库准备就绪了,现在可以开始撸后端代码了。为了让前后端能够交互,首先需要后端能够返回数据。因此,我打算先开发一个返回用户信息的接口,然后根据前端传来的用户名和密码来验证用户是否存在,从而实现登录功能(注意:这里仅仅是为了学习目的,并不意味着真正的登录逻辑)。

1. 熟悉后端代码的分层

在开始编写代码之前,先熟悉一下后端的分层结构。后端使用Spring Boot框架,分为四个主要层次:POJO、DAO、Service和Controller。

2. POJO(实体类)

首先是POJO层,用于定义实体类。以用户信息为例,开发一个User类。这个类需要与数据库表对应,字段包括id、username、password等。为了简化代码书写,可以使用Lombok的@Data注解,这样就不需要手动写get/set/toString方法了。

package com.mock.platform.pojo;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;import lombok.Data;import javax.persistence.*;import java.util.Date;@Data@Entity@Table(name = "user")@JsonIgnoreProperties({"handler", "hibernateLazyInitializer"})public class User {    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    @Column(name = "id")    private int id;    private String username;    private String password;    private Date createTime;}

3. DAO(数据访问对象)

DAO层负责与数据库进行交互。创建一个UserDAO接口,继承JpaRepository,这样可以直接使用Spring Data提供的常见CRUD方法和分页功能。

package com.mock.platform.dao;import com.mock.platform.pojo.User;import org.springframework.data.jpa.repository.JpaRepository;public interface UserDAO extends JpaRepository
{}

4. Service(业务逻辑层)

Service层是业务逻辑的核心,实现具体的业务逻辑。以查询用户信息为例,开发一个UserService类。通过Autowired注入UserDAO,调用 findAll方法获取所有用户数据,并使用Sort按id倒序排列。

package com.mock.platform.service;import com.mock平台.dao.UserDAO;import com.mock平台.pojo.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Sort;import org.springframework.stereotype.Service;@Servicepublic class UserService {    @Autowired    private UserDAO userDAO;    public List
userList() { Sort sort = Sort.by(Sort.Direction.DESC, "id"); return userDAO.findAll(sort); }}

5. Controller(控制器)

Controller层负责接收前端请求并返回响应。在UserController类中,使用@GetMapping注解暴露一个接口,返回所有用户信息。通过Autowired注入UserService,调用userList方法获取数据。

package com.mock.platform.controller;import com.mock.platform.pojo.User;import com.mock平台.service.UserService;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.RestController;@RestControllerpublic class UserController {    @Autowired    private UserService userService;    @GetMapping("/users")    public List
userList() throws Exception { return userService.userList(); }}

6. Properties(配置文件)

在properties文件中配置数据库和Spring的相关信息。确保数据库连接正确,并设置JPA的命名策略,去掉下划线。

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/my_platform?characterEncoding=UTF-8&serverTimezone=UTCspring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.jpa.hibernate.ddl-auto = nonespring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImplspring.jpa.show-sql=true

7. 验证

在本地启动服务器后,使用Postman发送GET请求到"/users"端点,查看返回的用户数据是否正确排序。确保数据库中有预先插入的3条用户数据。

通过以上步骤,一个简单的用户信息管理系统已经完成。接下来可以继续扩展功能,比如用户注册、登录验证、权限管理等。

转载地址:http://jzkfz.baihongyu.com/

你可能感兴趣的文章
Notes on Paul Irish's "Things I learned from the jQuery source" casts
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
NotImplementedError: Could not run torchvision::nms
查看>>
nova基于ubs机制扩展scheduler-filter
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘html-webpack-plugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>