目录

基本概念

代码与实例


基本概念

此篇博文记录了本人目前对web后端开发的认识。

Web开发一般的思路:

先dao再service最后controller

而dao又分为dataobject和repository!

而这个dataobject一般是数据库的映射,比如MySQL数据库,如果用jpa,那么命名也是有讲究的,但如果使用注解,就没有这么多的讲究了。

而这个repository,为接口,结构接口extends JpaRepository,并且可以自己写一些方法。这里就可以通过jpa操作数据。

个人觉得Service层一般先搞一个接口,把需要的逻辑业务写到这个接口里面,在用一个类,继承这个接口,然后通过调用repository,把那些数据给记录进去。这里可以看到一般Service中都有一个@Autowired,把Repository给注入进去了。

对于Controller层目前还没有什么深的体会。

对于dao,感觉是搞数据的,估计这个就叫做数据持久层,这个名字可真骚气啊。

对于service,感觉是搞逻辑业务的,但逻辑业务不够的时候,可以新增几个函数到接口里面,继承的类去实例化。

还有个关键点:个人觉得除非很熟悉这套模式可以不进行go to test,这个go to test还是很有用的,至少可以保证这部分代码是可以跑的。

还有个关键的地方,就是少去用0,1,表示状态,java中的枚举,感觉比C++的爽很多,感觉就和类,对象一样。多使用枚举去表示状态,增加可读性

代码与实例

如下的结构:

其中dataobject就是放基本数据,与数据库进行映射的。

其中enums就是增加代码可读性,避免大量的状态用0,1表示。

其中repository为存储数据的,service就是操作这个的。

其中service中有接口,和实例,这个要分开,这样才能使得结构更加清晰,

下面给出一组的代码(ProductInfo的dao和service层代码)

ProductInfo.java

package selldemo.demo.dataobject;import lombok.Data;import javax.persistence.Entity;
import javax.persistence.Id;
import java.math.BigDecimal;@Entity
@Data
public class ProductInfo {@Idprivate String productId;//名字private String productName;//单价private BigDecimal productPrice;//库存private Integer productStock;//描述private String productDescription;//小图private String productIcon;//状态 0 正常, 1 下架private Integer productStatus;//类目编号private Integer categoryType;
}

ProductInfoRepository.java

package selldemo.demo.repository;import org.springframework.data.jpa.repository.JpaRepository;
import selldemo.demo.dataobject.ProductInfo;import java.util.List;public interface ProductInfoRepository extends JpaRepository<ProductInfo, String> {//Dao层的东西主要在service中使用List<ProductInfo> findByProductStatus(Integer productStatus);
}

ProdctService.java

package selldemo.demo.service;import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import selldemo.demo.dataobject.ProductInfo;import java.util.List;//service层
public interface ProductService {ProductInfo findOne(String productId);//查询所有商品列表List<ProductInfo> findUpAll();Page<ProductInfo> findAll(Pageable pageable);ProductInfo save(ProductInfo productInfo);
}

ProductServiceImpl.java

package selldemo.demo.service.impl;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import selldemo.demo.dataobject.ProductInfo;
import selldemo.demo.enums.ProductStatusEnum;
import selldemo.demo.repository.ProductInfoRepository;
import selldemo.demo.service.ProductService;import java.util.List;@Service
public class ProductServiceImpl implements ProductService {@Autowiredprivate ProductInfoRepository repository;@Overridepublic ProductInfo findOne(String productId) {return repository.findOne(productId);}@Overridepublic List<ProductInfo> findUpAll() {return repository.findByProductStatus(ProductStatusEnum.UP.getCode());}@Overridepublic Page<ProductInfo> findAll(Pageable pageable) {return repository.findAll(pageable);}@Overridepublic ProductInfo save(ProductInfo productInfo) {return repository.save(productInfo);}
}

另外的那个枚举

ProductStatusEnum.java

package selldemo.demo.enums;import lombok.Getter;@Getter
public enum ProductStatusEnum {UP(0, "在架"),DOWN(1, "下架");private Integer code;private String message;ProductStatusEnum(Integer code, String message){this.code = code;this.message = message;}
}

