03-类别管理--添加类别--控制器层,类别管理–根据父级类别查询其所有子级类别–持久层\业务逻辑层\控制器层

10. 基于Spring JDBC的事务管理(续)

当需要方法是事务性的,可以使用@Transactional注解,此注解可以添加在:

  • 接口
    • 会使得此接口的实现类的所有实现方法都是事务性的
  • 接口中的抽象方法上
    • 会使得此接口的实现类中,重写的此方法是事务性的
    • 只作用于当前方法
    • 如果接口上也配置了此注解,并且接口和抽象方法的注解均配置了参数,以方法上的配置为准
  • 业务实现类
    • 会使得当前类中所有重写的方法都是事务性
      • 自定义的方法不会是事务性的
  • 业务实现类中的方法
    • 不可以添加在自定义的(不是重写的接口的)方法上
      • 语法上,可以添加,但执行时,不允许

Spring JDBC是通过接口代理的方式进行事务管理,所以,只对接口中声明的方法有效!

通常,应该将@Transactional添加在接口中的抽象方法上(如果偷懒,或为了避免遗漏,也可以直接添加在接口上)。

目前,由于csmall-product-service没有添加相关依赖,所以,并不能直接在接口中使用@Transactional注解(因为尚不可识别),则应该在此Module中添加依赖:

<!-- Mybatis Spring Boot:Mybatis及对Spring Boot的支持 -->
<!-- 仅需要保留spring-jdbc,使得业务接口可以使用@Transactional注解 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-autoconfigure</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </exclusion>
    </exclusions>
</dependency>

11. 类别管理–添加类别–业务逻辑层(续)

目前,在业务实现中,视为“错误”时始终抛出ServiceException,且没有任何异常信息,是不合理的!

在略大规模的项目中,“错误”的种类可能较大,如果为每一种“错误”都创建一个对应的异常,则需要创建的异常类型就比较多,但是,这些异常类除了名称不一样以外,几乎没有不同,所以,存在不利于管理和维护的问题。

其实,也可以只使用1个异常类型(或者少量异常类型),但是,每次抛出时,也需要明确的表示“是哪一种错误”,则可以在异常类型中添加“业务状态码”。

则首先需要业务状态码的类型,可以从前缀项目中复制State文件,例如:

package cn.tedu.csmall.common.web;

public enum State {

    OK(20000),
    ERR_CATEGORY_NAME_DUPLICATE(40100), // 客户端引起的--类别--名称冲突(被占用)
    ERR_CATEGORY_NOT_FOUND(40101), // 客户端引起的--类别--数据不存在(查询参数值不正确)
    ERR_INSERT(50000), // 服务端引起的--插入数据错误
    ERR_UPDATE(50001); // 服务端引起的--更新数据错误

    private Integer value;

    State(Integer value) {
        this.value = value;
    }

    public Integer getValue() {
        return value;
    }

}

然后,在ServiceException中,自定义构造方法,强制要求传入State stateString message参数,并且,为State类型参数提供公有的获取值的方法:

public class ServiceException extends RuntimeException {
    private State state;
    
    public ServiceException(State state, String message) {
        super(message);
        if (state == null) {
            throw new IllegalArgumentException("使用ServiceException必须指定错误时的业务状态码!");
        }
        this.state  = state;
    }
    
    public State getState() {
        return state;
    }
}

在后续抛出异常时,应该传入State stateString message

throw new ServiceException(State.ERR_CATEGORY_NOT_FOUND, "添加类别失败,父级类别不存在!");

完成后,应该再次执行测试,以保证修改代码后,原有代码依然能正确运行。

12. 类别管理–添加类别–控制器层

12.1. 处理跨域(一次性配置)

csmall-product-webapi的根包下config包下创建SpringMvcConfiguration类,实现WebMvcConfigururer接口,重写其中的方法,以解决跨域问题:

package cn.tedu.csmall.product.webapi.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SpringMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

}

提示:以上代码从此前的案例中复制过来即可。

12.2. 控制器类与处理请求的方法

