溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

hibernate開發(fā)步驟

發(fā)布時(shí)間:2020-05-18 12:04:10 來源:網(wǎng)絡(luò) 閱讀:469 作者:FujiAtobe 欄目:數(shù)據(jù)庫

Hibernate框架開發(fā)步驟

項(xiàng)目導(dǎo)入需要的jar:

http://pan.baidu.com/s/1eRQ19C2

編寫hibernate.cfg.xml文件

<?xml version='1.0'encoding='UTF-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

         "-//Hibernate/Hibernate Configuration DTD3.0//EN"

         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generatedby MyEclipse Hibernate Tools. -->

<hibernate-configuration>

    <session-factory>

        <!-- 配置連接數(shù)據(jù)庫信息 -->

        <!-- 加載數(shù)據(jù)庫的URL -->

        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/test</property>

        <!-- 數(shù)據(jù)庫連接名 -->

        <property name="connection.username">root</property>

        <!-- 數(shù)據(jù)庫的連接密碼 -->

        <property name="connection.password">123456</property>

        <!-- 加載數(shù)據(jù)庫驅(qū)動(dòng) -->

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>   

             <!-- 配置hibernate屬性信息 -->

             <!-- 設(shè)置數(shù)據(jù)庫方言 -->

             <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

             <!-- 后臺(tái)是否顯示SQL語句 -->

             <property name="show_sql">true</property>

             <!-- 是否格式化SQL語句 -->

             <property name="format_sql">true</property>

             <!-- 設(shè)置數(shù)據(jù)庫生成策略默認(rèn)為update-->

             <property name="hbm2ddl.auto">update</property>

             <!-- 加載hibernate映射文件 -->

             <mapping resource="com/edu/bean/User.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

編寫實(shí)體類,創(chuàng)建表

eg

package com.edu.bean;

 

public class User {

         private intid;

         private String username;

         private String password;

         public intgetId() {

                   return id;

         }

         public voidsetId(int id) {

                   this.id =id;

         }

         public String getUsername() {

                   return username;

         }

         public voidsetUsername(String username) {

                   this.username= username;

         }

         public String getPassword() {

                   return password;

         }

         public voidsetPassword(String password) {

                   this.password= password;

         }

public User(Stringusername, String password) {

                   super();

                   this.username= username;

                   this.password= password;

         }

         public User() {

                   // TODO Auto-generated constructor stub

         }

}

編寫映射文件

eg

<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

         <!-- name對應(yīng)實(shí)體類,table對應(yīng)數(shù)據(jù)庫中的表 -->

         <class name="com.edu.bean.User"table="user">

                   <!-- name對應(yīng)實(shí)體類中屬性名,column對應(yīng)表中字段,type為表中字段類型-->

                   <id name="id"column="id">

                            <!-- 設(shè)置主鍵生成策略 -->

                            <generator class="identity"></generator>

                   </id>

                   <property name="username"type="string"column="username"></property>

                   <property name="password"type="string"column="password"></property>

         </class>

</hibernate-mapping>

將映射文件放入到hibernate.cfg.xml

<!-- 加載hibernate映射文件 -->

<mapping resource="com/edu/bean/User.hbm.xml"/>

編寫代碼:

eg

package com.edu.test;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.junit.Test;

import com.edu.bean.User;

public class HibernateTest {

         @Test

         public voidtest(){

                   //讀取hibernate.cfg.xml主配置文件

                   Configurationcfg=new Configuration().configure("hibernate.cfg.xml");

                   //創(chuàng)建SessionFactory工廠

                   SessionFactorysf=cfg.buildSessionFactory();

                   //獲取session

                   Sessionsession=sf.openSession();

                   //創(chuàng)建事務(wù)

                   Transactiontc=session.beginTransaction();

                   //創(chuàng)建User對象

                   Useruser=new User("張三", "123456");

                   //持久化對象

                   try{

                            //保存數(shù)據(jù)

                            session.save(user);

                            //提交事務(wù)(不可少)

                            tc.commit();

                   }catch(Exception e){

                            //數(shù)據(jù)操作失敗,回滾事務(wù)

                            tc.rollback();

                   }

                   //關(guān)閉session

                   session.close();

         }

}

代碼分層優(yōu)化:
獲取SessionHibernateGetSession:

package com.edu.dbconn;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class HibernateGetSession {

         //保證SessionFactory工廠只創(chuàng)建一次

         private staticSessionFactory sf;

         static{

                   if(sf==null){

                            //讀取hibernate.cfg.xml主配置文件

                            Configurationcfg=new Configuration().configure("hibernate.cfg.xml");

                            //創(chuàng)建SessionFactory工廠

                            sf=cfg.buildSessionFactory();

                   }

         }

         public staticSession getSession(){

                   //創(chuàng)建session

                   Sessionsession=sf.openSession();

                   return session;       

         }

}

Dao層的UserDao:

package com.edu.dao;

import org.hibernate.Session;

import org.hibernate.Transaction;

import com.edu.bean.User;

import com.edu.dbconn.HibernateGetSession;

 

public class UserDao {

         public voidsaveUser(){

                   //獲取session

                   Sessionsession=HibernateGetSession.getSession();

                   //創(chuàng)建事務(wù)

                   Transactiontc=session.beginTransaction();

                   //創(chuàng)建User對象

                   Useruser=new User("李四", "123456");

                   //持久化對象

                   try{

                            //保存數(shù)據(jù)

                            session.save(user);

                            //提交事務(wù)(不可少)

                            tc.commit();

                   }catch(Exception e){

                            //數(shù)據(jù)操作失敗,回滾事務(wù)

                            tc.rollback();

                   }

                   //關(guān)閉session

                   session.close();

         }

}

測試層:

package com.edu.test;

import org.junit.Test;

import com.edu.dao.UserDao;

public class HibernateTest {

         @Test

         public voidtest(){

                   UserDaoud=new UserDao();

                   ud.saveUser();

         }

}

實(shí)例源碼:

http://pan.baidu.com/s/1eRPLFJO


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI