com.github.drinkjava2.jsqlbox
Class ActiveRecord<T>

java.lang.Object
  extended by com.github.drinkjava2.jsqlbox.ActiveRecord<T>
All Implemented Interfaces:
ActiveRecordSupport<T>

public class ActiveRecord<T>
extends Object
implements ActiveRecordSupport<T>

Entity class extended from ActiveRecord or implements ActiveRecordSupport interface will get CRUD methods, see below difference in jSqlBox to save ActiveRecord entity and normal entity(POJO) into database:

 ActiveRecord style:   
 
    SqlBoxContext ctx=new SqlBoxContext(dataSource);
    SqlBoxContext.setDefaultContext(ctx);           
    entity.insert(); 
 
    or 
    
    SqlBoxContext ctx=new SqlBoxContext(dataSource);
    entity.useContext(ctx);
    entity.insert();
    
    or 
    SqlBoxContext ctx=new SqlBoxContext(dataSource);
    entity.insert(ctx);
    
    
 Data Mapper style (for POJO entity):   
    SqlBoxContext ctx=new SqlBoxContext(dataSource);
    ctx.insert(entity);
    
    or 
    
    SqlBoxContext ctx=new SqlBoxContext(dataSource);
    someOtherCtx.insert(entity, ctx);
 
 

Since:
1.0.0
Author:
Yong Zhu

Field Summary
 
Fields inherited from interface com.github.drinkjava2.jsqlbox.ActiveRecordSupport
lastTimePutFieldsCache
 
Constructor Summary
ActiveRecord()
           
 
Method Summary
 SqlItem bind(Object... parameters)
          For tXxxx style templateEngine use, return a SqlItemType.PUT type SqlItem instance, Usage: put("key1",value1,"key2",value2...);
 int countAll(Object... optionItems)
          Return how many records for current entity class
protected  void crudMethods__________________()
           
 SqlBoxContext ctx(Object... optionItems)
           
 void delete(Object... optionItems)
          Delete entity in database, if not 1 row deleted, throw SqlBoxException
 void deleteById(Object id, Object... optionItems)
          Delete entity by given id, if not 1 row deleted, throw SqlBoxException
 int deleteByIdTry(Object id, Object... optionItems)
          Delete entity by given id, return how many rows deleted
 int deleteTry(Object... optionItems)
          Delete entity in database, return how many rows affected
 boolean exist(Object... optionItems)
          Check if entity exist by its id
 boolean existById(Object id, Object... optionItems)
          Check if entity exist by given id
 List<T> findAll(Object... optionItems)
          Find all entity of same entity class, if not found, return empty list
 List<T> findByIds(Iterable<?> ids, Object... optionItems)
          Find all entity according its id, if not found, return empty list
 List<T> findBySample(Object sampleBean, Object... optionItems)
          Find entity according a sample bean, ignore null fields, if not found, return empty list
 List<T> findBySQL(Object... optionItems)
          Find entity according SQL, if not found, return empty list
<E> E
findOneRelated(Object... optionItems)
          Find one related entity
<E> List<E>
findRelatedList(Object... optionItems)
          Find related entity List
<E> Map<Object,E>
findRelatedMap(Object... optionItems)
          Find related entity Map
<E> Set<E>
findRelatedSet(Object... optionItems)
          Find related entity Set
<U> U
guess(Object... params)
          In SqlMapper style, based on current method @Sql annotated String or Text(see user manual) in comments(need put Java file in resources folder, see user manual) and parameters, guess a best fit query/update/delete/execute method and run it
 PreparedSQL guessPreparedSQL(Object... params)
          In SqlMapper style, return current method's prepared SQL
 String guessSQL()
          In SqlMapper style, return current method's SQL String based on current method @Sql annotated String or Text(see user manual) in comments(need put Java file in resources folder, see user manual)
 T insert(Object... optionItems)
          Insert entity to database, if not 1 row updated, throw SqlBoxException
 T load(Object... optionItems)
          Load entity according its id, if not 1 row round, throw SqlBoxException
 T loadById(Object id, Object... optionItems)
          Load entity by given id, if not 1 row found, throw SqlBoxException
 T loadByIdTry(Object id, Object... optionItems)
          Load entity by given id, if not found, return null
 int loadTry(Object... optionItems)
          Load entity according its id, return how many rows found