先将此前项目中的JsonResult复制到csmall-common的根包下的web子包中(需要在csmall-common中补充依赖lombok)。

csmall-product-webapi的根包下创建controller.CategoryController类,在类上添加@RestController@RequestMapping(value = "/categories", produces = "application/json; charset=utf-8")这2个注解:

@RestController
@RequestMapping(value = "/categories", produces = "application/json; charset=utf-8")
public class CategoryController {
    
}

然后,在类中添加处理请求的方法:

@Autowired
private ICategoryService categoryService;

@PostMapping("/add-new")
public JsonResult<Void> addNew(CategoryAddNewDTO categoryAddNewDTO) {
    categoryService.addNew(categoryAddNewDTO);
    return JsonResult.ok();
}

12.3. 控制器层测试

csmall-product-webapi的测试的根包下创建controller.CategoryControllerTests测试类,编写并执行测试:

@SpringBootTest
@AutoConfigureMockMvc
public class CategoryControllerTests {
    
    @Autowired
    MockMvc mockMvc;
    
    @Test
    @Sql("classpath:truncate.sql")
    public void testAddNewSuccessfully() throws Exception {
        // 准备测试数据,不需要封装,应该全部声明为String类型
        String name = "水果";
        String parentId = "0"; // 即使目标类型是Long,参数值也不要加L
        String keywords = "水果的关键字是啥";
        String sort = "66";
        String icon = "图标待定";
        String isDisplay = "1";
        // 请求路径,不需要写协议、服务器主机和端口号
        String url = "/categories/add-new";
        // 执行测试
        // 以下代码相对比较固定
        mockMvc.perform( // 执行发出请求
                MockMvcRequestBuilders.post(url) // 根据请求方式决定调用的方法
                .contentType(MediaType.APPLICATION_FORM_URLENCODED) // 请求数据的文档类型,例如:application/json; charset=utf-8
                .param("name", name) // 请求参数,有多个时,多次调用param()方法
                .param("parentId", parentId)
                .param("keywords", keywords)
                .param("icon", icon)
                .param("sort", sort)
            	.param("isDisplay", isDisplay)
                .accept(MediaType.APPLICATION_JSON)) // 接收的响应结果的文档类型,注意:perform()方法到此结束
                .andExpect( // 预判结果,类似断言
                        MockMvcResultMatchers
                                .jsonPath("state") // 预判响应的JSON结果中将有名为state的属性
                                .value(20000)) // 预判响应的JSON结果中名为state的属性的值,注意:andExpect()方法到此结束
                .andDo( // 需要执行某任务
                        MockMvcResultHandlers.print()); // 打印日志
    }
    
}

12.4. 处理异常

csmall-common中添加依赖项:

<!-- Spring Boot Web:支持Spring MVC -->
<!-- 需要使用到@RestControllerAdvice等注解 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

csmall-common根包下的ex包下创建handler.GlobalExceptionHandler,并在此类中处理异常:

@RestControllerAdvice
public class GlobalExceptionHandler {
    
    @ExceptionHandler(ServiceException.class)
    public JsonResult<Void> handleServiceException(ServiceException ex) {
        return JsonResult.fail(ex.getState(), ex.getMessage());
    }

}

完成后,使用错误的测试数据时,会发现根本不会处理异常,是因为在csmall-product-webapi中默认执行的组件扫描不会扫描到以上GlobalExceptionHandler所在的包,为了解决此问题,应该先在csmall-common的根包下创建config.CsmallCommonConfiguration类,此类应该是配置类,且配置组件扫描:

package cn.tedu.csmall.common.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("cn.tedu.csmall.common.ex.handler")
public class CsmallCommonConfiguration {
}

然后,在csmall-product-webapi的启动类引用此配置类:

package cn.tedu.csmall.product.webapi;

@SpringBootApplication
@Import({CsmallCommonConfiguration.class}) // 新增
public class CsmallProductWebapiApplication {

    public static void main(String[] args) {
        SpringApplication.run(CsmallProductWebapiApplication.class, args);
    }

}

接下来,即可执行测试:

