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; }
实体类字段的工具类编写:字段常量编写
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"; }
- DAO:数据库访问层编写
public interface MerchantMccRepo extends IService<MerchantMcc> { /** * MCC 根据Code查询Description * * @param queryWrapper * @return */ MerchantMcc selectOne(QueryWrapper<MerchantMcc> queryWrapper); }
- 实现类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); } }
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); }
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>
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(); }
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); } }
🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——
点赞
👍收藏
⭐️评论
📝