In web projects, if the front-end MVC framework uses springmvc, you can use map to receive front-end request parameters, which is much easier than bean.
Especially when springmvc and mybatis are used together, using map greatly reduces the need for beans, Vo, Po, and other things.
You may also encounter a problem with map, that is, there are a lot of code for type conversion.
/**
* Obtain the page number of the current page.
*/
Private int getcurrentpage (Map <string, Object> Params ){
Return integer. parseint (Params. Get (pageutils. current_page). tostring ());
}
Obtain a value of the object type from the map, and convert it to another type such as the target integer.
Not concise enough. Even if the tool method is extracted, the code is still a little complicated.
Therefore, I encapsulate map once. (Someone has actually done this. I was inspired by others and decided to encapsulate it. However, I need to be concise enough to meet my preferences .)
Usage
@ Requestmapping (value = "/List ")
@ Responsebody
Public pagevo list (@ requestparamTypedmap<String, Object> Params ){
Integer age = Params. getint ("Age ");
String name = Params. getstring ("name ");
}
Typedmap code implementation
Package CN. fansunion. Common. collection;
Import java. util. date;
Import java. util. hashmap;
Import org. Apache. commons. lang3.stringutils;
Import CN. fansunion. Common. converter. integers;
Import CN. fansunion. Common. converter. Longs;
Import CN. fansunion. Common. util. timeutil;
/**
* Adds tools and methods such as getint and getlong Based on map.
*
* @ Author leiwen
*
*/
Public classTypedmap <K, V>Extends hashmap <K, V> {
Private Static final long serialversionuid = 1l;
/**
* Obtain an integer value from the set.
*
* @ Param key
* Key
* @ Param defaultvalue
* Default value. If the corresponding value is not found based on the key, use this default value.
* @ Return the value corresponding to the key or defaultvalue
*/
Public integer getint (string key, integer defaultvalue){
Object v = get (key );
Integer result = integers. valueof (v, defaultvalue );
Return result;
}
/**
* Obtain integer values based on keys.
*
* @ Param key
* Key
* @ Return the value corresponding to the key. If no value is found, null is returned.
*/
Public integerGetint(String key ){
Return getint (Key, null );
}
/**
* Obtain a value of the string type from the collection.
*
* @ Param key
* Key
* @ Param defaultvalue
* Default value. If the corresponding value is not found based on the key, use this default value.
* @ Return the value corresponding to the key or defaultvalue
*/
Public StringGetstring(String key, string defaultvalue ){
Object value = get (key );
If (value = NULL ){
Return defaultvalue;
}
String result = defaultvalue;
If (value instanceof string ){
Result = (string) value;
} Else {
Result = string. valueof (value );
}
Return result;
}
/**
* Obtain the string type value based on the key.
*
* @ Param key
* Key
* @ Return the value corresponding to the key. If no value is found, null is returned.
*/
Public StringGetstring(String key ){
Return getstring (Key, null );
}
/**
* Obtain the long type value based on the key.
*
* @ Param key
* Key
* @ Return the value corresponding to the key. If no value is found, null is returned.
*/
Public longGetlong(String key ){
Return getlong (Key, null );
}
/**
* Obtain a long value from the collection.
*
* @ Param key
* Key
* @ Param defaultvalue
* Default value. If the corresponding value is not found based on the key, use this default value.
* @ Return the value corresponding to the key or defaultvalue
*/
Public longGetlong(String key, long defaultvalue ){
Return longs. valueof (get (key), defaultvalue );
}
Public BooleanGetboolean(String key, Boolean defaultvalue ){
Object value = get (key );
If (value = NULL ){
Return defaultvalue;
}
If (Boolean. Class. isinstance (value )){
Return (Boolean) value;
} Else {
Return defaultvalue;
}
}
/**
* Obtain a Boolean value based on the key.
*
* @ Param key
* Key
* @ Return the value corresponding to the key. If no value is found, null is returned.
*/
Public BooleanGetboolean(String key ){
Return getboolean (Key, null );
}
/**
* Obtain a value of the date type from the collection.
*
* @ Param key
* Key
* @ Return the value corresponding to the key. If no value is found, null is returned.
*/
Public dateGetdate(String key ){
Return getdate (Key, null );
}
/**
* Obtain a value of the date type from the collection.
*
* @ Param key
* Key
* @ Param defaultvalue
* Default value. If the corresponding value is not found based on the key, use this default value.
* @ Return the value corresponding to the key or defaultvalue
*/
Public dateGetdate(String key, date defualtvalue ){
Object value = get (key );
If (value = NULL ){
Return NULL;
}
If (value instanceof date ){
Return (date) value;
}
Else {
String STR = value. tostring ();
If (stringutils. isnotempty (STR )){
Return timeutil. parseymdhms (key );
}
}
Return defualtvalue;
}
/**
* Obtain the value of the target type based on the key.
*
* @ Param key
* Key
* @ Param type
* Target type
* @ Return the value corresponding to the key. If no value is found, null is returned.
*/
Public <t> TGettypedobject(String key, T type ){
Object value = get (key );
If (value = NULL ){
Return NULL;
}
Return (t) value;
}
}
Typedmap unit test code
Package CN. fansunion. Common. collection;
Import java. util. date;
Import JUnit. Framework. testcase;
Import org. JUnit. test;
/**
* @ Author leiwen
*/
Public classTypedmaptestExtends testcase {
@ Test
Public static voidTestgetint(){
Typedmap <string, Object> map = createmap ();
Integer value = 198962;
Map. Put ("intkey", value );
Assertequals (value, map. getint ("intkey "));
Assertnull (Map. getint ("notexistintkey "));
}
@ Test
Public static voidTestgetintwithdefaultvalue(){
Typedmap <string, Object> map = createmap ();
Integer defaultvalue = 0;
Integer value = 198962;
Map. Put ("intkey", value );
Assertequals (value, map. getint ("intkey", defaultvalue ));
Assertequals (defaultvalue, map. getint ("notexistintkey", defaultvalue ));
}
@ Test
Public static voidTestgetlong(){
Typedmap <string, Object> map = createmap ();
Long value = 198962l;
Map. Put ("longkey", value );
Assertequals (value, map. getlong ("longkey "));
Assertnull (Map. getlong ("notexistlongkey "));
}
@ Test
Public static voidTestgetlongwithdefualtvalue(){
Typedmap <string, Object> map = createmap ();
Long defaultvalue = 0l;
Long value = 198962l;
Map. Put ("longkey", value );
Assertequals (value, map. getlong ("longkey", defaultvalue ));
Assertequals (defaultvalue, map. getlong ("notexistlongkey", defaultvalue ));
}
@ Test
Public static voidTestgetstring(){
Typedmap <string, Object> map = createmap ();
String value = "http://FansUnion.cn ";
Map. Put ("stringkey", value );
Assertequals (value, map. getstring ("stringkey "));
Assertnull (Map. getstring ("notexiststringkey "));
}
@ Test
Public static voidTestgetstringwithdefualtvalue(){
Typedmap <string, Object> map = createmap ();
String defaultvalue = "leiwen ";
String value = "http://FansUnion.cn ";
Map. Put ("stringkey", value );
Assertequals (value, map. getstring ("stringkey", defaultvalue ));
Assertequals (defaultvalue,
Map. getstring ("notexiststringkey", defaultvalue ));
}
@ Test
Public static voidTestgetdate(){
Typedmap <string, Object> map = createmap ();
Date value = new date ();
Map. Put ("datekey", value );
Assertequals (value, map. getdate ("datekey "));
Assertnull (Map. getdate ("notexistdatekey "));
}
@ Test
Public static voidTestgetdatewithdefualtvalue(){
Typedmap <string, Object> map = createmap ();
Date defaultvalue = NULL;
Date value = new date ();
Map. Put ("datekey", value );
Assertequals (value, map. getdate ("datekey", defaultvalue ));
Assertequals (defaultvalue, map. getdate ("notexistdatekey", defaultvalue ));
}
@ Test
Public static voidTestgetboolean(){
Typedmap <string, Object> map = createmap ();
Boolean value = false;
Map. Put ("booleankey", value );
Assertequals (value, map. getboolean ("booleankey "));
Assertnull (Map. getboolean ("notexistbooleankey "));
}
@ Test
Public static voidTestgetbooleanwithdefaultvalue(){
Typedmap <string, Object> map = createmap ();
Boolean defaultvalue = false;
Boolean value = true;
Map. Put ("booleankey", value );
Assertequals (value, map. getboolean ("booleankey", defaultvalue ));
Assertequals (defaultvalue,
Map. getboolean ("notexistbooleankey", defaultvalue ));
}
@ Test
Public static voidTestgettypedobject(){
Typedmap <string, Object> map = createmap ();
Integer value = 198962;
Map. Put ("intkey", value );
Assertequals (value, map. gettypedobject ("intkey", integer. Class ));
}
Public static typedmap <string, Object>Createmap(){
Return new typedmap <string, Object> ();
}
}
For more information, see improving productivity: using Extended Data Type typedmap to receive Web Request Parameters in springmvc
Related reading:
Improve productivity