@Test
@Sql({"classpath:truncate.sql", "classpath:insert_data.sql"})
public void testAddNewFailBecauseNameDuplicate() throws Exception {
    // 准备测试数据,不需要封装,应该全部声明为String类型
    String name = "类别001";
    String parentId = "0"; // 即使目标类型是Long,参数值也不要加L
    String keywords = "水果的关键字是啥";
    String sort = "66";
    String icon = "图标待定";
    String isDisplay = "1";
    // 请求路径,不需要写协议、服务器主机和端口号
    String url = "/categories/add-new";
    // 执行测试
    // 以下代码相对比较固定
    mockMvc.perform( // 执行发出请求
            MockMvcRequestBuilders.post(url) // 根据请求方式决定调用的方法
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED) // 请求数据的文档类型,例如:application/json; charset=utf-8
                    .param("name", name) // 请求参数,有多个时,多次调用param()方法
                    .param("parentId", parentId)
                    .param("keywords", keywords)
                    .param("icon", icon)
                    .param("sort", sort)
                    .param("isDisplay", isDisplay)
                    .accept(MediaType.APPLICATION_JSON)) // 接收的响应结果的文档类型,注意:perform()方法到此结束
            .andExpect( // 预判结果,类似断言
                    MockMvcResultMatchers
                            .jsonPath("state") // 预判响应的JSON结果中将有名为state的属性
                            .value(State.ERR_CATEGORY_NAME_DUPLICATE.getValue())) // 预判响应的JSON结果中名为state的属性的值,注意:andExpect()方法到此结束
            .andDo( // 需要执行某任务
                    MockMvcResultHandlers.print()); // 打印日志
}

@Test
@Sql({"classpath:truncate.sql"})
public void testAddNewFailBecauseParentNotFound() throws Exception {
    // 准备测试数据,不需要封装,应该全部声明为String类型
    String name = "类别001";
    String parentId = "-1"; // 即使目标类型是Long,参数值也不要加L
    String keywords = "水果的关键字是啥";
    String sort = "66";
    String icon = "图标待定";
    String isDisplay = "1";
    // 请求路径,不需要写协议、服务器主机和端口号
    String url = "/categories/add-new";
    // 执行测试
    // 以下代码相对比较固定
    mockMvc.perform( // 执行发出请求
            MockMvcRequestBuilders.post(url) // 根据请求方式决定调用的方法
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED) // 请求数据的文档类型,例如:application/json; charset=utf-8
                    .param("name", name) // 请求参数,有多个时,多次调用param()方法
                    .param("parentId", parentId)
                    .param("keywords", keywords)
                    .param("icon", icon)
                    .param("sort", sort)
                    .param("isDisplay", isDisplay)
                    .accept(MediaType.APPLICATION_JSON)) // 接收的响应结果的文档类型,注意:perform()方法到此结束
            .andExpect( // 预判结果,类似断言
                    MockMvcResultMatchers
                            .jsonPath("state") // 预判响应的JSON结果中将有名为state的属性
                            .value(State.ERR_CATEGORY_NOT_FOUND.getValue())) // 预判响应的JSON结果中名为state的属性的值,注意:andExpect()方法到此结束
            .andDo( // 需要执行某任务
                    MockMvcResultHandlers.print()); // 打印日志
}

12.5. 验证请求参数格式的基本有效性

关于Validation框架的基本使用:

  • 添加依赖
  • 在控制器类中处理请求的方法的被验证的参数(封装的对象)之前添加@Validated / @Valid
  • 在参数的类型(封装的类型)的属性之前添加验证注解
  • 在统一处理异常的类中对BindException进行处理

先在csmall-product-webapi中添加依赖(如果已经添加,则不需要重复添加):

<!-- Spring Boot Validation:验证请求参数的基本格式 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

CategoryController处理请求的方法的参数之前添加@Validated / @Valid注解:

@PostMapping("/add-new")
// ===== 在以下方法的参数前添加@Validated / @Valid注解 =====
public JsonResult<Void> addNew(@Validated CategoryAddNewDTO categoryAddNewDTO) {
    categoryService.addNew(categoryAddNewDTO);
    return JsonResult.ok();
}

