The base class pojo of the bean of oschina

Source: Internet
Author: User

Package my. dB;
Import java. Io. serializable;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. util. arraylist;
Import java. util. List;
Import java. util. Map;

Import my. cache. cachemanager;
Import my. util. inflector;
Import net. oschina. Beans. user;
Import org. Apache. commons. beanutils. beanutils;
Import org. Apache. commons. dbutils. dbutils;
* Base class of the database object
* @ Author winter Lau
* @ Date 2010-1-22 11:33:56 AM
*/
Public class pojo implements serializable {

Protected final static transient char obj_count_cache_key = '#';
Private long ___ key_id;

Public long GETID () {return ___ key_id ;}
Public void setid (long ID) {This. ___ key_id = ID ;}

Private string _ this_table_name;

Public static void evictcache (String cache, serializable key ){
Cachemanager. evict (cache, key );
}

Public static void setcache (String cache, serializable key, serializable value ){
Cachemanager. Set (cache, key, value );
Public static object getcache (String cache, serializable key ){
Return cachemanager. Get (cache, key );
* List all objects by PAGE
* @ Param page
* @ Param size
* @ Return
Public list <? Extends pojo> List (INT page, int size ){
String SQL = "select * from" + tablename () + "order by ID DESC ";
Return queryhelper. query_slice (getclass (), SQL, page, size );
Public list <? Extends pojo> filter (string filter, int page, int size ){
String SQL = "select * from" + tablename () + "where" + filter + "order by ID DESC ";
Return queryhelper. query_slice (getclass (), SQL, page, size );
* Count the total number of records of this object
* @ Return
*/
Public int totalcount (string filter ){
Return (INT) queryhelper. Stat ("select count (*) from" + tablename () + "where" + filter );
* Returns the table name corresponding to the default object.
* @ Return
Protected string tablename (){
If (_ this_table_name = NULL)
_ This_table_name = "OSC _" + inflector. getinstance (). tableize (getclass ());
Return _ this_table_name;
* Return the cache region name of the object.
* @ Return
Public String cacheregion () {return this. getclass (). getsimplename ();}

* Indicates whether to cache objects by ID. This method is valid for get (long ID ).
* @ Return
Protected Boolean isobjectcachedbyid () {return false ;}
* Insert an object to a database table
* @ Return
*/
Public long save (){
If (GETID ()> 0)
_ Insertobject (this );
Else
Setid (_ insertobject (this ));
If (this. isobjectcachedbyid ())
Cachemanager. evict (cacheregion (), obj_count_cache_key );
Return GETID ();
* Delete an object based on the id Primary Key
* @ Return
*/Http://www.huiyi8.com/jiaoben/
Public Boolean Delete () {webpage special effect code
Boolean DR = evict (queryhelper. Update ("delete from" + tablename () + "where id =? ", GETID () = 1 );
If (DR)
Cachemanager. evict (cacheregion (), obj_count_cache_key );
Return Dr;
* Determines whether to clear the object cache based on the condition.
* @ Param er
* @ Return
*/
Public Boolean evict (Boolean ER ){
If (ER & isobjectcachedbyid ())
Cachemanager. evict (cacheregion (), GETID ());
Return er;
* Clear the object cache of the specified Primary Key
* @ Param obj_id
*/
Protected void evict (long obj_id ){
Cachemanager. evict (cacheregion (), obj_id );
Public pojo get (Java. Math. biginteger ID ){
If (ID = NULL) return NULL;
Return get (Id. longvalue ());
* Read the object details based on the primary key and automatically determine whether the cache is required based on the preset method.
* @ Param ID
* @ Return
@ Suppresswarnings ("unchecked ")
Public <t extends pojo> t get (long ID ){
If (ID <= 0) return NULL;
String SQL = "select * from" + tablename () + "where id =? ";
Boolean cached = isobjectcachedbyid ();
Return (t) queryhelper. read_cache (getclass (), cached? Cacheregion (): NULL, ID, SQL, ID );
Public list <? Extends pojo> batchget (list <long> IDs ){
If (IDs = NULL | IDs. Size () = 0)
Return NULL;
Stringbuilder SQL = new stringbuilder ("select * from" + tablename () + "where ID in (");
For (INT I = 1; I <= IDs. Size (); I ++ ){
SQL. append ('? ');
If (I <IDs. Size ())
SQL. append (',');
SQL. append (')');
List <? Extends pojo> beans = queryhelper. Query (getclass (), SQL. tostring (), IDs. toarray (new object [IDs. Size ()]);
If (isobjectcachedbyid ()){
For (Object Bean: Beans ){
Cachemanager. Set (cacheregion (), (pojo) bean). GETID (), (serializable) bean );
Return beans;
* Count the total number of records of this object
* @ Return
*/
Public int totalcount (){
If (this. isobjectcachedbyid ())
Return (INT) queryhelper. stat_cache (cacheregion (), obj_count_cache_key, "select count (*) from" + tablename ());
Return (INT) queryhelper. Stat ("select count (*) from" + tablename ());
* Load projects in batches
* @ Param PIDs
* @ Return
*/
@ Suppresswarnings ("rawtypes ")
Public list loadlist (list <long> p_pids ){
If (p_pids = NULL)
Return NULL;
Final list <long> PIDs = new arraylist <long> (p_pids.size ());
For (number OBJ: p_pids ){
PIDs. Add (obj. longvalue ());
}
String cache = This. cacheregion ();
List <pojo> prjs = new arraylist <pojo> (PIDS. size () {for (INT I = 0; I <PIDs. size (); I ++) add (null );}};
List <long> no_cache_ids = new arraylist <long> ();
For (INT I = 0; I <PIDs. Size (); I ++ ){
Long pid = PIDs. Get (I );
Pojo OBJ = (pojo) cachemanager. Get (cache, pid );

If (OBJ! = NULL)
Prjs. Set (I, OBJ );
Else {
No_cache_ids.add (PID );
If (no_cache_ids.size ()> 0 ){
List <? Extends pojo> no_cache_prjs = batchget (no_cache_ids );
If (no_cache_prjs! = NULL)
For (pojo OBJ: no_cache_prjs ){
Prjs. Set (PIDS. indexof (obj. GETID (), OBJ );
No_cache_ids = NULL;
// Check users
If (prjs! = NULL & isautoloaduser ()){
List <long> no_cache_userids = new arraylist <long> ();
String user_cache = user. instance. cacheregion ();
For (pojo: prjs ){
If (pojo = NULL)
Continue;
Long userid = pojo. getautoloaduser ();
If (userid> 0 &&! No_cache_userids.contains (userid )){
Pojo user = (pojo) cachemanager. Get (user_cache, userid );
If (user = NULL ){
No_cache_userids.add (userid );
If (no_cache_userids.size ()> 0)
User. instance. batchget (no_cache_userids );

No_cache_userids = NULL;
Return prjs;
Protected Boolean isautoloaduser () {return false ;}
Protected long getautoloaduser () {return 0l ;}
* Insert an object
* @ Param OBJ
* @ Return returns the primary key of the inserted object.
*/

Private Static long _ insertobject (pojo OBJ ){
Map <string, Object> pojo_bean = obj. listinsertablefields ();
String [] fields = pojo_bean.keyset (). toarray (New String [pojo_bean.size ()]);
Stringbuilder SQL = new stringbuilder ("insert ");
SQL. append (obj. tablename ());
SQL. append ('(');
For (INT I = 0; I <fields. length; I ++ ){
If (I> 0) SQL. append (',');
SQL. append (fields [I]);
}
SQL. append (") values (");
For (INT I = 0; I <fields. length; I ++ ){
If (I> 0) SQL. append (',');
SQL. append ('? ');
}
SQL. append (')');
Preparedstatement PS = NULL;
Resultset rs = NULL;
Try {
PS = queryhelper. getconnection (). preparestatement (SQL. tostring (), preparedstatement. return_generated_keys );
For (INT I = 0; I <fields. length; I ++ ){
PS. setobject (I + 1, pojo_bean.get (fields [I]);
}
Ps.exe cuteupdate ();
Rs = ps. getgeneratedkeys ();
Return Rs. Next ()? Rs. getlong (1):-1;
} Catch (sqlexception e ){
Throw new dbexception (E );
} Finally {
Dbutils. closequietly (RS );
Dbutils. closequietly (PS );
SQL = NULL;
Fields = NULL;
Pojo_bean = NULL;
}
}

/**
* Lists the Domain sets to be inserted into the database. Subclass can override this method.
* @ Return
*/
@ Suppresswarnings ("unchecked ")
Protected Map <string, Object> listinsertablefields (){
Try {
Map <string, Object> props = beanutils. Describe (this );
If (GETID () <= 0)
Props. Remove ("ID ");
Props. Remove ("class ");
Return props;
} Catch (exception e ){
Throw new runtimeexception ("exception when fetching fields of" + this );
@ Override
Public Boolean equals (Object OBJ ){
If (OBJ = NULL) return false;
If (OBJ = This) return true;
// Different subclasses are not equal even if the IDs are the same
If (! Getclass (). Equals (obj. getclass ()))
Return false;
Pojo WB = (pojo) OBJ;
Return WB. GETID () = GETID ();
}

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.