真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

hibernate開(kāi)發(fā)步驟-創(chuàng)新互聯(lián)

Hibernate框架開(kāi)發(fā)步驟

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

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

專(zhuān)注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)池州免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
編寫(xiě)hibernate.cfg.xml文件

'1.0'encoding='UTF-8'?>

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

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

   

       

       

       "connection.url">jdbc:mysql://127.0.0.1:3306/test

       

       "connection.username">root

       

       "connection.password">123456

       

       "connection.driver_class">com.mysql.jdbc.Driver

           

           

           "dialect">org.hibernate.dialect.MySQLDialect

           

           "show_sql">true

           

           "format_sql">true

           

           "hbm2ddl.auto">update

           

           "com/edu/bean/User.hbm.xml"/>

   

編寫(xiě)實(shí)體類(lèi),創(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

        }

}

編寫(xiě)映射文件

eg:

"1.0"encoding="UTF-8"?>

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

        

        "com.edu.bean.User"table="user">

                  

                  "id"column="id">

                           

                           "identity">

                  

                  "username"type="string"column="username">

                  "password"type="string"column="password">

        

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

"com/edu/bean/User.hbm.xml"/>

編寫(xiě)代碼:

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對(duì)象

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

                  //持久化對(duì)象

                  try{

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

                           session.save(user);

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

                           tc.commit();

                  }catch(Exception e){

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

                           tc.rollback();

                  }

                  //關(guān)閉session

                  session.close();

        }

}

代碼分層優(yōu)化:
獲取Session的HibernateGetSession類(lèi):

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類(lèi):

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對(duì)象

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

                  //持久化對(duì)象

                  try{

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

                           session.save(user);

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

                           tc.commit();

                  }catch(Exception e){

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

                           tc.rollback();

                  }

                  //關(guān)閉session

                  session.close();

        }

}

測(cè)試層:

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

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。


當(dāng)前題目:hibernate開(kāi)發(fā)步驟-創(chuàng)新互聯(lián)
文章源于:http://weahome.cn/article/eoioj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部