由于CategoryAddNewDTO等类在csmall-pojo模块中的,要在此类中添加@NotNull等注解,则必须在csmall-pojo中添加依赖:

<!-- Spring Boot Validation:验证请求参数的基本格式 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
    <scope>provided</scope>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-el</artifactId>
        </exclusion>
    </exclusions>
</dependency>

然后,CategoryAddNewDTOname属性上添加@NotNull约束(其它的约束等到测试通过之后再补充):

@Data
public class CategoryAddNewDTO implements Serializable {

    @NotNull(message = "添加类别失败,必须填写类别名称!") // 新增
    private String name;
    
    // ===== 其它原有代码 =====
    
}

State中添加对应“请求参数格式错误”的枚举值:

public enum State {

    OK(20000),
    // ===== 下行为新增 ======
    ERR_BAD_REQUEST(40000), // 客户端引起的--请求参数格式错误
    
    // ===== 其它原有代码 =====
}

GlobalExceptionHandler中添加新的处理异常的方法:

@ExceptionHandler(BindException.class)
public JsonResult<Void> handleBindException(BindException ex) {
    List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
    StringBuilder stringBuilder = new StringBuilder();
    for (FieldError fieldError : fieldErrors) {
        stringBuilder.append(";");
        stringBuilder.append(fieldError.getDefaultMessage());
    }
    String message = stringBuilder.substring(1);
    return JsonResult.fail(State.ERR_BAD_REQUEST, message);
}

最后,添加测试:

@Test
@Sql({"classpath:truncate.sql"})
public void testAddNewFailBecauseBadRequest() throws Exception {
    // 准备测试数据,注意:此次没有提交必要的name属性值
    String parentId = "0"; // 即使目标类型是Long,参数值也不要加L
    String keywords = "水果的关键字是啥";
    String sort = "66";
    String icon = "图标待定";
    String isDisplay = "1";
    // 请求路径,不需要写协议、服务器主机和端口号
    String url = "/categories/add-new";
    // 执行测试
    // 以下代码相对比较固定
    mockMvc.perform( // 执行发出请求
            MockMvcRequestBuilders.post(url) // 根据请求方式决定调用的方法
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED) // 请求数据的文档类型,例如:application/json; charset=utf-8
                    // .param("name", name) // 注意:此处不提交必要的name属性
                    .param("parentId", parentId)
                    .param("keywords", keywords)
                    .param("icon", icon)
                    .param("sort", sort)
                    .param("isDisplay", isDisplay)
                    .accept(MediaType.APPLICATION_JSON)) // 接收的响应结果的文档类型,注意:perform()方法到此结束
            .andExpect( // 预判结果,类似断言
                    MockMvcResultMatchers
                            .jsonPath("state") // 预判响应的JSON结果中将有名为state的属性
                            .value(State.ERR_BAD_REQUEST.getValue())) // 预判响应的JSON结果中名为state的属性的值,注意:andExpect()方法到此结束
            .andDo( // 需要执行某任务
                    MockMvcResultHandlers.print()); // 打印日志
}

测试成功后,应该在CategoryAddNewDTO的各属性中补充更多的、必要的注解进行约束,并且,添加更多约束后,还应该编写更多的测试。

13. 类别管理–根据父级类别查询其所有子级类别–持久层

13.1. 规划SQL语句

本次需要执行的SQL语句大致是:

select * from pms_category where parent_id=? and enable=1 and is_display=1 order by sort desc, gmt_modified desc;

关于字段列表,应该包括:

id, name, sort, icon, is_parent

13.2. 抽象方法(可能需要创建VO类)

csmall-pojo的根包下的vo包下创建CategorySimpleListItemVO类,封装以上设计的5个字段对应的属性:

package cn.tedu.csmall.pojo.vo;

import lombok.Data;

import java.io.Serializable;

@Data
public class CategorySimpleListItemVO implements Serializable {

    private Long id;
    private String name;
    private Integer sort;
    private String icon;
    private Integer isParent;

}

CategoryMapper接口中添加:

