码迷,mamicode.com
首页 > 其他好文 > 详细

枚举传参,枚举使用详解

时间:2017-11-15 22:11:50      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:card   ora   nts   logs   ==   order   model   rate   span   

作者:NiceCui

  • 本文谢绝转载,如需转载需征得作者本人同意,谢谢。
  • 本文链接:http://www.cnblogs.com/NiceCui/p/7835122.html
  • 邮箱:moyi@moyibolg.com
  • 日期:2017-11-14 22:26

将枚举作为参数传递在复杂的服务调用中也是很常见的,大型互联网公司中都会写很多对本身服务中实体内容的描述和扩展,使用枚举去查询或者去展示都会显得逻辑很清楚,而且能够重复利用,修改非常方便,枚举中嵌套枚举

在我们的数据库设计中很多时候都会把一些类型什么的使用数字符号代替,当类型数量不是很多,或者复杂度没有那么高的情况下,都会在创建相关联的枚举。

这样的枚举怎么使用呢,当我们的使用velocity引擎模板或者其他的前端展示层模板的时候,我们会把查出的实体对象传递到视图层,

这时候如果有数字或者字母代替某个类型的字段我们如果使用循环比较的话就会看起来相当的麻烦,这个时候我们可以将对应的枚举对象传递过去这个时候我们只需要把对应字段放到

枚举中就可以了,就会得到我们得到代替符号背后我们需要的真实名字。

简单例子:

1、先来一个简单的枚举类

 1 package com.blog.NiceCui.Enum;
 2 
 3 /**
 4  * 
 5  * @author NiceCui
 6  * @date 2017-11-14
 7  * @cnblog 地址:http://www.cnblogs.com/NiceCui/
 8  *
 9  */    
10 public enum BusinessEnum {
11            
12         Order(2,"订单业务"),
13         User(3,"用户业务");
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、写一个简单的实体对象

 1 package com.blog.NiceCui.entity;
 2 
 3 
 4 /**
 5  * 
 6  * @author NiceCui
 7  * @date 2017-11-14
 8  * @cnblog 地址:http://www.cnblogs.com/NiceCui/
 9  *
10  */
11 public class Business {
12     
13     //id
14     private int id;
15     //业务类型id
16     private long typeId;
17     //用户id
18     private long userId;
19     //用户姓名
20     private String userName;
21     
22     
23     public Business() {
24         super();
25         // TODO Auto-generated constructor stub
26     }
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     @Override
59     public String toString() {
60         return "Business [id=" + id + ", typeId=" + typeId + ", userId=" + userId + ", userName=" + userName + "]";
61     }
62     
63     
64 
65 }

 3、写一个简单的接口和实现只做简单的模拟

package com.blog.NiceCui.service;

import com.blog.NiceCui.Enum.BusinessEnum;
import com.blog.NiceCui.entity.Business;

/**
 * 
 * @author NiceCui
 * @date 2017-11-14
 * @cnblog 地址:http://www.cnblogs.com/NiceCui/
 *
 */
public interface BusinessService {
    
    
    /**
     * @describe 这里只是一个简单的模拟一下服务
     * @param id
     * @return
     */
    
    /*通过对应的枚举得到对应的业务名字*/
    public String getByEnum(BusinessEnum bt,Business business);
    
    /*通过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 地址: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、输出

 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("对应的业务是:"+aa);
12     }
    

技术分享

5、如果我们前端页面调用:

<div class="form-group col-xs-12 col-sm-6 col-md-4">
   $!{BusinessEnum.
getEnumByType(business.getTypeId())}: $!{business.userName}
</div>

 6、枚举使用交互框当做参数传递,需要枚举实体.class:

1 beat.getModel().add("CardTypeEnum",CardTypeEnum.class);//证件类型等级枚举

 

枚举传参,枚举使用详解

标签:card   ora   nts   logs   ==   order   model   rate   span   

原文地址:http://www.cnblogs.com/NiceCui/p/7835122.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!