Java Recursive Query method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.