91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

hibernate開發步驟

發布時間:2020-05-18 12:04:10 來源:網絡 閱讀:469 作者:FujiAtobe 欄目:數據庫

Hibernate框架開發步驟

項目導入需要的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>

        <!-- 配置連接數據庫信息 -->

        <!-- 加載數據庫的URL -->

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

        <!-- 數據庫連接名 -->

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

        <!-- 數據庫的連接密碼 -->

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

        <!-- 加載數據庫驅動 -->

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

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

             <!-- 設置數據庫方言 -->

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

             <!-- 后臺是否顯示SQL語句 -->

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

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

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

             <!-- 設置數據庫生成策略默認為update-->

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

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

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

    </session-factory>

</hibernate-configuration>

編寫實體類,創建表

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對應實體類,table對應數據庫中的表 -->

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

                   <!-- name對應實體類中屬性名,column對應表中字段type為表中字段類型-->

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

                            <!-- 設置主鍵生成策略 -->

                            <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");

                   //創建SessionFactory工廠

                   SessionFactorysf=cfg.buildSessionFactory();

                   //獲取session

                   Sessionsession=sf.openSession();

                   //創建事務

                   Transactiontc=session.beginTransaction();

                   //創建User對象

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

                   //持久化對象

                   try{

                            //保存數據

                            session.save(user);

                            //提交事務(不可少)

                            tc.commit();

                   }catch(Exception e){

                            //數據操作失敗,回滾事務

                            tc.rollback();

                   }

                   //關閉session

                   session.close();

         }

}

代碼分層優化:
獲取SessionHibernateGetSession:

package com.edu.dbconn;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class HibernateGetSession {

         //保證SessionFactory工廠只創建一次

         private staticSessionFactory sf;

         static{

                   if(sf==null){

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

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

                            //創建SessionFactory工廠

                            sf=cfg.buildSessionFactory();

                   }

         }

         public staticSession getSession(){

                   //創建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();

                   //創建事務

                   Transactiontc=session.beginTransaction();

                   //創建User對象

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

                   //持久化對象

                   try{

                            //保存數據

                            session.save(user);

                            //提交事務(不可少)

                            tc.commit();

                   }catch(Exception e){

                            //數據操作失敗,回滾事務

                            tc.rollback();

                   }

                   //關閉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();

         }

}

實例源碼:

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


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

绿春县| 齐齐哈尔市| 无棣县| 平武县| 苍山县| 门头沟区| 乃东县| 文昌市| 虎林市| 石楼县| 金堂县| 乌拉特中旗| 麻江县| 喀什市| 海丰县| 玉屏| 达拉特旗| 水城县| 新源县| 子长县| 怀仁县| 关岭| 沙雅县| 通渭县| 阿图什市| 格尔木市| 榆林市| 成武县| 阳高县| 昌都县| 江津市| 岑溪市| 喀喇沁旗| 北辰区| 神农架林区| 盐城市| 冀州市| 三穗县| 巴林右旗| 云浮市| 和政县|