First, the demand
The project is to allow users to set the selected teaching materials of the chapter section, in order to provide corresponding topics for the chapter section for users to do the problem.
Design: After the user has set up the textbook, the first time to log in, for chapter settings. By default, the user selects the first chapter, the first lesson, and the first section.
Idea: The user visits the page, the chapter column displays all chapters, the class column shows the first chapter under all courses, the section column displays the first chapter, the first lesson under all sections. Then get the chapter lesson information that the user currently selects. If the current user does not have the chapter section of the textbook set, set the default first chapter, first lesson, and first section for it.
Database design: Here, all the information in the chapter section is stored in a table and can be queried recursively. The parentid of the last level chapter is the ID of the textbook. Therefore, a textbook ID can be found under all the chapters of the Section information.
Second, the settlement
has been set we do not discuss here, only need to go to the library to query the corresponding chapter section can be.
So for the first section of the first lesson of the default chapter, we use a recursive function to store the results of a query in a list
/*** According to the given ID, query the first lesson, the first section (not only for chapter three, if there is a level of directory, also can check * *) * *@paramL is the textbook ID *@paramlist *@return */ Public voidGetsubchapter (LongL, list<bookchapter>list) {Bookchapter C=NULL; String SQL= "Select d.chapter_id chapter_id, D. Chapter_name Chapter_name, D. Levels levels from ' + ' (SELECT * from mic_study_book_chapter c WHERE c.parent_chapter_id =?) ORDER by C.code) D WHERE ROWNUM = 1 "; Object[] Params={L}; Try{logger.info (sql.tostring (). ReplaceAll ("\\?", "{}"), params); List<BookChapter> Li = This. Getjdbctemplate (). query (SQL, params,NewBookchapterrowmapper ()); if(Li.size ()! = 0) {C= Li.get (0); if(c! =NULL) {List.add (c); Getsubchapter (C.getid (), list);//Recursive Query}}}Catch(Exception e) {logger.error (E.getmessage (), E); } }
The characteristics of recursive query: The function method uses itself, and through a certain condition to judge out the last called recursive method.
Java Recursive Query method