Do not use Left-join, such as multi-table association query, only with single-table query and Java program, easy to achieve "multi-table query" effect

Source: Internet
Author: User

The last time we mentioned that not using the Left-loin Association query, it might be to improve efficiency or to configure caching, or to simplify the writing of SQL statements. Just write a single-table query, SQL is really too simple. The problem is that querying data for multiple tables is still very much needed.

So there is a strong need to query 2 collections, how to combine data from 2 collections into 1 sets, and data associations to be correct. In practice, I refine the following process and tool methods:

Process
Query the 1th collection first, query the 2nd collection according to the result of the 1th collection, 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 methods for merging collections
/**
* Merges the associated data collection into the main table collection, merges the data from the 2 collections into a single collection, and associates them with a key to replace the database's left-join and other associated queries.
*
* methods are divided into 3 classes: Merging 2 elements for the collection of maps, merging 2 maps, finding 1 maps in another element for the associated map of the set.
*
* Optional Parameters: 2 Map Association key may be 1, or 2; When merging 2 maps, you can prefix the key of the associated map with prefix, prevent duplicate names, or do not add them to each other.
*
* Not applicable to the scene: Multi-table Query search and query conditions in the association table and the need for paging, only the use of single-table query +merge tool, is not enough.
* @author [email protected] November 26, 2014
*/
public class Mergeutil {
Merges all of the map/////////////////////in a 2 collection

/**
* Merge the associated data collection into the primary table, combine the data from 2 sets into a single collection, and associate it with a primary key (without a database left join)
*/
Merge 2 sets, find the associated map first, and then merge the 2 separate map,2 map with the associated key is the same
public static void Merge (List<map<string, object>> mainlist,
List<map<string, object>> relationlist, String Relationkey,
String prefix);

//merges 2 sets, finds the associated map first, merges 2 separate maps, merges the key of the map with the prefix, and the associated key in the 2 map is 2
public static void merge (list<map<string, object>> mainlist,
  String MainKey , list<map<string, object>> relationlist,
  String RelationKey, String prefix);

//merges 2 sets, finds the associated map first, and merges 2 separate map,2 map with the associated key is the same as 1
public static void merge (List <map<string, object>> mainlist,
  List<Map<String, Object>> relationlist, String relationkey);

Merges 2 sets, and the map in 2 sets is associated with a key
public static void Merge (List<map<string, object>> mainlist,
String Mainkey, list<map<string, object>> relationlist,
String Relationkey);

Merge 2 map, prefix prefix can not, not the case, key name, will overwrite/////////////////////

Merge 2 map,relation will be put in main, key all plus prefix
public static void Merge (Map<string, object> Mainmap,
Map<string, object> relation, String prefix);

Merges 2 maps, and if there are duplicates, the Relationmap overrides the one in main
public static void Merge (Map<string, object> Mainmap,
Map<string, object> Relationmap);

/////////////////////////look for a map in a set of associations map/////////////////////
/**
 * the target object from a collection, and if not found, returns NULL
 * 
 * @param 2 map common key
 */
public static map<string, object> Relationmap (map<string, object> mainmap,
  list<map<string, object>> List, String relationkey);

Found 1 map objects that need to be associated in the collection list, specifying their own key
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 process and base library of Java web development are still accumulating continuously.

Original starting: http://fansunion.cn/article/detail/540.html

Do not use Left-join, such as multi-table association query, only with single-table query and Java program, easy to achieve "multi-table query" effect

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.