A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

3.2.3 课程管理服务 3.2.3.1 Sql
课程计划是树型结构,采用表的自连接方式进行查询,sql语句如下:
[AppleScript] 纯文本查看 复制代码
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.2.3.2 Dao
1) mapper接口

[AppleScript] 纯文本查看 复制代码
@Mapper public interface TeachplanMapper {    
 public TeachplanNode selectList(String courseId); }

2)mapper映射文件
[AppleScript] 纯文本查看 复制代码
<resultMap type="com.xuecheng.framework.domain.course.ext.TeachplanNode" id="teachplanMap" >      <id property="id" column="one_id"/>    
  <result property="pname" column="one_name"/>  
     <collection property="children" 

[AppleScript] 纯文本查看 复制代码
ofType="com.xuecheng.framework.domain.course.ext.TeachplanNode">      
    <id property="id" column="two_id"/>     
     <result property="pname" column="two_name"/>   
       <collection property="children"  ofType="com.xuecheng.framework.domain.course.ext.TeachplanNode">      
        <id property="id" column="three_id"/>     
         <result property="pname" column="three_name"/>    
      </collection>   
   </collection>
  </resultMap>  
  <select id="selectList" resultMap="teachplanMap" parameterType="java.lang.String" >    
  SELECT   
     a.id one_id,   
     a.pname one_name,   
     b.id two_id,      
  b.pname two_name,    
    c.id three_id,    
    c.pname three_name  
      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'    
  <if test="_parameter!=null and _parameter!=''">    
      and a.courseid=#{courseId}  
    </if>    
    ORDER BY a.orderby,   
     b.orderby,     
   c.orderby </select>

说明:针对输入参数为简单类型#{}中可以是任意类型,判断参数是否为空要用 _parameter(它属于mybatis的内 置参数)
3.4.3.3 Service
创建CourseService类,定义查询课程计划方法。

[AppleScript] 纯文本查看 复制代码
@Service public class CourseService { 
    @Autowired  
   TeachplanMapper teachplanMapper;  
   //查询课程计划  
   public TeachplanNode findTeachplanList(String courseId){  
       TeachplanNode teachplanNode = teachplanMapper.selectList(courseId);       
  return teachplanNode;    
 } }

3.4.3.4 Controller
[AppleScript] 纯文本查看 复制代码
@RestController @RequestMapping("/course") public class CourseController implements CourseControllerApi {  
   @Autowired   
  CourseService courseService;  
     //查询课程计划  
   @Override   
  @GetMapping("/teachplan/list/{courseId}")  
   public TeachplanNode findTeachplanList(String courseId) {     
    return courseService.findTeachplanList(courseId); 
    } }

3.4.3.5 测试
使用postman或swagger-ui测试查询接口。 Get 请求:http://localhost:31200/course/teachplan/list/402885816243d2dd016243f24c030002


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马