简体   繁体   中英

How to call multi table insert in myBatis mapper?

I am using myBatis 3.2.x and have run into a scenario where I need to do multiple table inserts in one database trip,

I was wondering if I can create a master INSERT sql mapper file which would call these multi table inserts and save me network trips

I am consuming JSON objects from a EMS server and my Turn around time is a bit higher then required.

All suggestions and hints are welcome.

Thanks VR

Use Collections.sort() to sort and use a simple for cycle to catch doubles, eg:

Collections.sort(myList);
A previous = null;
for (A elem: myList) {
    if (elem.compareTo(previous) == 0) {
        System.err.println("Duplicate: "+elem);
    }
    previous = elem;
}

Assuming that the Comparable is consistent with the equals implementation, you can use a Set. You can add each element to a Set using Set.add(..) and use the return value of add to determine if the value was already present in the Set and create either a Set or a List to return.

Note: If you need each duplicate returned only once, you can change the return list to a set.

 List<A> duplicates(List<A> myList) {

  Set<A> s = new HashSet<A>();
  List<A> duplicates = new ArrayList<A>(); // change to using a Set if you want to report each duplicate item only once. 
  for (A item: myList) {
    if (!s.add(item)) { 
      duplicates.add(item);
    }
  }
  return duplicates;
 }

An improved version using sorting (to report duplicate elements only once, I assume there are no null values in the list):

Collections.sort(myList);
A previous = null, elem = null;
for (java.util.Iterator<A> it = myList.iterator; it.hasNext(); elem = it.next()) {
    if (elem.compareTo(previous) == 0) {
        System.err.println("Duplicate: "+elem);
        while (it.hasNext() && (elem = it.next()).compareTo(previous)) {
           //loop through other values
        }
    }
    previous = elem;
}

A version using SortedSet (probably this is faster a bit): and corrected the same

SortedSet<A> set = new TreeSet<>(), duplicates = new TreeSet<>();
for (A a: myList) {
    if (!set.add(a)) {
        duplicates.add(a);
    }
}
return duplicates;//or for (A a: duplicates) System.println("Duplicate: " + a);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM