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