Java generics for similar applications in spring JDBC template

Source: Internet
Author: User

The use of generics guarantees that the returned object type is correct;

 Packagecom.stono.gentest;Importjava.util.ArrayList;Importjava.util.List; Public classStoryteller { Public Static voidMain (string[] args) {List<Minstrel> story = storytemplate.getstory ("select",                NewStoryi<minstrel>() {@Override PublicMinstrel Gethero (String name) {return NewMinstrel (name); }                }, 1);  for(Minstrel minstrel:story) {System.out.println (Minstrel.getname ()); }    }}classMinstrel {PrivateString name;  PublicMinstrel (String name) { This. Name =name; }     PublicString GetName () {returnname; }}//================== Below is the pseudo-frame part, the above is the pseudo-client part; =====================//The template method creates generic objects according to the rules of the interface, and the logic of assembling them according to the conditions does not need to be written by the client;classStorytemplate { Public Static<T> list<t> getstory (String str, storyi<t>T, Object ... o) {List<T> list =NewArraylist<t>(); if("select". Equals (str)) {//a Query method similar to JDBC;String[] STRs = {"A", "B" };  for(String string:strs) {List.add (T.gethero (string)); }        }        returnlist; }}InterfaceStoryi<t>{T Gethero (String name);//passing in a parameter string guarantees that a generic object is returned, and the assembly logic is implemented by the client;}

Java generics for similar applications in spring JDBC template

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.