HibernateCRUD basic framework (1)-entity class

Source: Internet
Author: User

The basic framework of HibernateCRUD consists of three articles, which mainly describe the ideas of the entire basic framework of CRUD.

1st: Describes the most basic entity classes that encapsulate some concepts in SQL.

2nd: Based on these entity classes, develop an "HQL statement constructor-HqlQueryBuilder ".

3rd: Construct a simple and complex standard crud api.


Advantages: Improves code reuse and coding efficiency.

Disadvantages: The performance is not considered too much. The specific performance is not tested.

Limited Functions, only for common and relatively simple CRUD functions.


Reminder: The comments have been clearly written and will not be explained too much.

If you have any questions, leave a message and take the time to reply.


Article 1st: entity class

1. constants and basic definitions

Package cn. fansunion. hibernate. SQL;/*** SQL keyword constant, such as "and, like, where", comparison constant, and <, =. *** @ Author LeiWen@FansUnion.cn */public class ConstantBase {/*** SQL keyword or */public static final String OR = "or "; /*** SQL keyword and */public static final String AND = "and";/*** SQL keyword from */public static final String FROM = "from "; /*** SQL keyword as */public static final String AS = "as";/*** SQL keyword where */public static final String WHERE = "where "; /*** SQL keyword asc */public static final S Tring ASC = "asc";/*** SQL keyword desc */public static final String DESC = "desc "; /*** SQL keyword in */public static final String IN = "in";/*** not in */public static final String NOT_IN = "not in "; /*** SQL keyword like */public static final String LIKE = "like";/*** not like */public static final String NOT_LIKE = "not like "; /*** order by */public static final String ORDER_BY = "order ";/** * Group by */public static final String GROUP_BY = "group by";/*** SQL keyword limit */public static final String LIMIT = "limit "; /*** COLON */public static final String COLON = ":";/*** COMMA */public static final String COMMA = ","; /*** a space */public static final String BLANK = "";/*** an EMPTY String */public static final String EMPTY = ""; /*****/public static final boolean AUTO_ADD = true ;/** * Right brackets */public static final String RIGHT_BRACKET = ")";/*** left brackets */public static final String LEFT_BRACKET = "("; /***** PERCENT */public static final String PERCENT = "%";/***** single quotes */public static final String SINGLE_QUOTATION_MARK = "'"; /***** equal sign */public static final String EQUALS = "=";/***** unequal */public static final String NOT_EQUALS = "! = ";/*** Greater than sign */public static final String GREAT_THAN ="> ";/*** less than sign */public static final String LESS_THAN =" <"; /*** greater than or equal to */public static final String GREAT_THAN_EQUALS = "> =";/*** less than or equal to */public static final String LESS_THAN_EQUALS = "<= "; //*************************************/ *****************//** * ******************************** public static final String *s_with_blank = buildBlank (EQUALS ); public static final String NOT_EQUALS_WITH_BLANK = buildBlank (NOT_EQUALS); public static final String Signature = buildBlank (GREAT_THAN); public static final String Signature = buildBlank (LESS_THAN ); public static final String Signature = buildBlank (GREAT_THAN_EQUALS); public static final String Signature = buildBlank (LESS_THAN_EQUALS); public static final String IN_WITH_BLANK = buildBlank (IN ); public static final String NOT_IN_WITH_BLANK = buildBlank (NOT_IN); public static final String LIKE_WITH_BLANK = buildBlank (LIKE); public static final String NOT_LIKE_WITH_BLANK = buildBlank (NOT_LIKE ); public static final String ORDER_BY_WITH_BLANK = buildBlank (ORDER_BY); public static final String GROUP_BY_WITH_BLANK = buildBlank (GROUP_BY); public static final String LIMIT_WITH_BLANK = buildBlank (LIMIT ); public static final String WHERE_WITH_BLANK = buildBlank (WHERE); public static final String AS_WITH_BLANK = buildBlank (); /*** return the String value corresponding to the variable ** @ param or * @ return true returns "or", false returns "and" */public static String isOr (Boolean or) {String str = AND; if (or) {str = OR;} return str;}/*** Add the percent sign "%" to the left of the String ", add the percent sign "%" ** @ param str * String * @ return to the right of the String a new String surrounded by "%" */public static String buildLike (Object str) {String newStr = PERCENT + str + PERCENT; return newStr ;} /*** Add a space on the left and right of a String ** @ param str * @ return new String */public static String buildBlank (Object str) {String newStr = BLANK + str + BLANK; return newStr ;} /*** add a single quotation mark "'" ** @ param str * string * @ return to the left and right of the string, a new string surrounded by "'' "*/public static string buildQuota (Object str) {String newStr = SINGLE_QUOTATION_MARK + str + SINGLE_QUOTATION_MARK; return newStr;}/*** add left brackets to the left of the String "(", add right brackets to the right of the String ") "** @ param str * String * @ return is a new String enclosed by" () "*/public static String buildBracket (Object str) {String newStr = LEFT_BRACKET + str + RIGHT_BRACKET; return newStr;} public static void println (Object object) {System. out. println (object);} public static void print (Object object) {System. out. print (object );}}

Package cn. fansunion. hibernate. SQL;/*** operator type, such as "=! ==>=<= ". ** @ Author LeiWen@FansUnion.cn */public enum Operator {EQUALS, NOT_EQUALS, GREAT_THAN, GREAT_THAN_EQUALS, LESS_THAN, LESS_THAN_EQUALS, LIKE, NOT_LIKE, IN, NOT_IN; /*** convert to a String (it is better to put TODO in a separate tool class) */public static String toString (Operator operator) {String str = ""; switch (operator) {case EQUALS: str = ConstantBase. EQUALS; break; case NOT_EQUALS: str = ConstantBase. NOT_EQUALS; break; case GREAT_THAN: str = ConstantBase. GREAT_THAN; break; case GREAT_THAN_EQUALS: str = ConstantBase. GREAT_THAN_EQUALS; break; case LESS_THAN: str = ConstantBase. LESS_THAN; break; case LESS_THAN_EQUALS: str = ConstantBase. LESS_THAN_EQUALS; break; case LIKE: str = ConstantBase. LIKE; break; case NOT_LIKE: str = ConstantBase. NOT_LIKE; break; case IN: str = ConstantBase. IN; break; case NOT_IN: str = ConstantBase. NOT_IN; break; default: break;} return str ;}}


public enum AndOr {    AND, OR}

2. entity class


Package cn. fansunion. hibernate. SQL. entity; import org. apache. commons. lang. stringUtils; import org. apache. commons. lang. text. strBuilder; import cn. fansunion. hibernate. SQL. constantBase;/*** From statement, such as "from User user ". ** @ Author LeiWen@FansUnion.cn */public class From extends ConstantBase {/*** class of the entity class, such as User. Class */private class
 ModelClazz;/*** String representation of the object Class, such as User */private String model;/*** alias, such as user */private String alias; public From () {super ();} public From (Class
 ModelClazz) {super (); this. modelClazz = modelClazz;} public From (Class
 ModelClazz, String alias) {super (); this. modelClazz = modelClazz; this. alias = alias;} public From (String model) {super (); this. model = model;} public From (String model, String alias) {super (); this. model = model; this. alias = alias;} // public String getModel () {return model;} public void setModel (String model) {this. model = model;} public Class
 GetModelClazz () {return modelClazz;} public void setModelClazz (Class
 ModelClazz) {this. modelClazz = modelClazz;} public String getAlias () {return alias;} public void setAlias (String alias) {this. alias = alias;}/*** convert to String */public String toString () {if (modelClazz! = Null) {this. model = modelClazz. getSimpleName ();} StrBuilder builder = new StrBuilder (); if (StringUtils. isNotEmpty (model) {builder. append (FROM ). append (BLANK ). append (modelClazz. getSimpleName (); if (StringUtils. isNotEmpty (alias) {builder. append (AS_WITH_BLANK ). append (alias) ;}} return builder. toString ();}}


Package cn. fansunion. hibernate. SQL. entity; import java. util. date; import cn. fansunion. hibernate. SQL. constantBase; import cn. fansunion. hibernate. SQL. operator;/*** one query condition. Consists of three parts: Key-operation-value, for example, "age> 12", "email = 'leiwen @ FansUnion.cn. ** @ Author LeiWen@FansUnion.cn */public class Condition extends ConstantBase {/*** Condition key */private String key;/*** Condition Operator */private operator Operator; /*** Condition value */private Object value; public Condition () {} public Condition (String key, Operator operator) {super (); this. key = key; this. operator = operator;} public Condition (String key, Object value) {this. key = key; this. value = value;} Public Condition (String key, Operator operator, Object value) {this. key = key; this. operator = operator; this. value = value;} public String getKey () {return key;} public void setKey (String key) {this. key = key;} public Operator getOperator () {return operator;} public void setOperator (Operator operator) {this. operator = operator;} public Object getValue () {return value;} public void s EtValue (Object value) {this. value = value;}/*** convert to String */public String toString () {String str = ""; String newValue = ""; // construct the complete statement if (value! = Null) {// whether to automatically add "%", "()" if (AUTO_ADD) {switch (operator) {case LIKE: case NOT_LIKE: newValue = buildLike (value); break; case IN: case NOT_IN: newValue = buildBracket (value); break; default: break ;} // type of quotation marks required: boolean isString = value instanceof String; boolean isDate = value instanceof Date; boolean isSqlDate = value instanceof java. SQL. date; if (isString | isDate | isSqlDate) {newValue = buildQuota (value);} else {newValue = value. toString () ;}}// construct the placeholder statement else {newValue = COLON + key;} // "name = a", "name like ", add a space str + = key + BLANK + Operator. toString (operator) + BLANK + newValue; return str ;}}


Package cn. fansunion. hibernate. SQL. entity; import java. util. arrayList; import java. util. list; import org. apache. commons. lang. text. strBuilder; import cn. fansunion. hibernate. andOr; import cn. fansunion. hibernate. SQL. constantBase; import cn. fansunion. hibernate. util. emptyUtils;/*** conditions that logically belong to one group. ** @ Author LeiWen@FansUnion.cn */public class GroupCondition extends ConstantBase {/*** a set of multiple conditions */private List
 
  
ConditionList;/*** relationship between conditions in the group: and | or, default: And */private List
  
   
RelationList; public static final Boolean AND = true; public static final Boolean OR = false; public GroupCondition () {conditionList = new ArrayList
   
    
(); RelationList = new ArrayList
    
     
();} Public void addCondition (Condition condition) {addCondition (condition, true);} public void addCondition (Condition condition, boolean or) {conditionList. add (condition); if (EmptyUtils. notEmpty (conditionList) {relationList. add (or) ;}} public void addCondition (Condition condition, AndOr ao) {conditionList. add (condition); if (EmptyUtils. notEmpty (conditionList) {if (ao = AndOr. AND) {relationList. add (AND);} else {relationList. add (OR) ;}} public void addOr (int index, boolean or) {relationList. set (index, or);}/*** convert to String */public String toString () {if (EmptyUtils. isEmpty (conditionList) {return EMPTY;} StrBuilder builder = new StrBuilder (); int size = conditionList. size (); for (int index = 0; index <size; index ++) {if (index = 0) {builder. append (conditionList. get (index);} else {builder. append (BLANK ). append (isOr (relationList. get (index ))). append (BLANK ). append (conditionList. get (index) ;}} return builder. toString ();}}
    
   
  
 

Package cn. fansunion. hibernate. SQL. entity; import java. util. arrayList; import java. util. list; import org. apache. commons. lang. text. strBuilder; import cn. fansunion. hibernate. SQL. constantBase; import cn. fansunion. hibernate. util. emptyUtils;/*** Search Condition (between two groups, and | or .) ** @ Author LeiWen@FansUnion.cn */public class SearchCondition extends ConstantBase {/*** a set of multiple group conditions */private List
 
  
GroupConditionList;/*** relationship between multiple group conditions: and | or, default: And */private List
  
   
And; public SearchCondition () {groupConditionList = new ArrayList
   
    
(); And = new ArrayList
    
     
();} Public void addGroupCondition (GroupCondition groupCondition) {addGroupCondition (groupCondition, false);} public void addGroupCondition (GroupCondition groupCondition, boolean or) {groupConditionList. add (groupCondition); if (EmptyUtils. notEmpty (groupConditionList) {and. add (or) ;}} public void addOr (int index, boolean or) {and. set (index, or);} public List
     
      
GetGroupConditionList () {return groupConditionList;} public void setGroupConditionList (List
      
        GroupConditionList) {this. groupConditionList = groupConditionList;} public List
       
         GetAnd () {return and;} public void setAnd (List
        
          And) {this. and = and;}/*** convert to String */public String toString () {if (EmptyUtils. isEmpty (groupConditionList) {return EMPTY;} StrBuilder builder = new StrBuilder (); int size = groupConditionList. size (); for (int index = 0; index <size; index ++) {// conditions of one group are wrapped with () if (index = 0) {builder. append (WHERE_WITH_BLANK ). append (buildBracket (groupConditionList. get (index);} else {builder. append (BLANK ). append (isOr (and. get (index ))). append (BLANK ). append (buildBracket (groupConditionList. get (index) ;}} return builder. toString ();}}
        
       
      
     
    
   
  
 

Package cn. fansunion. hibernate. SQL. entity; import org. apache. commons. lang. stringUtils; import org. apache. commons. lang. text. strBuilder; import cn. fansunion. hibernate. SQL. constantBase;/*** sort statement, such as "order by id desc ". ** @ Author LeiWen@FansUnion.cn */public class Order extends ConstantBase {/*** sort statement key */private String key; /*** sort statement in ascending or descending order (String form) */private String asc;/*** sort statement in ascending or descending order (Boolean form) */private Boolean isAsc; public Order () {} public Order (String key, String asc) {this. key = key; this. asc = asc;} public Order (String key, Boolean isAsc) {super (); this. key = key; this. isAsc = isAsc;}/*** convert to String */public String toString () {String finalSort = DESC; if (StringUtils. isNotEmpty (asc) {finalSort = asc;} else {if (isAsc) {finalSort = ASC ;}} StrBuilder builder = new StrBuilder (); builder. append (key ). append (BLANK ). append (finalSort); return builder. toString ();}}

Package cn. fansunion. hibernate. SQL. entity; import java. util. arrayList; import java. util. list; import org. apache. commons. lang. text. strBuilder; import cn. fansunion. hibernate. SQL. constantBase; import cn. fansunion. hibernate. util. listUtils;/*** sorting group, such as "order by id desc, name asc ". ** @ Author LeiWen@FansUnion.cn */public class OrderGroup extends ConstantBase {/*** a set of multiple sorting forms */private List
 
  
OrderList; public OrderGroup () {orderList = new ArrayList
  
   
();} Public OrderGroup (Order order) {this (); orderList. add (order) ;}public OrderGroup (Order order1, Order order2) {this (); orderList. add (order1); orderList. add (order2);} public void addOrder (Order order) {orderList. add (order) ;}/ *** convert to String */public String toString () {StrBuilder builder = new StrBuilder (); if (ListUtils. notEmpty (orderList) {String orders = ListUtils. list2String (orderList); builder. append (ORDER_BY_WITH_BLANK ). append (orders);} return builder. toString ();}}
  
 

Package cn. fansunion. hibernate. SQL. entity; import java. util. arrayList; import java. util. list; import cn. fansunion. hibernate. SQL. constantBase; import cn. fansunion. hibernate. util. emptyUtils; import cn. fansunion. hibernate. util. listUtils;/*** grouping statement, such as "group by id, name ". ** @ Author LeiWen@FansUnion.cn */public class GroupBy extends ConstantBase {/*** the set of field names in the group does not support having clauses */private List
 
  
Group; public GroupBy () {group = new ArrayList
  
   
();} Public List
   
    
GetGroup () {return group;}/*** convert to String */public String toString () {if (EmptyUtils. notEmpty (group) {String groupStr = ListUtils. list2String (group); return GROUP_BY_WITH_BLANK + groupStr;} return EMPTY;} public void addGroup (String g) {this. group. add (g );}}
   
  
 

Original article link: Http://blog.fansunion.cn/articles/3616 (Xiao Lei blog-blog.fansunion.cn)

Related Article

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.