微服务开发模式下的接口开发流程

最近开发微服务项目, 接口方面的定义与之前传统的开发流程略有差池, 为了巩固与增加理解深度, 故写下次文章, 仅为总结浅显心得;

1, 定义ControllerApi接口:

微服务工程会用到Api接口, 该接口一般都会有专门的工程来存放

那么先定义Api接口, 如图:

接口定义了要实现的方法, 需要值得注意的是, ResponseResult是定义好的返回值对象, 接口对数据的操作完成后, 需要返回结果, 

当中就有成功, 失败等的常量定义:

/**
 * @Author: mrt.
 * @Description:
 * @Date:Created in 2018/1/24 18:33.
 * @Modified By:
 */
@Data
@ToString
@NoArgsConstructor
public class ResponseResult implements Response {

    //操作是否成功
    boolean success = SUCCESS;

    //操作代码
    int code = SUCCESS_CODE;

    //提示信息
    String message;

    public ResponseResult(ResultCode resultCode){
        this.success = resultCode.success();
        this.code = resultCode.code();
        this.message = resultCode.message();
    }

    public static ResponseResult SUCCESS(){
        return new ResponseResult(CommonCode.SUCCESS);
    }
    public static ResponseResult FAIL(){
        return new ResponseResult(CommonCode.FAIL);
    }

}

 

2. 如果是使用Mybaits, 则第二步先定义好SQL语句:

SELECT

a.id one_id,

a.pname one_pname,

b.id two_id,

b.pname two_pname,

c.id three_id,

c.pname three_pname

FROM

teachplan a

LEFT JOIN teachplan b

ON a.id = b.parentid

LEFT JOIN teachplan c

ON b.id = c.parentid

WHERE a.parentid = '0'

AND a.courseid = '402885816243d2dd016243f24c030002'

ORDER BY a.orderby,

b.orderby,

c.orderby

 

3. 定义Dao层的Mapper接口:

 

4. 那么相对应的Mapping文件:

<?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.xuecheng.manage_course.dao.TeachplanMapper">

    <resultMap id="teachplanMap" type="com.xuecheng.framework.domain.course.ext.TeachplanNode">
        <id column="one_id" property="id"></id>
        <result column="one_pname" property="pname"></result>
        <collection property="children" ofType="com.xuecheng.framework.domain.course.ext.TeachplanNode">
            <id column="two_id" property="id"></id>
            <result column="two_pname" property="pname"></result>
            <collection property="children" ofType="com.xuecheng.framework.domain.course.ext.TeachplanNode">
                <id column="three_id" property="id"></id>
                <result column="three_pname" property="pname"></result>
            </collection>
        </collection>

    </resultMap>

    <select id="selectList" parameterType="java.lang.String"
            resultMap="teachplanMap">
                      SELECT
                      a.id one_id,
                      a.pname one_pname,
                      b.id two_id,
                      b.pname two_pname,
                      c.id three_id,
                      c.pname three_pname
                    FROM
                      teachplan a
                      LEFT JOIN teachplan b
                        ON b.parentid = a.id
                      LEFT JOIN teachplan c
                        ON c.parentid = b.id
                    WHERE a.parentid = '0'
                    <if test="_parameter !=null and _parameter!=''">
                        AND a.courseid = #{courseId}
                    </if>

                    ORDER BY a.orderby,
                      b.orderby,
                      c.orderby
    </select>
</mapper>

这里对Mybatis的mapping文件的参数记性说明下, 巩固基础

a, <mapper></mapper>标签是整个mapping文件的父标签, 所有的增删改查的标签跟语句均在其中, namespace属性的值是mapper接口的全路径;

b. 属性映射, resultMap是映射返回值实体类属性与数据库表字段的一个标签, 其中type是返回值封装类的全路径, 也可以是其他类型, column是对应数据库的字段名, property是返回值实体类的属性名; 其中值得提到的是, 截图中的resultMap是做了转译映射处理, 就是返回值实体类teachplanNode内部是一个list结构, 如图, 在需要制定返回值的结构类型的时候, 可以在reslultMap中进行操作

5. 定义Service

6. 定义Controller实现api接口

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值