這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)hibernate中怎么使用configuration類配置數(shù)據(jù)庫(kù),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)為客戶提供專業(yè)的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)、程序、域名、空間一條龍服務(wù),提供基于WEB的系統(tǒng)開(kāi)發(fā). 服務(wù)項(xiàng)目涵蓋了網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站程序開(kāi)發(fā)、WEB系統(tǒng)開(kāi)發(fā)、微信二次開(kāi)發(fā)、手機(jī)網(wǎng)站制作設(shè)計(jì)等網(wǎng)站方面業(yè)務(wù)。
提供session的hibernate工具類:
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class HibernateUtils1 { private static Configuration cfg = null; private static SessionFactory factory = null; private static Session session = null; static { cfg = new Configuration().setProperty("hibernate.connection.driver_class", "com.MySQL.jdbc.Driver") .setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/aleavesystem?pinGlobalTxToPhysicalConnection=true&characterEncoding=UTF-8") .setProperty("hibernate.connection.username", "root") .setProperty("hibernate.connection.password", "root").addAnnotatedClass(Employee.class); ; factory = cfg.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()) .build()); } public static Session getSession() { if(factory != null) { return factory.openSession(); }else{ factory = cfg.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()) .build()); } return factory.openSession(); } public static void closeSession() { if(session != null && session.isOpen()){ session.close(); } } }
說(shuō)明:參閱了hibernate的接口文檔,org.hibernate.cfg.Configuration類提供設(shè)置property屬性的方法setProperty,參數(shù)格式(屬性名稱,屬性值),例如設(shè)置數(shù)據(jù)庫(kù)連接為setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/aleavesystem?characterEncoding=UTF-8"),其他屬性依次類推,可以面條式增加屬性configuration.setProperty("a","1").setProperty("b","2")......
addAnnotatedClass,則是為實(shí)體類配置提供的方法,如上面代碼addAnnotatedClass(Employee.class),配置注解實(shí)體類,同樣也是可以面條式增加多個(gè)實(shí)體類addAnnotatedClass(類A).addAnnotatedClass(類B).addAnnotatedClass(類C),參數(shù)注意是class類,直接實(shí)體類后面加.class就行。
實(shí)體類Employee:
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "Employee ") public class Employee { @Id @Column(name = "id") private int id; public Employee() {} public int getId() { return id; } public void setId( int id ) { this.id = id; } }
測(cè)試入口類:
import org.hibernate.*; public class Test { public static void main(String[] args) { /**** 上面是配置準(zhǔn)備,下面開(kāi)始我們的數(shù)據(jù)庫(kù)操作 ******/ Session session = HibernateUtils1.getSession();// 從會(huì)話工廠獲取一個(gè)session Transaction t = session.beginTransaction(); Employee e1 = new Employee(); e1.setId(1); Employee e2 = new Employee(); e2.setId(2); session.persist(e1); session.persist(e2); t.commit(); session.close(); System.out.println("successfully saved"); } }
上述就是小編為大家分享的hibernate中怎么使用configuration類配置數(shù)據(jù)庫(kù)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。