第二课 SpringBoot微信点餐系统买家类目设计

tags:

  • Java
  • 慕课网

categories:

  • DAO层开发
  • service层开发

文章目录

  • 第二课 SpringBoot微信点餐系统买家类目设计
    • 第一节 DAO层设计与开发
      • 1.1 数据库配置
      • 1.2 数据库映射成对象
      • 1.3 DAO层并测试一下
      • 1.4 采坑记录
      • 1. 5 优化修改时间自动更新
      • 1. 6 优化setter和getter的自动添加
      • 1. 7 优化商品查询
    • 第二节 service层设计与开发
      • 2.1 创建service接口
      • 2.2 service接口实现
      • 2.3 测试实现类中的方法

第一节 DAO层设计与开发

1.1 数据库配置

  1. pom.xml添加mysql驱动和操作数据库的依赖包
        <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency>
  1. src/main/resources/application.yaml下配置mysql的连接
spring:datasource:driver-class-name: com.mysql.jdbc.Driverusername: rootpassword: 123456url: jdbc:mysql://127.0.0.1/sell?characterEncoding=utf-8&useSSL=falsejpa:show-sql: true

1.2 数据库映射成对象

  1. 创建src/main/java/com/qnhyn/dataobject/ProductCategory.java。
  2. ProductCategory类名,驼峰式写法。数据库表名是下划线。如果表名想和类名不一致。
// 加上注解设置表名和类名不一致 对应的表为s_product_category
@Table(name = "s_product_category")
  1. 写一些数据表中的字段,加上setter、getter和toString方法。
package com.qnhyn.dataobject;import javax.persistence.*;
import java.util.Date;@Entity
public class ProductCategory {/** 类目id. */@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer categoryId;/** 类目名字. */private String categoryName;/** 类目编号. */private Integer categoryType;private Date createTime;public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public Date getUpdateTime() {return updateTime;}public void setUpdateTime(Date updateTime) {this.updateTime = updateTime;}private Date updateTime;public Integer getCategoryId() {return categoryId;}public void setCategoryId(Integer categoryId) {this.categoryId = categoryId;}public String getCategoryName() {return categoryName;}public void setCategoryName(String categoryName) {this.categoryName = categoryName;}public Integer getCategoryType() {return categoryType;}public void setCategoryType(Integer categoryType) {this.categoryType = categoryType;}@Overridepublic String toString() {return "ProductCategory{" +"categoryId=" + categoryId +", categoryName='" + categoryName + '\'' +", categoryType=" + categoryType +'}';}
}

1.3 DAO层并测试一下

  1. 创建src/main/java/com/qnhyn/repository/ProductCategoryRepository 接口。这里习惯用repository,不写成dao
package com.qnhyn.repository;
import com.qnhyn.dataobject.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {}
  1. 选中上面接口名右键GO TO -> TEST。自动进入创建好的测试类中。写一写测试函数操作数据库。
package com.qnhyn.repository;import com.qnhyn.dataobject.ProductCategory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {@Autowiredprivate ProductCategoryRepository repository;@Testpublic void findOneTest() {ProductCategory productCategory = repository.findById(1).orElse(null);System.out.println(productCategory.toString());}@Testpublic void saveTest(){ProductCategory productCategory = new ProductCategory();productCategory.setCategoryId(2);productCategory.setCategoryName("男生最爱");productCategory.setCategoryType(3);repository.save(productCategory);}
}

1.4 采坑记录

  1. 注意入口程序的目录,如果不在这,在一个sell文件夹中会报错。src/main/java/com/qnhyn/SellApplication.java

  2. 添加数据出错时。修改上面配置的ProductCategory.java

@GeneratedValue
// 变成下面
@GeneratedValue(strategy = GenerationType.IDENTITY)

