Hibernate框架之入门配置
生活随笔
收集整理的這篇文章主要介紹了
Hibernate框架之入门配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、Hibernate導入相關的包
? ? ?參考:http://blog.csdn.net/tunni/article/details/54982160
? ? ?這些包包括相應數據庫驅動、hibernate.zip下lib目錄下的jar包,其中必須包是required目錄下的.jar
二、在項目classpath(類路徑,即src目錄下)配置hibernate.cfg.xml,并且配置數據庫連接
?
hibernate.cfg.xml配置文件
<!DOCTYPE hibernate-configuration PUBLIC ? ?"-//Hibernate/Hibernate Configuration DTD 3.0//EN" ? ?"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> ? ?<hibernate-configuration> ? ?<session-factory > ? ?<!-- mysql數據庫驅動 --> ? ?<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> ? ?<!-- mysql數據庫名稱 --> ? ?<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_db</property> ??<!-- 數據庫的登陸用戶名 --> ? ?<property name="hibernate.connection.username">root</property> ? ?<!-- 數據庫的登陸密碼 --> ? ?<property name="hibernate.connection.password">admin</property> ?<!-- 方言:為每一種數據庫提供適配器,方便轉換 --> ? ?<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> ? ?<!-- 建議配置,方便在日志中查看sql語句--> ? ?<propertynamepropertyname="hibernate.show_sql">true</property> ?<propertyname="hibernate.format_sql">true</property> ?<!--配置類與表的映射文件 --> ? ?<mapping resource="com/hibernate/User.hbm.xml"/></session-factory> ? ? </hibernate-configuration>?
?
com.hibernate.User類
package com.hibernate;public class User { ? ?private String id; ? ?private String username; ? ?private String password; ? ?public String getId() { ? ?return id; ? ?} ? ?public void setId(String id) { ? ?this.id = id; ? ?} ? ?public String getUsername() { ? ?return username; ? ?} ? ?public void setUsername(String userName) { ? ?this.username = userName; ? ?} ? ?public String getPassword() { ? ?return password; ? ?} ? ?public void setPassword(String password) { ? ?this.password = password; ? ?} ? ? } ? ?User.hbm.xml配置文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- 類與數據庫的表對應 --> <class name="com.hibernate.User" table="user"> <!-- 主鍵名 --> <id name="id" column="id"> <!-- 生成策略 --> <generator class="uuid"/> </id> <!-- 其他類屬性與表字段 --> <property name="username" column="username"/> <property name="password"/> </class> </hibernate-mapping>hibernate訪問工具類
?
package hibernate; /*** hibernate工具* @author maokun* */ import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;public class HibernateUtil {private static final SessionFactory sessionFactory;static{try{ //配置文件放在classpath路徑,即src目錄下//如果hibernate的配置文件目錄為hibernate.cfg.xml,則//Configuration config = new Configuration().configure();//或Configuration config = new Configuration().configure("hibernate.cfg.xml");//或Configuration config = new Configuration().configure("hibernate.cfg.xml");? ? //配置其他路徑如下://Configuration config = new Configuration().configure("hibernate/hibernate.cfg.xml");Configuration config = new Configuration().configure("/hibernate/hibernate.cfg.xml");sessionFactory = config.buildSessionFactory();}catch(Throwable e){throw new ExceptionInInitializerError(e);}}public static final ThreadLocal session = new ThreadLocal();public static Session currentSession() throws HibernateException{Session s = (Session)session.get();//如果線程沒有session,打開新的sessionif(s == null || !s.isOpen()){s = sessionFactory.openSession();session.set(s);}return s;}public static void closeSession() throws HibernateException{Session s = (Session)session.get();session.set(null);if(s != null)s.close();}}三、第一個hibernate例子
?
先創建hibernate_db數據庫,接著創建user表包含id,username,password。
Test類
?
package hibernate;import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction;import com.hibernate.User;public class Test {public static void main(String[] args) {User user =new User();user.setUsername("name");user.setPassword("pass");Session session = HibernateUtil.currentSession();//生成Session實例Transaction tx = session.beginTransaction();try{session.save(user); //保存持久類對象tx.commit(); //提交到數據庫session.close(); }catch(HibernateException e){e.printStackTrace();tx.rollback();}} }總結
以上是生活随笔為你收集整理的Hibernate框架之入门配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hibernate之必须导入jar包
- 下一篇: hibernate框架之主键生成