day71(添加类别--持久层,添加类别--业务逻辑层)
1.类别管理--添加类别--持久层
续前日,无新增
2.规划需要执行的SQL语句
续前日,无新增
3.接口与抽象方法
此前需要执行的SQL语句大致是
select id from pms_category where name=?
1.创建CategorySimpleVO类
在csmall-pojo的根包下创建vo.CategorySimpleVO类,用于封装以上查询结果:
2.添加抽象方法(在csmall-product-webapi
的CategoryMapper
接口中)
CategorySimpleVO getByName(String name);
4.配置SQL语句
在csmall-product-webapi的CategoryMapper.xml中添加配置:
<!-- CategorySimpleVO getByName(String name); -->
<select id="getByName" resultMap="SimpleResultMap">
select id from pms_category where name=#{name}
</select>
<resultMap id="SimpleResultMap" type="cn.tedu.csmall.pojo.vo.CategorySimpleVO">
<id column="id" property="id" />
</resultMap>
5.测试
1.创建insert_data.sql文件(用于插入数据测试)
在csmall-product-webapi
的src\test\resources
下创建insert_data.sql
文件
insert into pms_category (name) value ('类别001'), ('类别002');
2.在CategoryMapperTests中添加测试方法:
3.执行整个测试类
2.类别管理--添加类别--业务逻辑层
1.接口与抽象方法
1.建立专门存放业务逻辑层的接口的Module(名为:csmall-product-service)
2.配置(新Module下的pom.xml)
3.在csmall-product的pom.xml中补充子级Module
<!-- 当前Project的各子级Module -->
<modules>
<module>csmall-product-webapi</module>
<module>csmall-product-service</module> <!-- 新增 -->
</modules>
4.删除不必要文件
-
启动类
-
src\main\resources
及其下的配置文件 -
src\test
5.创建接口并添加抽象方法
1.方法的参数应该是封装的对象(因为一个String
或Long
等简单数据不足以完成添加类别的操作)
2.创建dto.CategoryAddNewDTO类(在csmall-pojo的根包下)
package cn.tedu.csmall.pojo.dto;
import lombok.Data;
import java.io.Serializable;
3.创建接口(CategoryService接口)
在csmall-product-service
中,在cn.tedu.csmall.product.service下创建ICategoryService接口:
public interface ICategoryService {
void addNew(CategoryAddNewDTO categoryAddNewDTO);
}
2.实现
1.方法
-
因为在csmall-product-service中只能存放业务逻辑层的接口
-
业务逻辑层的实现类仍在csmall-product-webapi中。
-
因此,需要在csmall-product-webapi中依赖csmall-product-service
2.步骤
1.在Project的pom.xml
中添加对csmall-product-service
的依赖管理:
<!-- ===== 原有其它代码 ===== -->
<!-- 依赖管理,主要管理各依赖项的版本,使得子级Module添加依赖时不必指定版本 -->
<dependencyManagement>
<dependencies>
<!-- Csmall Product Service -->
<dependency>
<groupId>cn.tedu</groupId>
<artifactId>csmall-product-service</artifactId>
<version>${csmall.version}</version>
</dependency>
<!-- ===== 原有其它代码 ===== -->
2.在csmall-product-webapi
中添加依赖:
<!-- ===== 原有其它代码 ===== -->
<!-- 当前项目需要使用的依赖项 -->
<dependencies>
<!-- Csmall Product Service -->
<dependency>
<groupId>cn.tedu</groupId>
<artifactId>csmall-product-service</artifactId>
</dependency>
<!-- ===== 原有其它代码 ===== -->
3.创建service.CategoryServiceImpl类:实现ICategoryService
接口,添加
@Service
注解(在cn.tedu.csmall.product.webapi下)
package cn.tedu.csmall.product.webapi.service;
import cn.tedu.csmall.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.service.ICategoryService;
import org.springframework.stereotype.Service;
4.业务分析
1.实现(根据id查询类别信息)
-
要实现以上业务,需要先在持久层完成“根据id查询类别信息”的功能,则在CategorySimpleVO中添加private Integer depth;属性(原
getByName()
方法对应的查询也作对应的修改,虽然不是必须的)。 -
添加private Integer isParent;属性(在CategorySimpleVO中),并且,必须在接下的查询中,查出此值。
-
CategeoryMapper接口中添加:
CategorySimpleVO getById(Long id);
-
在CategoryMapper.xml中配置以上方法映射的SQL:
<!-- CategorySimpleVO getById(Long id); -->
<select id="getById" resultMap="SimpleResultMap">
select id, depth from pms_category where id=#{id}
</select> -
测试(CategoryMapperTests)
-
在CategoryMapper接口中添加:
int updateIsParentById(
-
在
CategoryMapper.xml
中配置以上方法映射的SQL:<!-- int updateIsParentById(
-
测试(CategoryMapperTests)
5.自定义异常类型(创建csmall-commonModule)
原因:
由于后续还有不少需要被多个Module共同使用的类、接口等,所以,此异常类型和后续可能被共用的类、接口都应该放在一个公共的Module中,则在Project下创建csmall-common这个新的Module,创建成功后,需要:
-
在
csmall-common
中,修改pom.xml
中的父项目 -
在
csmall-common
中,在pom.xml
删除依赖项 -
在
csmall-common
中,在pom.xml
删除<build>
配置 -
在
csmall-common
中,删除src/test
-
在
csmall-common
中,删除src/main/resources
-
在
csmall-common
中,删除启动类 -
在Project的
pom.xml
中,添加<module>
-
在Project的
pom.xml
中,添加对新Module的依赖管理 -
在
csmall-product-webapi
中的pom.xml
中,添加对csmall-common
的依赖
1.创建ex.ServiceException类
public class ServiceException extends RuntimeException {
// 暂时不加构造方法
}
6.实现业务(在csmall-product-webapi中的CategoryServiceImpl)
package cn.tedu.csmall.product.webapi.service;
import cn.tedu.csmall.common.ex.ServiceException;
import cn.tedu.csmall.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.pojo.entity.Category;
import cn.tedu.csmall.pojo.vo.CategorySimpleVO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.webapi.mapper.CategoryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;