Enumeration parameter passing, enumeration usage, and parameter passing enumeration usage
By NiceCui
- This document is not reposted. If you need to reposted, you must obtain the author's consent. Thank you.
- Link: http://www.cnblogs.com/NiceCui/p/7835122.html
- Email: moyi@moyibolg.com
- Date:
It is also common to pass enumeration as parameters in complex service calls. Large Internet companies will write a lot of descriptions and extensions of the entity content in their own services, the logic of querying or displaying with enumeration is clear and can be reused for convenient modification. nested enumeration in Enumeration
In our database design, we often replace some types with numerical symbols. When the number of types is not large or the complexity is not so high, the associated enumeration will be created.
How can we use this enumeration? When we use the velocity engine template or other front-end presentation layer templates, we will pass the detected object to the view layer,
At this time, if we use numbers or letters to replace a certain type of field, we will look quite troublesome if we use loop comparison, in this case, we can pass the corresponding enumerated object. In this case, we only need to put the corresponding field
You can obtain the real name that we need to replace the symbol.
Simple Example:
1. A simple enumeration class
1 package com. blog. niceCui. enum; 2 3/** 4*5 * @ author NiceCui 6 * @ date 2017-11-14 7 * @ cnblog address: http://www.cnblogs.com/NiceCui/ 8*9 */10 public enum BusinessEnum {11 12 Order (2, "Order business"), 13 User (3, "User business"); 14 15 private int type; 16 private String name; 17 18 private BusinessEnum (int type, String name) {19 this. type = type; 20 this. name = name; 21} 22 public int getType () {23 return type; 24} 25 public void setType (int type) {26 this. type = type; 27} 28 public String getName () {29 return name; 30} 31 public void setName (String name) {32 this. name = name; 33} 34 35 public static BusinessEnum getEnumByType (int type) {36 for (BusinessEnum bt: values () {37 if (bt. type = type) {38 return bt; 39} 40} 41 return null; 42} 43 44}
2. Write a simple object
1 package com. blog. niceCui. entity; 2 3 4/** 5*6 * @ author NiceCui 7 * @ date 2017-11-14 8 * @ cnblog address: http://www.cnblogs.com/NiceCui/ 9*10 */11 public class Business {12 13 // id14 private int id; 15 // Business type id16 private long typeId; 17 // user id18 private long userId; 19 // User Name 20 private String userName; 21 22 23 public Business () {24 super (); 25 // TODO Auto-generated constructor stub26} 27 public Business (int id, long typeId, long userId, String userName) {28 super (); 29 this. id = id; 30 this. typeId = typeId; 31 this. userId = userId; 32 this. userName = userName; 33} 34 public int getId () {35 return id; 36} 37 public void setId (int id) {38 this. id = id; 39} 40 public long getTypeId () {41 return typeId; 42} 43 public void setTypeId (long typeId) {44 this. typeId = typeId; 45} 46 public long getUserId () {47 return userId; 48} 49 public void setUserId (long userId) {50 this. userId = userId; 51} 52 public String getUserName () {53 return userName; 54} 55 public void setUserName (String userName) {56 this. userName = userName; 57} 58 @ Override59 public String toString () {60 return "Business [id =" + id + ", typeId =" + typeId + ", userId = "+ userId +", userName = "+ userName +"] "; 61} 62 63 64 65}
3. Write a simple interface and implement only simple Simulation
Package com. blog. niceCui. service; import com. blog. niceCui. enum. businessEnum; import com. blog. niceCui. entity. business;/***** @ author NiceCui * @ date 2017-11-14 * @ cnblog address: http://www.cnblogs.com/NiceCui/ **/public interface BusinessService {/*** @ describe here is just a simple simulation of the Service * @ param id * @ return * // * Through the corresponding enumeration to obtain the corresponding business name */public String getByEnum (BusinessEnum bt, business business);/* obtain the corresponding object through id */public Business getById (long id );}
Package com. blog. niceCui. service; import com. blog. niceCui. enum. businessEnum; import com. blog. niceCui. entity. business;/**** @ author NiceCui * @ date 2017-11-14 * @ cnblog address: http://www.cnblogs.com/NiceCui/ **/public class BusinessServiceImpl implements BusinessService {public Business getById (long id) {// TODO Auto-generated method stub return null;} public String getByEnum (BusinessEnum bt) {// TODO Auto-generated method stub return bt. getName ();}}
4. Output
1 public static void main (String [] args) {2 3 // int id, long typeId, long userId, String userName 4 Business business = new Business (1, 2L, 3L, "hello enum"); 5 6 BusinessService businessService = new BusinessServiceImpl (); 7 8 9 String aa = businessService. getByEnum (BusinessEnum. getEnumByType (business. getTypeId (); 10 11 System. out. println ("corresponding business:" + aa); 12}
5. If we call the front-end page:
<div class="form-group col-xs-12 col-sm-6 col-md-4"> $!{BusinessEnum.
getEnumByType(business.getTypeId())}: $!{business.userName}
</div>
6. The enumeration is transmitted using the interactive box as a parameter, and the entity. class needs to be enumerated:
1Beat. getModel (). add ("CardTypeEnum", CardTypeEnum. class); // enumeration of Document Type levels