Spring Boot笔记-目前对Web后端开发的认识相关推荐

  1. Spring Boot 2.X 对 web 的开发支持(二)

    Spring Boot 2.X 对 web 的支持开发 上章节的 Spring Boot 的入门案例,我们感受到 Spring Boot 简单的配置即可运行项目. 今天了解 Spring Boot 对 ...

  2. Spring Boot笔记-JPA分页(后端分页)

    数据库内容如下: 使用Pageable即可. Maven如下: <?xml version="1.0" encoding="UTF-8"?> < ...

  3. Spring Boot(3) Web开发(1)静态资源处理

    Spring Boot(3) Web开发(1)静态资源处理 基于spring boot 2.4.3版本 1.静态资源访问 1.1 静态资源目录 把静态资源放在类路径下的以下目录:/static; /p ...

  4. 记一次Spring boot 和Vue的前后端分离的入门培训

    记一次Spring boot 和Vue的前后端分离的入门培训 由于公司之前是写C#的,现在要转 Java分布式 + vue,所以进行一次前后端的简单培训. 前端工具和环境: Node.js V10.1 ...

  5. 重新学习web后端开发-001-写在前面的话

    "长风破浪会有时 直挂云帆济沧海" -- 李白 <!-- more --> 1. 为什么会写这个系列 随着互联网技术飞速的非常,web开发一直都是互联网技术的重要部分之 ...

  6. Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台项目

    项目介绍 Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台 基于 Layui 的后台管理系统模板,扩展 Layui 原生 U ...

  7. spring boot构建基础版web项目(一)springboot、thymeleaf控制层基础构

    原文作者:弥诺R 原文地址:http://www.minuor.com/147852147/article 转载声明:转载请注明原文地址,注意版权维护,谢谢! 写前说明 根据个人在各篇博文中看到的信息 ...

  8. 【Spring Boot】使用Spring Boot来搭建Java web项目以及开发过程

    [Spring Boot]使用Spring Boot来搭建Java web项目以及开发过程 一.Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来 ...

  9. Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台

    前言 项目介绍 Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台 基于Layui的后台管理系统模板,扩展Layui原生UI样 ...

最新文章

  1. Java 组合模式及其应用
  2. 重磅!2021泰晤士世界大学排名公布,清华排名首次挺进top20
  3. [排序算法] 选择排序(2种)
  4. 12.6日个人工作总结
  5. php chilkat.certstore,angularjs实现冒泡排序算法的可视化
  6. https连接java_如何从Java应用程序设置Https连接
  7. leetcode(3)——414. 第三大的数(C++中的 set,::作用符号,迭代器),628 三个数的最大乘积(sort函数的用法)
  8. 《C和指针》——数组的存储顺序
  9. 程序员们之间的“鄙视链”,程序员底之间无声的战争
  10. 将小程序代码转成uni-app代码
  11. 复杂c语言游戏程序代码,【计算机】c语言经典游戏代码分享!
  12. 白帽子讲Web安全pdf
  13. 办公软件不能打印能打印测试页,在office办公软件word中不能打印是什么原因
  14. 阿里面试题:设计相关的系统对外提供商品实时价格获取功能
  15. 照片删除格式化恢复后损坏的碎片重组修复数据恢复方法
  16. html 分页样式首页下一页,css中分页样式怎么设置
  17. css3上下滑动,CSS3 上下浮动的页面滚动提示箭头
  18. 【游戏开发实战】Unity循环复用列表,支持不规则尺寸(对象池 | UGUI | ScrollRect | Demo源码)
  19. 鸿蒙开发|呼吸训练实战项目(一)
  20. 怎么把线稿提取出来_PS怎么扣抠线稿?Adobe Photoshop CS6如何提取清晰的线稿

热门文章

  1. 禁用UITabBarController双击事件
  2. 肿瘤坏死因子(TNF)阻断剂治疗幼年型银屑病关节炎: 有效吗
  3. 网络知识入门:路由器基础知识全接触
  4. 意尔康体育:帆软助力其提速增效,让数据帮助业务效率提升400%
  5. 程序员应学习蜡笔小新的心态
  6. 哈佛成功金句 -25则
  7. 03MFC的ODBC类简介
  8. mysql dp.cal 显示汉子_计算1到N中各个数字出现的次数 --数位DP
  9. githup用户名密码怎么看_MacBook Pro 开机密码忘记解决方法
  10. pytorch分布式训练(一):torch.nn.DataParallel