SpringBoot 三层开发
1.前后端分离
前后端分离开发模式是目前比较流行的开发模式,指的是:项目基于前后端分离的架构进行开发,前后端分离架构总体上包括前端和服务端(后端),通常是多人协作开发。
- 对于后端java工程师: 把精力系统设计、数据库表设计、业务逻辑处理、性能优化等。
- 对于前端工程师: 把精力放页面编写、样式、动画效果、浏览器兼容性等。
2. 前后端开发流程
1,需求分析
前后端:梳理用户的需求,分析业务流程
2,接口定义
前后端:根据需求进行分析,由后端定义出简版接口文档
3,正式编码
后端:依据接口文档进行服务端接口开发,使用swagger生成页面格式的接口文档
前端:根据用户需求开发操作界面,并根据页面接口文档制作mock数据,进行测试
4,接口联调
前端调用后端接口进行,出现接口问题由后端解决,前端调试接口直到接口全部调通没有问题
3. 后端(服务端)接口开发步骤
- 定义基础类:在业务模块中定义需求中对应的domain类和vo类
- 定义Controller类:在admin模块中按接口文档要求定义Controller类和接口,接口内暂时返回null
- 定义Mapper类:在业务模块中定义业务层需要用到的数据层Mapper类
- 定义Service类:在业务模块中定义业务层的Service接口类和实现类,并实现业务逻辑
- 补全Controller接口:在admin模块中Controller的接口内补全service的简单调用
- 接口测试:启动服务,通过接口测试工具swagger/knife4j/postman/apipost/apifox进行接口测试
4. RestFul风格API
REST,即Representational State Transfer的缩写。直接翻译的意思是"表现层状态转化"。 它是一种互联网应用程序的API设计理念:URL定位资源,用HTTP动词(GET,POST,PUT,DELETE)描述操作。
HTTP动词 | URL | 含义 |
---|---|---|
GET | http://localhost:8080/channel/1 | 查询ID为1的频道 |
POST | http://localhost:8080/channel | 新增频道 |
PUT | http://localhost:8080/channel/1 | 修改ID为1的频道 |
DELETE | http://localhost:8080/channel/1 | 删除ID为1的频道 |
5. 工程编码规范
- 包名规范 •包名定义按照统一格式:com.pf.模块名 •在模块名下新建controller、service、mapper
- Controller规范 •请求参数: 普通请求参数使用DTO类型 分页请求参数定义DTO类继承PageRequestDto •响应结果:统一响应ResponseResult类型
- 类规范 •三层类:类名称需与业务有关 •三层类:单一职责原则
6. 搭建工程结构
- 技术选型:SpringBoot Spring SpringMVC JDBCTemplate
- 操作步骤:
- IDEA新建Maven工程
- 依赖:配置Maven依赖坐标
- 配置:配置数据源信息
- 代码:启动类、基础类、包结构
- 校验:启动查看日志
7. 开发接口
7.1 ------ 新增频道 ------
7.1.1 接口描述
- 请求路径:/channel/add
- 请求方式:POST
- body参数:body -> json
- 响应:统一JSON
7.1.2 接口实现:
- controller
- service
7.2 ------修改频道 ------
7.2.1 接口描述
- 请求路径:/channel/update/{id}
- 请求方式:PUT
- body参数: json
- 响应:统一JSON
7.2.2 接口实现:
- controller
- service
7.3 ------删除频道 ------
7.3.1 接口描述
- 请求路径:/channel/delete/{id}
- 请求方式:DELETE
- body参数:无
- 响应:统一JSON
7.3.2 接口实现:
- controller
- service
7.4 ------查询单个频道 ------
7.4.1 接口描述
- 请求路径:/channel/{id}
- 请求方式:GET
- body参数:无
- 响应:统一JSON
7.4.2 接口实现:
- controller
- service
7.5 ------查询全部频道 ------
7.5.1 接口描述
- 请求路径:/channel/list
- 请求方式:GET
- body数据:无
- 响应:统一JSON
7.5.2 接口实现:
- controller
- service
8. 代码实现
controller
代码语言:javascript复制package com.pf.wemedia.controller;
import com.pf.wemedia.dto.WmChannelDto;
import com.pf.wemedia.service.WmChannelService;
import com.pf.wemedia.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/channel")
public class WmChannelController {
@Autowired
private WmChannelService wmChannelService;
@PostMapping("/add")
public ResponseResult add(@RequestBody WmChannelDto dto){
return wmChannelService.add(dto);
}
@PutMapping("/update/{id}")
public ResponseResult update(@PathVariable("id")Integer id, @RequestBody WmChannelDto dto){
return wmChannelService.update(id,dto);
}
@DeleteMapping("/delete/{id}")
public ResponseResult delete(@PathVariable("id") Integer id){
return wmChannelService.delete(id);
}
@GetMapping("/{id}")
public ResponseResult one(@PathVariable("id") Integer id){
return wmChannelService.one(id);
}
@GetMapping("/list")
public ResponseResult list(){
return wmChannelService.list();
}
}
service
代码语言:javascript复制package com.pf.wemedia.service;
import com.pf.wemedia.dto.WmChannelDto;
import com.pf.wemedia.util.ResponseResult;
public interface WmChannelService {
/**
* 新增频道
* @param dto
* @return
*/
ResponseResult add(WmChannelDto dto);
/**
* 更新频道
* @param id
* @param dto
* @return
*/
ResponseResult update(Integer id, WmChannelDto dto);
/**
* 删除频道
* @param id
* @return
*/
ResponseResult delete(Integer id);
/**
* 查询单条频道
* @param id
* @return
*/
ResponseResult one(Integer id);
/**
* 查询全部频道列表
* @return
*/
ResponseResult list();
}
serviceImpl
代码语言:javascript复制package com.pf.wemedia.service.impl;
import com.pf.wemedia.dto.WmChannelDto;
import com.pf.wemedia.pojo.WmChannel;
import com.pf.wemedia.service.WmChannelService;
import com.pf.wemedia.util.AppHttpCodeEnum;
import com.pf.wemedia.util.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
@Service
public class WmChannelServiceImpl implements WmChannelService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public ResponseResult add(WmChannelDto dto) {
WmChannel wmChannel = new WmChannel();
BeanUtils.copyProperties(dto,wmChannel);
wmChannel.setCreatedTime(new Date());
String sql = "insert into wm_channel values (?,?,?,?,?,?,?);";
Object[] args = {wmChannel.getId(), wmChannel.getName(), wmChannel.getDescription(),
wmChannel.getIsDefault(), wmChannel.getStatus(), wmChannel.getOrd(), wmChannel.getCreatedTime() };
int result = jdbcTemplate.update(sql, args); //result表示生效条数,为0表示失败,大于0表示成功
if(result==0){
return ResponseResult.errorResult(AppHttpCodeEnum.ADD_ERROR);
}
return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
}
@Override
public ResponseResult update(Integer id, WmChannelDto dto) {
WmChannel wmChannel = new WmChannel();
BeanUtils.copyProperties(dto,wmChannel);
wmChannel.setId(id);
String sql = "update wm_channel set name=?,description=? where id=?";
Object[] args = {wmChannel.getName(), wmChannel.getDescription(), wmChannel.getId()};
int result = jdbcTemplate.update(sql, args); //result表示生效条数,为0表示失败,大于0表示成功
if(result==0){
return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
}
return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
}
@Override
public ResponseResult delete(Integer id) {
String sql = "delete from wm_channel where id=?";
Object[] args = {id};
int result = jdbcTemplate.update(sql, args); //result表示生效条数,为0表示失败,大于0表示成功
if(result==0){
return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
}
return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
}
@Override
public ResponseResult one(Integer id) {
String sql = "select * from wm_channel where id=?";
Object[] args = {id};
WmChannel wmChannel = jdbcTemplate.queryForObject(sql,
BeanPropertyRowMapper.newInstance(WmChannel.class), args);
return ResponseResult.okResult(wmChannel);
}
@Override
public ResponseResult list() {
String sql = "select * from wm_channel";
List<WmChannel> wmChannelList = jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(WmChannel.class));
return ResponseResult.okResult(wmChannelList);
}
}
dto
代码语言:javascript复制package com.pf.sb.dto;
import com.pf.sb.pojo.WmChannel;
public class WmChannelDto extends WmChannel {
}
pojo
代码语言:javascript复制package com.pf.sb.pojo;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
public class WmChannel implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
/**
* 频道名称
*/
private String name;
/**
* 频道描述
*/
private String description;
/**
* 是否默认频道
* 1:默认 true
* 0:非默认 false
*/
private Boolean isDefault;
/**
* 是否启用
* 1:启用 true
* 0:禁用 false
*/
private Boolean status;
/**
* 默认排序
*/
private Integer ord;
/**
* 创建时间
*/
private Date createdTime;
}
util.AppHttpCodeEnum
代码语言:javascript复制package com.pf.sb.util;
public enum AppHttpCodeEnum {
SUCCESS(200,"操作成功"),
ADD_ERROR(1,"新增失败"),
UPDATE_ERROR(2,"更新失败"),
DELETE_ERROR(3,"删除失败");
int code;
String errorMessage;
AppHttpCodeEnum(int code, String errorMessage){
this.code = code;
this.errorMessage = errorMessage;
}
public int getCode() {
return code;
}
public String getErrorMessage() {
return errorMessage;
}
}
util.ResponseResult
代码语言:javascript复制package com.pf.sb.util;
import java.io.Serializable;
/**
* 通用的结果返回类
* @param <T>
*/
public class ResponseResult<T> implements Serializable {
private String host;
private Integer code;
private String errorMessage;
private T data;
public ResponseResult() {
this.code = 200;
}
public ResponseResult(Integer code, T data) {
this.code = code;
this.data = data;
}
public ResponseResult(Integer code, String msg, T data) {
this.code = code;
this.errorMessage = msg;
this.data = data;
}
public ResponseResult(Integer code, String msg) {
this.code = code;
this.errorMessage = msg;
}
public static ResponseResult errorResult(int code, String msg) {
ResponseResult result = new ResponseResult();
return result.error(code, msg);
}
public static ResponseResult okResult(int code, String msg) {
ResponseResult result = new ResponseResult();
return result.ok(code, null, msg);
}
public static ResponseResult okResult(Object data) {
ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getErrorMessage());
if(data!=null) {
result.setData(data);
}
return result;
}
public static ResponseResult errorResult(AppHttpCodeEnum enums){
return setAppHttpCodeEnum(enums,enums.getErrorMessage());
}
public static ResponseResult errorResult(AppHttpCodeEnum enums, String errorMessage){
return setAppHttpCodeEnum(enums,errorMessage);
}
public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums){
return okResult(enums.getCode(),enums.getErrorMessage());
}
private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String errorMessage){
return okResult(enums.getCode(),errorMessage);
}
public ResponseResult<?> error(Integer code, String msg) {
this.code = code;
this.errorMessage = msg;
return this;
}
public ResponseResult<?> ok(Integer code, T data) {
this.code = code;
this.data = data;
return this;
}
public ResponseResult<?> ok(Integer code, T data, String msg) {
this.code = code;
this.data = data;
this.errorMessage = msg;
return this;
}
public ResponseResult<?> ok(T data) {
this.data = data;
return this;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
}