protected  void miscMethods__________________()
           
 T put(Object... fieldAndValues)
          Link style set values for entity field, format like: user.put("id","id1").put("name","Sam").put("address","Beijing","phone","12345", "email","abc@123.com")
 T putFields(String... fieldNames)
          Cache a field array in ThreadLocal for putValues method use
 T putValues(Object... values)
          Put values for entity fields, field names should be cached by call putFields method first
 SqlBoxContext shardDB(Object... optionItems)
          Return current entity's shardDatabase according its sharding key values
 String shardTB(Object... optionItems)
          Return current entity's shardTable according its sharding key values
 T update(Object... optionItems)
          Update entity in database, if not 1 row updated, throw SqlBoxException
 int updateTry(Object... optionItems)
          Update entity in database, return how many rows affected
 T useContext(SqlBoxContext ctx)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ActiveRecord

public ActiveRecord()
Method Detail

ctx

public SqlBoxContext ctx(Object... optionItems)
Specified by:
ctx in interface ActiveRecordSupport<T>
Returns:
current SqlBoxContext instance

useContext

public T useContext(SqlBoxContext ctx)

crudMethods__________________

protected void crudMethods__________________()

insert

public T insert(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Insert entity to database, if not 1 row updated, throw SqlBoxException

Specified by:
insert in interface ActiveRecordSupport<T>

update

public T update(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Update entity in database, if not 1 row updated, throw SqlBoxException

Specified by:
update in interface ActiveRecordSupport<T>

updateTry

public int updateTry(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Update entity in database, return how many rows affected

Specified by:
updateTry in interface ActiveRecordSupport<T>

delete

public void delete(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Delete entity in database, if not 1 row deleted, throw SqlBoxException

Specified by:
delete in interface ActiveRecordSupport<T>

deleteTry

public int deleteTry(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Delete entity in database, return how many rows affected

Specified by:
deleteTry in interface ActiveRecordSupport<T>

deleteById

public void deleteById(Object id,
                       Object... optionItems)
Description copied from interface: ActiveRecordSupport
Delete entity by given id, if not 1 row deleted, throw SqlBoxException

Specified by:
deleteById in interface ActiveRecordSupport<T>

deleteByIdTry

public int deleteByIdTry(Object id,
                         Object... optionItems)
Description copied from interface: ActiveRecordSupport
Delete entity by given id, return how many rows deleted

Specified by:
deleteByIdTry in interface ActiveRecordSupport<T>

exist

public boolean exist(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Check if entity exist by its id

Specified by:
exist in interface ActiveRecordSupport<T>

existById

public boolean existById(Object id,
                         Object... optionItems)
Description copied from interface: ActiveRecordSupport
Check if entity exist by given id

Specified by:
existById in interface ActiveRecordSupport<T>

load

public T load(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Load entity according its id, if not 1 row round, throw SqlBoxException

Specified by:
load in interface ActiveRecordSupport<T>

loadTry

public int loadTry(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Load entity according its id, return how many rows found

Specified by:
loadTry in interface ActiveRecordSupport<T>

loadById

public T loadById(Object id,
                  Object... optionItems)
Description copied from interface: ActiveRecordSupport
Load entity by given id, if not 1 row found, throw SqlBoxException

Specified by:
loadById in interface ActiveRecordSupport<T>

loadByIdTry

public T loadByIdTry(Object id,
                     Object... optionItems)
Description copied from interface: ActiveRecordSupport
Load entity by given id, if not found, return null

Specified by:
loadByIdTry in interface ActiveRecordSupport<T>

findAll

public List<T> findAll(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Find all entity of same entity class, if not found, return empty list

Specified by:
findAll in interface ActiveRecordSupport<T>

findByIds

public List<T> findByIds(Iterable<?> ids,
                         Object... optionItems)
Description copied from interface: ActiveRecordSupport
Find all entity according its id, if not found, return empty list

Specified by:
findByIds in interface ActiveRecordSupport<T>

findBySQL

public List<T> findBySQL(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Find entity according SQL, if not found, return empty list

Specified by:
findBySQL in interface ActiveRecordSupport<T>

findBySample

public List<T> findBySample(Object sampleBean,
                            Object... optionItems)
Description copied from interface: ActiveRecordSupport
Find entity according a sample bean, ignore null fields, if not found, return empty list

Specified by:
findBySample in interface ActiveRecordSupport<T>

findOneRelated

public <E> E findOneRelated(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Find one related entity

Specified by:
findOneRelated in interface ActiveRecordSupport<T>

findRelatedList

public <E> List<E> findRelatedList(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Find related entity List

Specified by:
findRelatedList in interface ActiveRecordSupport<T>

findRelatedSet

public <E> Set<E> findRelatedSet(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Find related entity Set

Specified by:
findRelatedSet in interface ActiveRecordSupport<T>

findRelatedMap

public <E> Map<Object,E> findRelatedMap(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Find related entity Map

Specified by:
findRelatedMap in interface ActiveRecordSupport<T>

countAll

public int countAll(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Return how many records for current entity class

Specified by:
countAll in interface ActiveRecordSupport<T>

miscMethods__________________

protected void miscMethods__________________()

put

public T put(Object... fieldAndValues)
Description copied from interface: ActiveRecordSupport
Link style set values for entity field, format like: user.put("id","id1").put("name","Sam").put("address","Beijing","phone","12345", "email","abc@123.com")

Specified by:
put in interface ActiveRecordSupport<T>

putFields

public T putFields(String... fieldNames)
Description copied from interface: ActiveRecordSupport
Cache a field array in ThreadLocal for putValues method use

Specified by:
putFields in interface ActiveRecordSupport<T>

putValues

public T putValues(Object... values)
Description copied from interface: ActiveRecordSupport
Put values for entity fields, field names should be cached by call putFields method first

Specified by:
putValues in interface ActiveRecordSupport<T>

guess

public <U> U guess(Object... params)
Description copied from interface: ActiveRecordSupport
In SqlMapper style, based on current method @Sql annotated String or Text(see user manual) in comments(need put Java file in resources folder, see user manual) and parameters, guess a best fit query/update/delete/execute method and run it

Specified by:
guess in interface ActiveRecordSupport<T>

guessSQL

public String guessSQL()
Description copied from interface: ActiveRecordSupport
In SqlMapper style, return current method's SQL String based on current method @Sql annotated String or Text(see user manual) in comments(need put Java file in resources folder, see user manual)

Specified by:
guessSQL in interface ActiveRecordSupport<T>

guessPreparedSQL

public PreparedSQL guessPreparedSQL(Object... params)
Description copied from interface: ActiveRecordSupport
In SqlMapper style, return current method's prepared SQL

Specified by:
guessPreparedSQL in interface ActiveRecordSupport<T>

bind

public SqlItem bind(Object... parameters)
Description copied from interface: ActiveRecordSupport
For tXxxx style templateEngine use, return a SqlItemType.PUT type SqlItem instance, Usage: put("key1",value1,"key2",value2...);

Specified by:
bind in interface ActiveRecordSupport<T>

shardTB

public String shardTB(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Return current entity's shardTable according its sharding key values

Specified by:
shardTB in interface ActiveRecordSupport<T>

shardDB

public SqlBoxContext shardDB(Object... optionItems)
Description copied from interface: ActiveRecordSupport
Return current entity's shardDatabase according its sharding key values

Specified by:
shardDB in interface ActiveRecordSupport<T>


Copyright © 2018. All rights reserved.