• Java后端接口编写流程


    在这里插入图片描述

    💗wei_shuo的个人主页

    💫wei_shuo的学习社区

    🌐Hello World !


    Java后端接口编写流程

    Java后端接口编写流程,更具业务逻辑编写Java后端接口,提供给前端访问

    实现逻辑流程

    • POJO:实体类编写
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @TableName("merchant_mcc")
    public class MerchantMcc {
        /**
         * null | merchant_mcc.id | @mbg.generated
         */
        @ApiModelProperty("null")
        @TableId
        private Long id;
    
        /**
         * MCC字段Code | merchant_mcc.code | @mbg.generated
         */
        @ApiModelProperty("MCC字段Code")
        private String code;
    
        /**
         * MCC字段Description | merchant_mcc.description | @mbg.generated
         */
        @ApiModelProperty("MCC字段Description")
        private String description;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    实体类字段的工具类编写:字段常量编写

    package com.wei.gen.po;
    
    public class MerchantMccCol {
    
    	public static final String ID = "id";
    
    	public static final String CODE = "code";
    
    	public static final String DESCRIPTION = "description";
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • DAO:数据库访问层编写
    public interface MerchantMccRepo extends IService<MerchantMcc> {
    
        /**
         * MCC 根据Code查询Description
         *
         * @param queryWrapper
         * @return
         */
        MerchantMcc selectOne(QueryWrapper<MerchantMcc> queryWrapper);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 实现类Impl编写
    @Slf4j
    @Repository
    public class MerchantMccRepoImpl extends ServiceImpl<MerchantMccMapper, MerchantMcc> implements MerchantMccRepo {
    
        private final MerchantMccMapper merchantMccMapper;
    
        public MerchantMccRepoImpl(MerchantMccMapper merchantMccMapper) {
            this.merchantMccMapper = merchantMccMapper;
        }
    
        @Override
        public MerchantMcc selectOne(QueryWrapper<MerchantMcc> queryWrapper) {
            return merchantMccMapper.selectOne(queryWrapper);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    Mapper编写:实现对数据库的操作

    @Mapper
    public interface MerchantMccMapper extends BaseMapper<MerchantMcc> {
        /**
         * This method was generated by MyBatis Generator.
         * This method corresponds to the database table merchant_mcc
         *
         * @mbg.generated
         */
        MerchantMcc selectByPrimaryKey(Long id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Mapper.xml编写:自定义编写SQL语句,业务逻辑

    id属性:指定Mapper.xml文件中的SQL语句对应的Java接口方法名称,这个方法将会执行相应的SQL语句,并返回执行结果

    type属性:指定Mapper.xml文件中的Java类对应的实体类名称,这个实体类包含了与SQL语句相对应的属性和方法,用于将执行结果转换为Java对象

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.wei.gen.dao.MerchantMccMapper">
      
    </mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Service层:业务逻辑编写

     /**
         * MCC 根据Code查询Description
         */
        public String getDescriptionByCode(String code) {
            if (code == null || code.isEmpty()){
                throw new ManageException("Code不能为空");
            }
    
            MerchantMcc merchantMcc = merchantMccRepo.selectOne(new QueryWrapper<MerchantMcc>().eq("code", code));
    
            if (merchantMcc == null) {
                throw new ManageException("Code不存在");
            }
            return merchantMcc.getDescription();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    Controller层:接口编写

    @RestController
    @Slf4j
    @RequestMapping("common")
    public class SystemCommonEndpoint {
        
        @Resource
        private CommonService commonService;
    
        /**
         * MCC 根据Code查询Description
         */
        @GetMapping("/search")
        @ApiOperation("MCC根据Code查询Description")
        public Result<String> searchDescription(@RequestParam("code") String code) {
            String description = commonService.getDescriptionByCode(code);
            return Result.succ(description);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞👍收藏⭐️评论📝


    在这里插入图片描述

  • 相关阅读:
    Vue开发时Vscode建议安装的常用插件
    SpringBoot-RabbitMQ
    【Spring】——4、使用@Scope注解设置组件的作用域
    关于类成员的构造和析构函数调用的进一步理解
    中国机器人发展的机遇(赵杰)
    Python中的协程、线程和进程
    复杂查询方法-视图、子查询、函数等
    .NET混合开发解决方案13 自定义WebView2中的上下文菜单
    前端html原生页面兼容多端H5和移动端适配方案
    记一次冲突:JsonSerialize注解标注的字段返回两次的错误
  • 原文地址:https://blog.csdn.net/weixin_62765017/article/details/133427117