List<CategorySimpleListItemVO> listByParentId(Long parentId);

13.3. 在XML中配置SQL

CategoryMapper.xml中添加配置:

<!-- List<CategorySimpleListItemVO> listByParentId(Long parentId); -->
<select id="listByParentId" resultMap="SimpleListResultMap">
    select
        <include refid="SimpleListQueryFields" />
    from
        pms_category
    where
        parent_id=#{id} and enable=1 and is_display=1
    order by
        sort desc, gmt_modified desc
</select>

<sql id="SimpleListQueryFields">
    <if test="true">
        id, name, sort, icon, is_parent
    </if>
</sql>

<resultMap id="SimpleListResultMap" type="cn.tedu.csmall.pojo.vo.CategorySimpleListItemVO">
    <id column="id" property="id" />
    <result column="name" property="name" />
    <result column="sort" property="sort" />
    <result column="icon" property="icon" />
    <result column="is_parent" property="isParent" />
</resultMap>

13.4. 测试

本次测试推荐使用人工检查查询结果。

@Test
@Sql({"classpath:truncate.sql", "classpath:insert_data.sql"})
public void testListByParentId() {
    // 测试数据
    Long parentId = 1L;
    // 执行测试,获取查询结果
    List<CategorySimpleListItemVO> list = mapper.listByParentId(parentId);
    // 查看结果
    System.out.println("查询结果数量:" + list.size());
    for (CategorySimpleListItemVO item : list) {
        System.out.println(item);
    }
}

14. 类别管理–根据父级类别查询其所有子级类别–业务逻辑层

14.1. 接口和抽象方法

ICategoryService中添加:

List<CategorySimpleListItemVO> listByParentId(Long parentId);

14.2. 实现

CategoryServiceImpl中直接调用categoryMapper执行查询并返回即可。

@Override
public List<CategorySimpleListItemVO> listByParentId(Long parentId) {
    return categoryMapper.listByParentId(parentId);
}

14.3. 测试

与持久层测试类似。

@Test
@Sql({"classpath:truncate.sql", "classpath:insert_data.sql"})
public void testListByParentId() {
    // 测试数据
    Long parentId = 1L;
    // 执行测试,获取查询结果
    List<CategorySimpleListItemVO> list = service.listByParentId(parentId);
    // 查看结果
    System.out.println("查询结果数量:" + list.size());
    for (CategorySimpleListItemVO item : list) {
        System.out.println(item);
    }
}

15. 类别管理–根据父级类别查询其所有子级类别–控制器层

CategoryController中添加:

@GetMapping("/list-by-parent")
public JsonResult<List<CategorySimpleListItemVO>> listByParentId(Long parentId) {
    // 调用service并将结果封装到JsonResult中
    List<CategorySimpleListItemVO> list = categoryService.listByParentId(parentId);
    return JsonResult.ok(list);
}

CategoryControllerTests中测试:

@Test
@Sql({"classpath:truncate.sql", "classpath:insert_data.sql"})
public void testListByParentId() throws Exception {
    // 准备测试数据,注意:此次没有提交必要的name属性值
    String parentId = "0";
    // 请求路径,不需要写协议、服务器主机和端口号
    String url = "/categories/list-by-parent";
    // 执行测试
    // 以下代码相对比较固定
    mockMvc.perform( // 执行发出请求
            MockMvcRequestBuilders.get(url) // 根据请求方式决定调用的方法
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED) // 请求数据的文档类型,例如:application/json; charset=utf-8
                    .param("parentId", parentId)
                    .accept(MediaType.APPLICATION_JSON)) // 接收的响应结果的文档类型,注意:perform()方法到此结束
            .andExpect( // 预判结果,类似断言
                    MockMvcResultMatchers
                            .jsonPath("state") // 预判响应的JSON结果中将有名为state的属性
                            .value(State.OK.getValue())) // 预判响应的JSON结果中名为state的属性的值,注意:andExpect()方法到此结束
            .andDo( // 需要执行某任务
                    MockMvcResultHandlers.print()); // 打印日志
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青木编码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值