java自界說靜態鏈接數據庫示例。本站提示廣大學習愛好者:(java自界說靜態鏈接數據庫示例)文章只能為提供參考,不一定能成為您想要的結果。以下是java自界說靜態鏈接數據庫示例正文
package dao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* @author minxuenetcn
*/
public class HibernateSessionFactory {
private final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private Configuration configuration = new Configuration();
private org.hibernate.SessionFactory sessionFactory;
/**
* hibernate.cfg.xml
* @param configFile
*/
public void setConfiguration(String configFile){
this.configuration=new Configuration();
configuration.configure(configFile);
}
/**
* Returns the ThreadLocal Session instance.
* @return Session
* @throws HibernateException
*/
public Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public void rebuildSessionFactory() {
try {
sessionFactory = this.configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return hibernate configuration
*/
public Configuration getConfiguration() {
return configuration;
}
}
// parcelable對象傳遞辦法 public void setParcelableMethod() { Person person = new Person(); person.setmName("王海康"); person.setmSex("男"); person.setmAge(45); Intent intent = new Intent(this, PersonTest.class); Bundle bundle = new Bundle(); bundle.putParcelable(PAR_KEY, person); intent.putExtras(bundle); startActivity(intent); } // parcelable對象獲得辦法 public Person getParcelableMethod(){ Person mPerson = (Person)getIntent().getParcelableExtra(PAR_KEY); return mPerson; }
2)傳遞對象列表,詳細代碼以下:
須要留意的是,若List personList = new ArrayList();則會報錯,由於上面挪用的putParcelableArrayList()函數個中一個參數的類型為ArrayList。
// parcelable對象List傳遞辦法 public void setParcelableListMethod() { ArrayList<Person> personList = new ArrayList<Person>(); Person person1 = new Person(); person1.setmName("王海康"); person1.setmSex("男"); person1.setmAge(45); personList.add(person1); Person person2 = new Person(); person2.setmName("薛岳"); person2.setmSex("男"); person2.setmAge(15); personList.add(person2); Intent intent = new Intent(this, PersonTest.class); Bundle bundle = new Bundle(); bundle.putParcelableArrayList(PAR_LIST_KEY, personList); intent.putExtras(bundle); startActivity(intent); } // parcelable對象獲得辦法 public ArrayList<Person> getParcelableMethod(){ ArrayList<Person> mPersonList = getIntent().getParcelableExtra(PAR_LIST_KEY); return mPersonList; }
3)最初引見一個投契取巧的辦法:
不消繼續Parcelable或Serializable辦法便可完成IPC中對象的傳遞。這類辦法的完成道理不是很明確,只曉得代碼中new ArrayList()前往的實際上是一個EmptyArray.OBJECT數組,不外我感到應當照樣體系挪用Serializable停止序列化的,假如列位讀者有好的設法主意,迎接告訴。
詳細代碼以下:
//對象List傳遞 public void setObjectMethod(){ ......(省略) ArrayList list = new ArrayList(); //ObjectList為某一對象列表 list.add(ObjectList); bundle.putParcelableArrayList(PAR_LIST_KEY, list); intent.putExtras(bundle); startActivity(intent); } //獲得對象List ArrayList list = bundle.getParcelableArrayList("list"); //強轉成你本身界說的list,如許ObjectList就是你傳過去的誰人list了。 ObjectList= (List<Object>) list.get(0);