在看了大神们写的代码,人家写的就是又质量,通过看他们的代码,自己也长了一些经验,比如我们在写SQL语句的时候,我们会定义一个变量比如String str = "select * from XXX";这样来写,在代码中,就会有那么长的一句话,看起来很不舒服,然而怎么才能写的代码更加漂亮,看上去第一眼不知道是干什么的,嘿嘿 就是封装起来,就是把一些把一些常用的sql语句封装到一个常量中然后调用这个常量就可以了,如果还有其他的条件的,就在where后面写上一个1=1就可以。
请看代码:
public class SqlConst { public static final String and = "and"; public static final String select = "select"; public static final String where = "where"; public static final String from = "from"; public static final String order_by = "order by"; public static final String group_by = "group by"; public static final String desc = "desc"; public static final String range = "range"; public static final String range_year = "year"; public static final String range_month = "month"; public static final int function_count = -1; public static final String OPPORTUNITY_SQL_BEGINNING = "select opportunity from Opportunity opportunity where 1=1 "; public static final String QUOTATION_SQL_BEGINNING = "select quotation from Quotation quotation where 1=1 "; public static final String MARKET_SQL_BEGINNING = "select market from Market market where 1=1 "; public static final String ACTIVITY_SQL_BEGINNING = "select activity from Activity activity where 1=1 "; public static final String CUSTOMER_SQL_BEGINNING = "select customer from Customer customer where 1=1 "; public static final String CONTACT_SQL_BEGINNING = "select contact from Contact contact where 1=1 "; public static final String CLUE_SQL_BEGINNING = "select clue from Clue clue where 1=1 "; public static final String PRODUCT_SQL_BEGINNING = "select product from Product product where 1=1 "; public static final String CONTRACT_SQL_BEGINNING = "select contract from Contract contract where 1=1 "; }
是不是看上去这样写的代码很有水平,在你调用的地方直接类名就可以调用到了,大神们写的代码没有多么难,但是人家写的就是规范,让人看上去,第一眼不知道是干什么的,这就是封装。
同样的下面是对表名称的封装,数据库中有几张表就在一个类中封装几个常量,方便后面的编码。
package com.ebizwindow.crm.constants; public class TableConst { public static final String CRM_Customer = "CRM_Customer"; public static final String CRM_Contact = "CRM_Contact"; public static final String CRM_Market = "CRM_Market"; public static final String CRM_Clue = "CRM_Clue"; public static final String CRM_Opportunity = "CRM_Opportunity"; public static final String CRM_Quotation = "CRM_Quotation"; public static final String CRM_Order = "CRM_Order"; public static final String CRM_Activity = "CRM_Activity"; public static final String CRM_Product = "CRM_Product"; public static final String CRM_Contract = "CRM_Contract"; public static final String CRM_PriceBook = "CRM_PriceBook"; public static final String CRM_RPLAN = "CRM_ReceivablesPlan"; public static final String CRM_RRECORD = "CRM_ReceivablesRecord"; public static final String CUSTOMER = "customer"; public static final String CONTACT = "contact"; public static final String MARKET = "market"; public static final String CLUE = "clue"; public static final String QUOTATION = "quotation"; public static final String OPPORTUNITY = "opportunity"; public static final String ORDER = "order_"; public static final String CONTRACT = "contract"; public static final String ACTIVITY = "activity"; public static final String ACTIVITY_HISTORY = "activityHistory"; public static final String PRODUCT = "product"; public static final String PRICEBOOK = "priceBook"; public static final String RPLAN = "receivablesPlan"; public static final String RRECORD = "receivablesRecord"; public static final String REPORT = "report"; public static final String DEPARTMENT = "department"; public static final String OPERATOR = "operator"; public static final String OPERATOR_Operator = "OPERATOR_Operator"; public static final String OPERATOR_Department = "OPERATOR_Department"; }
时间: 2024-11-25 10:31:17