If you do not use left-join or other multi-table join queries, you can use only single-table queries and Java programs to easily implement multi-table queries.

Source: Internet
Author: User

If you do not use left-join or other multi-table join queries, you can use only single-table queries and Java programs to easily implement multi-table queries.

As we mentioned last time, the left-loin clause is not used to associate queries. It may be to improve efficiency or configure cache, or to simplify the compilation of SQL statements. It is really easy to write a single table query. The problem is that it is quite necessary to query data from multiple tables.

Therefore, there is a strong demand: to query two sets, how to merge the data in two sets into one set, and the data association must be correct. In practice, I have extracted the following procedures and tools:

Process
First, query 1st sets. Based on the results of 1st sets, query 2nd sets and merge 2 sets.
Public PageVo listPage (PageVo form ){
List <Map <String, Object> list = transferDao. listPage (form );
If (list! = Null & list. size ()> 0 ){
List <Map <String, Object> loanList = loanInfoService. batchList (list );
MergeUtilmerge (list, loanList );
Form. setList (list );
}
Return form;
}

Tool Method for merging Sets
/**
* Merge associated datasets into the master table set, merge the data in the two sets into one set, and associate the data with a key to replace the left-join and other associated queries of the database.
*
* The methods are divided into three categories: merging two elements into a Map set, merging two maps, and searching for an associated Map in which another element is a set.
*
* Optional parameter: the associated keys of two maps may be one or two. When merging two maps, you can add prefix for the associated map keys, avoid duplicate names.
*
* Not applicable scenarios: When multi-Table query and search are performed and the query conditions are associated with the table and need to be paged, the single-Table query + Merge tool is not enough.
* @ Author fansunion@qq.com November 26, 2014
*/
Public class MergeUtil {
//// // Merge all the maps in the two sets //////// /////////////

/**
* Merge the associated dataset into the combination of the master table, merge the data in the two sets into one set, and associate the data using a primary key (left join is not required in the database)
*/
// Merge two sets. First, search for the associated map and then merge two separate maps. The associated keys in the two maps are the same
Public static void merge (List <Map <String, Object> mainList,
List <Map <String, Object> relationList, String relationKey,
String prefix );

// Merge two sets. First, search for the associated map and then merge two separate maps. The keys of the merged map are prefixed, and the associated keys of the two maps are two.
Public static void merge (List <Map <String, Object> mainList,
String mainKey, List <Map <String, Object> relationList,
String relationKey, String prefix );

// Merge two sets. First, search for the associated map and then merge two separate maps. The associated keys in the two maps are the same
Public static void merge (List <Map <String, Object> mainList,
List <Map <String, Object> relationList, String relationKey );

// Merge two sets. The map in the two sets is associated with the key.
Public static void merge (List <Map <String, Object> mainList,
String mainKey, List <Map <String, Object> relationList,
String relationKey );

//////////// Merge two maps. The prefix can be optional or not. If the prefix is not required, it will overwrite /////////////////////

// Merge two maps. The relation will be placed in the main total, and the keys will be prefix.
Public static void merge (Map <String, Object> mainMap,
Map <String, Object> relation, String prefix );

// Merge two maps. If there are duplicates, relationMap overwrites
Public static void merge (Map <String, Object> mainMap,
Map <String, Object> relationMap );

//// // Search for a Map associated with a set ////// ///////////////
/**
* Find the target object from a collection. If no target object is found, null is returned.
*
* @ Param the key of the two maps
*/
Public static Map <String, Object> relationMap (Map <String, Object> mainMap,
List <Map <String, Object> list, String relationKey );

// Locate the objects to be associated with a map in the set list and specify their respective keys.
Public static Map <String, Object> relationMap (Map <String, Object> mainMap,
String mainKey, List <Map <String, Object> list, String relationKey );

The source code of the tool method is not provided, and the Java Web development process and basic library are still accumulating.

First: http://fansunion.cn/article/detail/540.html

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.