Welcome to “Algorithms and the Beauty of Programming” ↑ pay attention to us!

This article was first published on the wechat official account “Beauty of Algorithms and Programming”. Please follow us and learn more about this series of articles in time.

1 Problem Description

Recently, I completed a student bank card change system using SSH framework. During the process, I also encountered many difficulties, one of which was to query through the joint table and hand the data to the front end. Used to learn the main SSM framework using Mybatis joint table query, suddenly to an SSH framework is really a little ignorant.

2 Problem Analysis

This question is to be queried through a one-to-one linkage table, that is, I have a student information table and a bank card information table. By querying these two tables, I will send the data in JSON format to the front end after matching the students and corresponding bank cards. This leads to two problems, how to join table queries and how to render the retrieved data in JSON format.

3 Solution

When WE first started, we didn’t know that we could use the @Query annotation to write SQL statements for a federative table Query and let the Query statement be executed by the annotated methods. I also don’t know how to use the key-value pair of the queried data for the front end. Later, with the help of the teacher and Baidu, I also solved this problem.

First of all, this is the method I used in the DAO layer interface to Query a linked table. This method sets it as a List collection of Object[] array type, gives the method an annotation @Query, and assigns the annotation value to the SQL statement for the linked table Query, followed by an nativeQuery = true. Create an entity class BankCardApplyModel and place all the attributes of the query in it, so as to provide JSON data to the front end. For example:

The next step is to convert the data to JSON format in the Service layer. My method is to create a set of BankCardApplyModel data type by iterating through the data just queried. ArrayList<> can easily add and subtract elements in the set. Define a set variable tmpList of Object[] data type, assign tmpList to the result of the joint table query, loop through tmpList, define a variable BankCardApplyModel in the loop to receive the traversal value, Add (model) : add(model) : add(model); add(model) : add(model); The code is as follows:

4 summarizes

Learning is a process of expanding, only by constantly learning better ways, to optimize your code will be, will be recognized by others, and each time she learned a few things, will feel a sense of accomplishment may be this is the power of interest, I hope this article can help the junior partner and I meet the same problem.

More interesting articles:


Tips: Click on the lower right corner of the page “Write a message” to comment, looking forward to your participation! Looking forward to your forwarding!