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

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

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

    vjame

    優(yōu)化代碼是無止境的
    隨筆 - 65, 文章 - 9, 評論 - 26, 引用 - 0
    數(shù)據(jù)加載中……

    Hibernate編程式事務(wù)


    采用編程式事務(wù)

    1、getCurrentSession()與openSession()的區(qū)別?
     * 采用getCurrentSession()創(chuàng)建的session會綁定到當(dāng)前線程中,而采用openSession()
       創(chuàng)建的session則不會
     * 采用getCurrentSession()創(chuàng)建的session在commit或rollback時會自動關(guān)閉,而采用openSession()
       創(chuàng)建的session必須手動關(guān)閉
      
    2、使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
     * 如果使用的是本地事務(wù)(jdbc事務(wù))
     <property name="hibernate.current_session_context_class">thread</property>
     * 如果使用的是全局事務(wù)(jta事務(wù))
     <property name="hibernate.current_session_context_class">jta</property>   

    1、創(chuàng)建包

    ├─client
    ├─manager
    │  └─impl
    ├─model
    └─util

    2、編寫po和hbm映射文件
    User.java

    package com.strongit.usermgr.model;

    public class User {
        
        
    private int id;
        
    private String name;
        
    public int getId() {
            
    return id;
        }
        
    public void setId(int id) {
            
    this.id = id;
        }
        
    public String getName() {
            
    return name;
        }
        
    public void setName(String name) {
            
    this.name = name;
        }

    }

    Log.java

    package com.strongit.usermgr.model;

    import java.util.Date;

    public class Log {
        
        
    private int id;
        
        
    private String type;
        
        
    private String detail;
        
        
    private Date time;

        
    public int getId() {
            
    return id;
        }

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

        
    public String getType() {
            
    return type;
        }

        
    public void setType(String type) {
            
    this.type = type;
        }

        
    public String getDetail() {
            
    return detail;
        }

        
    public void setDetail(String detail) {
            
    this.detail = detail;
        }

        
    public Date getTime() {
            
    return time;
        }

        
    public void setTime(Date time) {
            
    this.time = time;
        }

    }


    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 package="com.strongit.usermgr.model">
        
    <class name="User" table="t_user">
            
    <id name="id">
                
    <generator class="native" />
            
    </id>
            
    <property generated="never" lazy="false" name="name" />
        
    </class>
    </hibernate-mapping>

    Log.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 package="com.strongit.usermgr.model">
     
    <class name="Log" table="t_log">
      
    <id name="id">
       
    <generator class="native"/>
      
    </id>
      
    <property generated="never" lazy="false" name="type"/>
      
    <property generated="never" lazy="false" name="detail"/>
      
    <property generated="never" lazy="false" name="time"/>
     
    </class>
    </hibernate-mapping>

    3、配置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>
            
    <property name="hibernate.connection.url">jdbc:mysql://localhost/spring_hibernate_1</property>
            
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            
    <property name="hibernate.connection.username">root</property>
            
    <property name="hibernate.connection.password">root</property>
            
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            
    <property name="hibernate.show_sql">true</property>
            
    <property name="hibernate.current_session_context_class">thread</property>
            
    <!-- 
            <property name="hibernate.current_session_context_class">jta</property>
             
    -->        
            
    <mapping resource="com/strongit/usermgr/model/User.hbm.xml"/>
            
    <mapping resource="com/strongit/usermgr/model/Log.hbm.xml"/>
        
    </session-factory>
    </hibernate-configuration>

    4、創(chuàng)建數(shù)據(jù)庫

    啟動mysql      
    net start mysql

    執(zhí)行以下代碼
    package com.strongit.usermgr.util;

    import org.hibernate.cfg.Configuration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;

    public class ExportDB {

        
    public static void main(String[] args) {
            
    //讀取hibernate.cfg.xml文件
            Configuration cfg = new Configuration().configure();
            
            SchemaExport export 
    = new SchemaExport(cfg);
            
            export.create(
    truetrue);

        }
    }

    5、編寫服務(wù)層代碼

    用戶接口 UserManager.java

    package com.strongit.usermgr.manager;

    import com.strongit.usermgr.model.User;

    public interface UserManager {
        
        
    public void addUser(User user);

    }

    用戶接口實現(xiàn) UserManagerImpl.java
    package com.strongit.usermgr.manager.impl;

    import java.util.Date;

    import org.hibernate.Session;

    import com.strongit.usermgr.manager.LogManager;
    import com.strongit.usermgr.manager.UserManager;
    import com.strongit.usermgr.model.Log;
    import com.strongit.usermgr.model.User;
    import com.strongit.usermgr.util.HibernateUtils;

    public class UserManagerImpl implements UserManager {

        
    public void addUser(User user) {
            Session session 
    = null;
            
    try {
    //            session = HibernateUtils.getSession();
                session = HibernateUtils.getSessionFactory().getCurrentSession();
                session.beginTransaction();
                
                session.save(user);

                Log log 
    = new Log();
                log.setType(
    "安全日志");
                log.setDetail(
    "xxx進入系統(tǒng)");
                log.setTime(
    new Date());

                LogManager logManager 
    = new LogManagerImpl();
                logManager.addLog(log);

                session.getTransaction().commit();
            }
    catch(Exception e) {
                e.printStackTrace();
                session.getTransaction().rollback();
    //        }finally {
    //            HibernateUtils.closeSession(session);
            }

        }

    }

    日志接口 LogManager.java
    package com.strongit.usermgr.manager;

    import com.strongit.usermgr.model.Log;

    public interface LogManager {
        
        
    public void addLog(Log log);

    }

    日志接口實現(xiàn) LogManagerImpl.java
    package com.strongit.usermgr.manager.impl;

    import com.strongit.usermgr.manager.LogManager;
    import com.strongit.usermgr.model.Log;
    import com.strongit.usermgr.util.HibernateUtils;

    public class LogManagerImpl implements LogManager {

        
    public void addLog(Log log) {
            HibernateUtils.getSessionFactory().getCurrentSession().save(log);
        }

    }

    hibernate幫助類 HibernateUtils.java
    package com.strongit.usermgr.util;

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

    public class HibernateUtils {

        
    private static SessionFactory factory;
        
        
    static {
            
    try {
                Configuration cfg 
    = new Configuration().configure();
                factory 
    = cfg.buildSessionFactory();
            }
    catch(Exception e) {
                e.printStackTrace();
            }
        }
        
        
    public static SessionFactory getSessionFactory() {
            
    return factory;
        }
        
        
    public static Session getSession() {
            
    return factory.openSession();
        }
        
        
    public static void closeSession(Session session) {
            
    if (session != null) {
                
    if (session.isOpen()) {
                    session.close();
                }
            }
        }
    }

    6、測試類
    package com.strongit.usermgr.client;

    import com.strongit.usermgr.manager.UserManager;
    import com.strongit.usermgr.manager.impl.UserManagerImpl;
    import com.strongit.usermgr.model.User;

    public class Client {

        
    /**  
         *   @Description 方法實現(xiàn)功能描述  
         *   
    @param args
         *   
    @author lanjh 9:02:58 PM
         *   
    @return void
         *   
    @throws  拋出異常說明
         
    */
        
    public static void main(String[] args) {

            
            User user 
    = new User();
            user.setName(
    "lanjh");
            
            UserManager userManager 
    = new UserManagerImpl();
            userManager.addUser(user);

        }

    }

    posted on 2009-08-07 18:02 lanjh 閱讀(626) 評論(0)  編輯  收藏 所屬分類: Java Web

    主站蜘蛛池模板: 亚洲女女女同性video| 国产精品99精品久久免费| 精品亚洲视频在线观看 | 一区二区三区观看免费中文视频在线播放 | 亚洲免费电影网站| 亚洲国产成人一区二区精品区| 2022国内精品免费福利视频| 免费国产成人午夜私人影视| 99久久国产精品免费一区二区| 亚洲国产视频一区| 亚洲精品高清在线| 日本XXX黄区免费看| 一级毛片免费在线| 国产精品亚洲二区在线观看 | 蜜桃AV无码免费看永久| 亚洲欧洲自拍拍偷综合| 亚洲成av人片一区二区三区| 国产国产人免费人成成免视频| 亚洲国产美女视频| 亚洲精品无码精品mV在线观看| 成年女人午夜毛片免费视频| 一本色道久久88—综合亚洲精品 | 亚洲一区二区三区高清| 18禁成人网站免费观看| 一级中文字幕免费乱码专区| 狠狠亚洲婷婷综合色香五月排名| 一个人免费高清在线观看| 国产免费AV片在线观看| 免费国产高清毛不卡片基地| 亚洲一级特黄特黄的大片| 亚洲精品国产成人99久久| 亚洲情a成黄在线观看| 日韩高清在线高清免费| 国产精品视频免费| 亚洲av第一网站久章草| 亚洲欧洲国产经精品香蕉网| 亚洲精品少妇30p| 国产美女亚洲精品久久久综合| 免费a级毛片永久免费| 日本成人在线免费观看| 无遮免费网站在线入口|