您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)hibernate中怎么使用configuration類配置數(shù)據(jù)庫,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
提供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(); } } }
說明:參閱了hibernate的接口文檔,org.hibernate.cfg.Configuration類提供設(shè)置property屬性的方法setProperty,參數(shù)格式(屬性名稱,屬性值),例如設(shè)置數(shù)據(jù)庫連接為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)備,下面開始我們的數(shù)據(jù)庫操作 ******/ 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ù)庫了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。