<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-208  評論-469  文章-30  trackbacks-0
    程序結構圖

    SQLServer中的表設計

    db.HibernateUtil
    package?db;

    import?org.hibernate.HibernateException;
    import?org.hibernate.Session;
    import?org.hibernate.SessionFactory;
    import?org.hibernate.cfg.Configuration;

    /**
    ?*?Configures?and?provides?access?to?Hibernate?sessions,?tied?to?the?current
    ?*?thread?of?execution.?Follows?the?Thread?Local?Session?pattern,?see
    ?*?{
    @link?http://hibernate.org/42.html}.
    ?
    */

    public?class?HibernateUtil
    {

    ????
    /**
    ?????*?Location?of?hibernate.cfg.xml?file.?NOTICE:?Location?should?be?on?the
    ?????*?classpath?as?Hibernate?uses?#resourceAsStream?style?lookup?for?its
    ?????*?configuration?file.?That?is?place?the?config?file?in?a?Java?package?-?the
    ?????*?default?location?is?the?default?Java?package.<br>
    ?????*?<br>
    ?????*?Defaults:?<br>
    ?????*?<code>CONFIG_FILE_LOCATION?=?"/hibernate.conf.xml"</code>?You?can
    ?????*?change?location?with?setConfigFile?method?session?will?be?rebuilded?after
    ?????*?change?of?config?file
    ?????
    */

    ????
    private?static?String?CONFIG_FILE_LOCATION?=?"/hibernate.cfg.xml";

    ????
    private?static?final?ThreadLocal?threadLocal?=?new?ThreadLocal();

    ????
    private?static?Configuration?configuration?=?new?Configuration();

    ????
    private?static?SessionFactory?sessionFactory;

    ????
    private?static?String?configFile?=?CONFIG_FILE_LOCATION;

    ????
    private?HibernateUtil()
    ????
    {
    ????}


    ????
    /**
    ?????*?Returns?the?ThreadLocal?Session?instance.?Lazy?initialize?the
    ?????*?<code>SessionFactory</code>?if?needed.
    ?????*?
    ?????*?
    @return?Session
    ?????*?
    @throws?HibernateException
    ?????
    */

    ????
    public?static?Session?getCurrentSession()?throws?HibernateException
    ????
    {
    ????????Session?session?
    =?(Session)?threadLocal.get();

    ????????
    if?(session?==?null?||?!session.isOpen())
    ????????
    {
    ????????????
    if?(sessionFactory?==?null)
    ????????????
    {
    ????????????????rebuildSessionFactory();
    ????????????}

    ????????????session?
    =?(sessionFactory?!=?null)???sessionFactory.openSession()
    ????????????????????:?
    null;
    ????????????threadLocal.set(session);
    ????????}


    ????????
    return?session;
    ????}


    ????
    /**
    ?????*?Rebuild?hibernate?session?factory
    ?????*?
    ?????
    */

    ????
    public?static?void?rebuildSessionFactory()
    ????
    {
    ????????
    try
    ????????
    {
    ????????????configuration.configure(configFile);
    ????????????sessionFactory?
    =?configuration.buildSessionFactory();
    ????????}
    ?catch?(Exception?e)
    ????????
    {
    ????????????System.err.println(
    "%%%%?Error?Creating?SessionFactory?%%%%");
    ????????????e.printStackTrace();
    ????????}

    ????}


    ????
    /**
    ?????*?Close?the?single?hibernate?session?instance.
    ?????*?
    ?????*?
    @throws?HibernateException
    ?????
    */

    ????
    public?static?void?closeCurrentSession()?throws?HibernateException
    ????
    {
    ????????Session?session?
    =?(Session)?threadLocal.get();
    ????????threadLocal.set(
    null);

    ????????
    if?(session?!=?null)
    ????????
    {
    ????????????session.close();
    ????????}

    ????}


    ????
    /**
    ?????*?return?session?factory
    ?????*?
    ?????
    */

    ????
    public?static?SessionFactory?getSessionFactory()
    ????
    {
    ????????
    return?sessionFactory;
    ????}


    ????
    /**
    ?????*?return?session?factory
    ?????*?
    ?????*?session?factory?will?be?rebuilded?in?the?next?call
    ?????
    */

    ????
    public?static?void?setConfigFile(String?configFile)
    ????
    {
    ????????HibernateUtil.configFile?
    =?configFile;
    ????????sessionFactory?
    =?null;
    ????}


    ????
    /**
    ?????*?return?hibernate?configuration
    ?????*?
    ?????
    */

    ????
    public?static?Configuration?getConfiguration()
    ????
    {
    ????????
    return?configuration;
    ????}


    }
    model.User
    package?model;

    public?class?User
    {
    ????
    private?Integer?id;

    ????
    private?String?username;

    ????
    private?String?password;
    ????
    public?User()
    ????
    {
    ????????
    ????}


    ????
    public?User(?String?password)
    ????
    {
    ????????
    this.password?=?password;
    ????}


    ????
    public?Integer?getId()
    ????
    {
    ????????
    return?id;
    ????}


    ????
    public?void?setId(Integer?id)
    ????
    {
    ????????
    this.id?=?id;
    ????}


    ????
    public?String?getPassword()
    ????
    {
    ????????
    return?password;
    ????}


    ????
    public?void?setPassword(String?password)
    ????
    {
    ????????
    this.password?=?password;
    ????}


    ????
    public?String?getUsername()
    ????
    {
    ????????
    return?username;
    ????}


    ????
    public?void?setUsername(String?username)
    ????
    {
    ????????
    this.username?=?username;
    ????}

    }

    model.hbm.xml
    <?xml?version='1.0'?encoding='UTF-8'?>
    <!DOCTYPE?hibernate-mapping?PUBLIC
    ??????????"-//Hibernate/Hibernate?mapping?DTD?2.0//EN"
    ??????????"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
    >
    <hibernate-mapping>
    <class?name="model.User"?table="user1"?>
    <id?name="id">
    <generator?class="identity"/>
    </id>
    <property?name="username"/>
    <property?name="password"/>
    </class>
    </hibernate-mapping>
    hibernate.cfg.xml
    <?xml?version='1.0'?encoding='UTF-8'?>
    <!DOCTYPE?hibernate-configuration?PUBLIC
    ??????????"-//Hibernate/Hibernate?Configuration?DTD?2.0//EN"
    ??????????"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"
    >

    <!--?Generated?by?MyEclipse?Hibernate?Tools.???????????????????-->
    <hibernate-configuration>

    <session-factory>
    ????
    <property?name="dialect">org.hibernate.dialect.SQLServerDialect</property>
    ????
    <property?name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=login</property>
    ????
    <property?name="connection.username">lzqdiy</property>
    ????
    <property?name="connection.password">lzqdiy</property>
    ????
    <property?name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
    ????
    <property?name="myeclipse.connection.profile">conn2</property>
    ????
    <mapping?resource="model/model.hbm.xml"?/>
    </session-factory>

    </hibernate-configuration>
    HibernateTest

    import?java.util.Iterator;
    import?java.util.List;

    import?org.hibernate.HibernateException;
    import?org.hibernate.Query;
    import?org.hibernate.Session;
    import?org.hibernate.Transaction;
    import?model.User;
    import?db.HibernateUtil;

    public?class?HibernateTest
    {

    ????
    /**
    ?????*?
    @param?args
    ?????*?
    @throws?HibernateException
    ?????
    */

    ????
    public?void?insertUser()?
    ????
    {
    ????????Session?session?
    =?HibernateUtil.getCurrentSession();
    ????????Transaction?tx?
    =?session.beginTransaction();
    ????????User?user?
    =?new?User();
    ????????user.setUsername(
    "lzqdiy");
    ????????user.setPassword(
    "lzqdiy");
    ????????session.save(user);
    ????????tx.commit();
    ????????HibernateUtil.closeCurrentSession();
    ????????System.out.println(
    "新增成功!");
    ????}


    ????
    public?List?getUsers()?
    ????
    {
    ????????Session?session?
    =?HibernateUtil.getCurrentSession();
    ????????Transaction?tx?
    =?session.beginTransaction();
    ????????String?hql?
    =?"select?new?User(password)?from?model.User??where?username=:name";
    ????????Query?query?
    =?session.createQuery(hql);
    ????????query.setString(
    "name",?"lzqdiy");
    ????????List?list?
    =?query.list();
    ????????tx.commit();
    ????????HibernateUtil.closeCurrentSession();
    ????????
    return?list;
    ????}

    ????
    public?void?updateUsers()?
    ????
    {
    ????????Session?session?
    =?HibernateUtil.getCurrentSession();
    ????????Transaction?tx?
    =?session.beginTransaction();
    ????????String?hql?
    =?"update??User??set?password='gm'?";
    ????????Query?query?
    =?session.createQuery(hql);
    ????????query.executeUpdate();
    ????????tx.commit();
    ????????HibernateUtil.closeCurrentSession();
    ????????System.out.println(
    "更新成功!");
    ????}

    ????
    public?void?deleteUsers()?
    ????
    {
    ????????Session?session?
    =?HibernateUtil.getCurrentSession();
    ????????Transaction?tx?
    =?session.beginTransaction();
    ????????String?hql?
    =?"delete??User??where?id=1";
    ????????Query?query?
    =?session.createQuery(hql);
    ????????query.executeUpdate();
    ????????tx.commit();
    ????????HibernateUtil.closeCurrentSession();
    ????????System.out.println(
    "刪除成功!");
    ????}


    ????
    public?static?void?main(String[]?args)?
    ????
    {
    ????????
    //?TODO?Auto-generated?method?stub
    ????????new?HibernateTest().insertUser();
    ????????
    new?HibernateTest().updateUsers();
    ????????
    new?HibernateTest().deleteUsers();
    ????????List?list?
    =?new?HibernateTest().getUsers();
    ????????
    for?(Iterator?iter?=?list.iterator();?iter.hasNext();)
    ????????
    {
    ????????????User?user?
    =?(User)?iter.next();
    ????????????System.out.println(user.getPassword());
    ????????}

    ????}

    }

    其中使用的hql語句比較初級,大家不要見笑,以后我將做深入的研究。
    補充:其實對于更新和刪除并不需要使用hql就可以實現。
    將HibernateTest更改如下,同樣可以實現更新和刪除。

    import?java.util.Iterator;
    import?java.util.List;

    import?org.hibernate.HibernateException;
    import?org.hibernate.Query;
    import?org.hibernate.Session;
    import?org.hibernate.Transaction;
    import?model.User;
    import?db.HibernateUtil;

    public?class?HibernateTest
    {

    ????
    /**
    ?????*?
    @param?args
    ?????*?
    @throws?HibernateException
    ?????
    */

    ????
    public?void?insertUser()?
    ????
    {
    ????????Session?session?
    =?HibernateUtil.getCurrentSession();
    ????????Transaction?tx?
    =?session.beginTransaction();
    ????????User?user?
    =?new?User();
    ????????user.setUsername(
    "lzqdiy");
    ????????user.setPassword(
    "lzqdiy");
    ????????session.save(user);
    ????????tx.commit();
    ????????HibernateUtil.closeCurrentSession();
    ????????System.out.println(
    "新增成功!");
    ????}


    ????
    public?List?getUsers()?
    ????
    {
    ????????Session?session?
    =?HibernateUtil.getCurrentSession();
    ????????Transaction?tx?
    =?session.beginTransaction();
    ????????String?hql?
    =?"select?new?User(password)?from?model.User??where?username=:name";
    ????????Query?query?
    =?session.createQuery(hql);
    ????????query.setString(
    "name",?"lzqdiy");
    ????????List?list?
    =?query.list();
    ????????tx.commit();
    ????????HibernateUtil.closeCurrentSession();
    ????????
    return?list;
    ????}

    ????
    public?void?updateUsers()?
    ????
    {
    ????????Session?session?
    =?HibernateUtil.getCurrentSession();
    ????????Transaction?tx?
    =?session.beginTransaction();
    ????????User?user
    =new?User();
    ????????user.setId(
    new?Integer(1));
    ????????user.setUsername(
    "lzqdiy");
    ????????user.setPassword(
    "gm");
    ????????session.saveOrUpdate(user);
    ????????tx.commit();
    ????????HibernateUtil.closeCurrentSession();
    ????????System.out.println(
    "更新成功!");
    ????}

    ????
    public?void?deleteUsers()?
    ????
    {
    ????????Session?session?
    =?HibernateUtil.getCurrentSession();
    ????????Transaction?tx?
    =?session.beginTransaction();
    ????????User?user
    =new?User();
    ????????user.setId(
    new?Integer(1));
    ????????session.delete(user);
    ????????tx.commit();
    ????????HibernateUtil.closeCurrentSession();
    ????????System.out.println(
    "刪除成功!");
    ????}


    ????
    public?static?void?main(String[]?args)?
    ????
    {
    ????????
    //?TODO?Auto-generated?method?stub
    ????????new?HibernateTest().insertUser();
    ????????
    new?HibernateTest().updateUsers();
    ????????
    new?HibernateTest().deleteUsers();
    ????????List?list?
    =?new?HibernateTest().getUsers();
    ????????
    for?(Iterator?iter?=?list.iterator();?iter.hasNext();)
    ????????
    {
    ????????????User?user?
    =?(User)?iter.next();
    ????????????System.out.println(user.getPassword());
    ????????}

    ????}

    }

    posted on 2007-06-10 21:43 EricWong 閱讀(2258) 評論(1)  編輯  收藏

    評論:
    # re: 用hibernate3實現增刪改查 [未登錄] 2007-08-09 15:36 | haha
    頂  回復  更多評論
      
    # re: 用hibernate3實現增刪改查 2007-09-06 16:15 | 吳楊明
    好 在學hibernate 應用
    謝謝啦 我的博客 m.tkk7.com/coacoa2008
    大家一起進步  回復  更多評論
      
    # re: 用hibernate3實現增刪改查 2007-09-06 16:56 | 吳楊明

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲日本va午夜中文字幕一区| 亚洲国产精品无码久久久秋霞1| aⅴ免费在线观看| 亚洲日韩一区二区三区| 国产亚洲精品高清在线| 国产2021精品视频免费播放| 特级aa**毛片免费观看| 亚洲综合婷婷久久| 免费a级毛片在线观看| 可以免费观看的国产视频| 亚洲中文字幕久久久一区| 国产国拍精品亚洲AV片| 欧美在线看片A免费观看| 久久国产精品免费| 亚洲精品国产第一综合99久久| 国产精品亚洲片在线| 在线a毛片免费视频观看| 一个人免费视频在线观看www| 亚洲欧美日韩中文高清www777| 亚洲精品自产拍在线观看| 免费看大黄高清网站视频在线| 野花香高清视频在线观看免费 | 久久久久亚洲精品日久生情 | 亚洲精品无码av人在线观看| 大地资源二在线观看免费高清| 岛国精品一区免费视频在线观看 | 波多野结衣免费一区视频| 亚洲6080yy久久无码产自国产| 久久久影院亚洲精品| 亚洲A丁香五香天堂网| 啦啦啦高清视频在线观看免费| 成在人线av无码免费高潮喷水| 国产天堂亚洲精品| 亚洲成A人片在线播放器| 亚洲午夜久久久精品影院| 亚洲一区二区三区在线视频| 永久久久免费浮力影院| 皇色在线视频免费网站| 99爱视频99爱在线观看免费| 三年片免费高清版| 国产精品免费在线播放|