綁定本地的Session

來源:互聯網
上載者:User

標籤:dia   none   本地   org   save   構造   static   trace   factor   

綁定本地的Session圖示解析:

 代碼的結構:

代碼:SaveServlet.java
 1 package com.itheima.servlet; 2  3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8  9 import com.itheima.domain.User;10 import com.itheima.service.UserService;11 12 /**13  * Servlet implementation class SaveServlet14  */15 public class SaveServlet extends HttpServlet {16     private static final long serialVersionUID = 1L;17        18 19     public SaveServlet() {20         super();21     }22 23 24     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {25         User u1=new User();26         u1.setName("測試一");27         28         User u2=new User();29         u2.setName("測試二");30         31         new UserService().save(u1, u2);32     }33 34 35     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {36             // TODO Auto-generated method stub37             doGet(request, response);38     }39 40 }
View CodeUserService.java
 1 package com.itheima.service; 2  3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5  6 import com.itheima.dao.UserDao; 7 import com.itheima.domain.User; 8 import com.itheima.utils.HibernateUtils; 9 10 public class UserService {11 12     public void save(User u1,User u2) {13         UserDao dao=new UserDao();14         //擷取session15         Session session=HibernateUtils.getCurrentSession();16         //開啟事務17         Transaction tr=session.beginTransaction();18         try {19             dao.save1(u1);20             dao.save2(u2);21             //提交事務22             tr.commit();23         } catch (Exception e) {24             e.printStackTrace();25             //出現問題:復原事務26             tr.rollback();27         }finally {28             29         }30         31     }32 }
View Code UserDao.java
 1 package com.itheima.dao; 2  3 import org.hibernate.Hibernate; 4 import org.hibernate.Session; 5 import org.hibernate.Transaction; 6  7 import com.itheima.domain.User; 8 import com.itheima.utils.HibernateUtils; 9 10 public class UserDao {11 12     public void save1(User u1) {13     14         Session session=HibernateUtils.getCurrentSession();15         session.save(u1);16     }17     public void save2(User u2) {18         Session session=HibernateUtils.getCurrentSession();19         session.save(u2);20     }21 }
View CodeHibernateUtils.java(稍微修改)
 1 package com.itheima.utils; 2  3 import javax.servlet.jsp.jstl.core.Config; 4  5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import org.hibernate.cfg.Configuration; 8  9 /*10  * Hibernate架構的工具11  * @author chenyanlong12  */13 public class HibernateUtils {14 15     //Curl+shift+x16     private static final Configuration CONFIG;17     private static final SessionFactory FACTORY;18     19     //編寫靜態代碼20     static {21         //載入設定檔22         CONFIG =new Configuration().configure();23         //構造工廠24         FACTORY=CONFIG.buildSessionFactory();25     }26     27     /*28      * 從工廠擷取Session對象29      * @return30      */31     public static Session getSession() {32         return FACTORY.openSession();33         34     }35     //擷取本地Session36     public static Session getCurrentSession() {37         return FACTORY.getCurrentSession();38     }39     public static  void main(String[] args) {40         //調用擷取session的方法41         getSession();42     }43 }
View Codehibernate.cfg.xml(開啟綁定本地的session)
 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5  6 <hibernate-configuration> 7     <!-- 記住:先配置sessionFactoryy --> 8      9     <session-factory>10         <!-- 必須的配置 -->11         <!-- 配置串連資料庫的基本的資訊: -->12         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>13         <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day02</property>14         <property name="hibernate.connection.username">root</property>15         <property name="hibernate.connection.password">123456</property>16         17         <!-- 資料庫的方言: -->18         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>19         <!-- 可選配置 -->20         <!-- 顯示SQL語句,在控制台顯示 -->21         <property name="hibernate.show_sql">true</property>22         <!-- 格式化SQL語句 -->23         <property name="hibernate.format_sql">true</property>24         <!-- 產生資料庫的表結構 25             update:如果沒有表結構,建立表結構。如果存在,不會建立,添加資料26         -->27         <property name="hibernate.hbm2ddl.auto">update</property>28         29         <!-- 設定資料庫的隔離等級,就使用預設值就OK 30         <property name="hibernate.connection.isolation">4</property>31         -->32         33         <!-- 開啟綁定本地的session -->34         <property name="hibernate.current_session_context_class">thread</property>35         36         <!-- 映射設定檔,需要引入映射的設定檔 -->37         <mapping resource="com/itheima/domain/User.hbm.xml"/>38         39 40     </session-factory>41 </hibernate-configuration>
View Code

 

綁定本地的Session

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.