绑定本地的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 SaveServlet 14 */ 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 stub 37 doGet(request, response); 38 } 39 40 }
UserService.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 //获取session 15 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 }
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 }
HibernateUtils.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 chenyanlong 12 */ 13 public class HibernateUtils { 14 15 //Curl+shift+x 16 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 * @return 30 */ 31 public static Session getSession() { 32 return FACTORY.openSession(); 33 34 } 35 //获取本地Session 36 public static Session getCurrentSession() { 37 return FACTORY.getCurrentSession(); 38 } 39 public static void main(String[] args) { 40 //调用获取session的方法 41 getSession(); 42 } 43 }
hibernate.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>