1. 5 优化修改时间自动更新

  1. 上面的保存和修改其实并不实用。一般都会先查出数据对查出数据进行修改。测试后发现时间的不能自动更新了因为它取到原来的时间后又进行保存了,如下:
    @Testpublic void saveTest(){// 先查出内容 在 修改发现时间没有变化ProductCategory productCategory = repository.findById(2).orElse(null);productCategory.setCategoryType(3);repository.save(productCategory);}
  1. 加注解@DynamicUpdate自动更新时间。重新修改productCategory.setCategoryType(12)。如果设置值和本身值一致,默认不修改。并测试:
import org.hibernate.annotations.DynamicUpdate;
@Entity
@DynamicUpdate
public class ProductCategory {

1. 6 优化setter和getter的自动添加

  1. setter和getter的自动添加虽然方便,但是如果某个字段类型变化后手动更新还是很麻烦。而且字段一多也比较难以维护。
  2. 通过插件省略setter和getter一堆函数。pom.xml中配置
 <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.16</version></dependency>
  1. idea中如果要跑起来需要安装插件。Lombok plugin, 重启idea
  2. 修改我们的代码。ProductCategory.java。这里的注释**@Data帮我们自动生成了getter,setter,toString方法**。如果只希望用到getter方法可以用注释@Getter
import lombok.Data;
@Entity
@DynamicUpdate
@Data
public class ProductCategory {
  1. 整理后的代码如下,测试一下代码通过说明生成代码生效。性能方面不会有任何损失的,因为在编译时就已经生成好了那些方法。
package com.qnhyn.dataobject;import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;
import javax.persistence.*;
import java.util.Date;@Entity
@DynamicUpdate
@Data
public class ProductCategory {/** 类目id. */@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer categoryId;/** 类目名字. */private String categoryName;/** 类目编号. */private Integer categoryType;private Date createTime;private Date updateTime;public ProductCategory() {}public ProductCategory(String categoryName, Integer categoryType) {this.categoryName = categoryName;this.categoryType = categoryType;}
}
  1. 测试一下。让测试的添加数据不出现在数据库中,它实际上是一种完全回滚,加入注解 @Transactional
 @Test@Transactionalpublic void saveTest(){// 这里给ProductCategory一个构造方法就行 不用设置简单ProductCategory productCategory = new ProductCategory("女生最爱", 4);ProductCategory result = repository.save(productCategory);Assert.assertNotNull(result);//Assert.assertNotEquals(null, result);}

1. 7 优化商品查询

  1. 如果要查询商品列表,我们肯定希望一次查询到多个类别的类目。通过type来查类目,而不是查很多次。
  2. 修改接口。
public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
}
  1. 测试方法, 这里一定要做测试。如果有错误,没有检查出来错误会跟到service层。
  @Testpublic void findByCategoryTypeIn() {List<Integer> list = Arrays.asList(2,3,4);List<ProductCategory> result = repository.findByCategoryTypeIn(list);Assert.assertNotEquals(0, result.size());}
  1. 运行后报错。No default constructor for entity: : com.qnhyn.dataobject.ProductCategory;
  2. 没有设置默认的无参构造函数
public ProductCategory() {}
  1. 运行成功。断点后看到多条数据。

第二节 service层设计与开发

2.1 创建service接口

  1. sell\src\main\java\com\qnhyn下创建service包。
  2. service包下创建接口文件, 定义一些函数。CategoryService.java
package com.qnhyn.service;import com.qnhyn.dataobject.ProductCategory;import java.util.List;public interface CategoryService {ProductCategory findOne(Integer categoryId);List<ProductCategory> findAll();List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);ProductCategory save(ProductCategory productCategory);
}

2.2 service接口实现

  1. sell\src\main\java\com\qnhyn\service下创建impl包。
  2. 写个实现类。CategoryServiceImpl.java
package com.qnhyn.service.impl;import com.qnhyn.dataobject.ProductCategory;
import com.qnhyn.repository.ProductCategoryRepository;
import com.qnhyn.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class CategoryServiceImpl implements CategoryService {@Autowiredprivate ProductCategoryRepository repository;@Overridepublic ProductCategory findOne(Integer categoryId) {return repository.findById(categoryId).orElse(null);}@Overridepublic List<ProductCategory> findAll() {return repository.findAll();}@Overridepublic List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList) {return repository.findByCategoryTypeIn(categoryTypeList);}@Overridepublic ProductCategory save(ProductCategory productCategory) {return repository.save(productCategory);}
}

2.3 测试实现类中的方法

  1. 选择上面实现类。右键GO TO test,选择四个函数
  2. 写四个测试函数。右键一件运行。四个函数测试全通过即可。
package com.qnhyn.service.impl;import com.qnhyn.dataobject.ProductCategory;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.util.Arrays;
import java.util.List;import static org.junit.jupiter.api.Assertions.*;@RunWith(SpringRunner.class)
@SpringBootTest
public class CategoryServiceImplTest {@Autowiredprivate CategoryServiceImpl categoryService;@Testpublic void findOne() throws Exception {ProductCategory productCategory = categoryService.findOne(1);Assert.assertEquals(new Integer(1), productCategory.getCategoryId());}@Testpublic void findAll() throws Exception {List<ProductCategory> productCategoryList = categoryService.findAll();Assert.assertNotEquals(0, productCategoryList.size());}@Testpublic void findByCategoryTypeIn() throws Exception {List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(Arrays.asList(1, 2, 3, 4));Assert.assertNotEquals(0, productCategoryList.size());}@Testpublic void save() throws Exception{ProductCategory productCategory = new ProductCategory("热销榜", 10);ProductCategory result = categoryService.save(productCategory);Assert.assertNotNull(result);}
}

第二课 SpringBoot微信点餐系统买家类目设计相关推荐

  1. 微信点餐系统——买家类目service层创建并测试通过

    ICategoryService /*** Created by 李柏霖* 2020/10/17 15:17*/package com.lbl.service;import com.lbl.dataO ...

  2. 基于Springboot微信点餐系统的开发与实现

    微信点餐数据库注意事项 商品表: 1:在企业级中,由于数据量是比较大的,所以id一般采用的是varchar,不采用int 2:凡是涉及到价格的统一采用decimal,例如本项目中单价如下: produ ...

  3. 学习笔记 | SpringBoot微信点餐系统实战课程笔记(一)、数据库设计与创建

    本系列是用于记录学习慕课网廖师兄的<新版微服务时代Spring Boot企业微信点餐系统>实战课程的实战中的遇到的问题.疑惑.重点笔记等.文章可能不成条理,请见谅.欢迎多多交流学习~ 0. ...

  4. (附源码)springboot微信点餐系统的设计与实现 毕业设计221541

    springboot点餐微信小程序 摘 要 点餐微信小程序采用B/S模式.采用JAVA语言.springboot框架.mysql数据库.小程序框架uniapp等开工具.促进了点餐微信小程序的业务发展. ...

  5. (附源码)springboot微信点餐系统的设计与实现 毕业设计221541

    springboot点餐微信小程序 摘 要 点餐微信小程序采用B/S模式.采用JAVA语言.springboot框架.mysql数据库.小程序框架uniapp等开工具.促进了点餐微信小程序的业务发展. ...

  6. springboot微信点餐系统的设计与实现 毕业设计- 附源码221541

    springboot点餐微信小程序 摘  要 点餐微信小程序采用B/S模式.采用JAVA语言.springboot框架.mysql数据库.小程序框架uniapp等开工具.促进了点餐微信小程序的业务发展 ...

  7. SpringBoot微信点餐系统

    架构 前后端分离: 部署架构: Nginx与Tomcat的关系在这篇文章,几分钟可以快速了解: https://www.jianshu.com/p/22dcb7ef9172 补充: setting.x ...

  8. 腾讯内部技术——SpringBoot微信点餐系统

    架构 前后端分离: 部署架构: 补充: setting.xml 文件的作用:settings.xml是maven的全局配置文件.而pom.xml文件是所在项目的局部配置.Settings.xml中包含 ...

  9. 实战 | SpringBoot微信点餐系统(附源码)

    点击上方"java进阶架构师",选择右上角"置顶公众号" 20大进阶架构专题每日送达 架构 前后端分离: 补充: setting.xml 文件的作用:setti ...

最新文章

  1. 写给大忙人的ELK最新版6.2.4学习笔记-Logstash和Filebeat解析(java异常堆栈下多行日志配置支持)...
  2. html5调用手机摄像头,实现拍照上传功能
  3. linux之hexdump命令
  4. 数据结构:二叉查找树(C语言实现)
  5. linux内核模块常见问题
  6. 实战:RediSearch 高性能的全文搜索引擎
  7. 收藏+下载!Flink 社区最全学习渠道汇总
  8. 【语音去噪】基于matlab GUI软阈值+硬阈值+软硬折中阈值语音去噪【含Matlab源码 1810期】
  9. iWebOffice2015入门(二)
  10. 【中国传媒大学】史上最全的《电视原理》笔记
  11. 解决Android部分手机图片剪切返回崩溃问题
  12. 常见计算机病毒有些什么症状,电脑中病毒的症状有哪些
  13. 无法访问EChasrts官网的问题之电脑dns解析问题
  14. android本地图片转bitmap,Android中图片的网络路径转换为Bitmap格式
  15. 4g网络设置dns地址_网速变慢?你可能需要先设置好 DNS | 科普
  16. 泣神曲服务器维护,泣神曲手游预约-泣神曲官网安卓版预约v1.0.0_第一手游网
  17. 计算机二级PS教学视频百度云,计算机二级ps考试题库完整教程文件.pdf
  18. Linux系统管理上机作业2
  19. 初学ue4#2 制作3d视角人物part2
  20. IMAP协议RFC3501中文文档

热门文章

  1. 系统升级会导致服务器错误,Win10系统升级更新时出现0x80072ee2错误怎么办?
  2. linux 高级指令,Linux高级指令
  3. Java (高级)软件工程师面试考纲
  4. 文档控件Aspose概述:带你全面了解Aspose产品特点
  5. java 操作鼠标实现qq加好友_易语言通过按键模拟的方式实现QQ后台加人加群的代码...
  6. 计算机毕业论文里的编程,计算机编程毕业论文正文
  7. 自制IDE转SATA电源转接线
  8. 下载中小学各个版本电子教材地址
  9. Php生成图片的大小单位是cm,图样中的尺寸大小是以厘米为单位 ()
  10. html5 百度收录,百度网站收录教程(个人版)