
正文
绑定本地的Session
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
绑定本地的Session图示解析:

代码的结构:

代码:SaveServlet.java
package com.itheima.servlet; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.itheima.domain.User;
import com.itheima.service.UserService; /**
* Servlet implementation class SaveServlet
*/
public class SaveServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public SaveServlet() {
super();
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User u1=new User();
u1.setName("测试一"); User u2=new User();
u2.setName("测试二"); new UserService().save(u1, u2);
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
UserService.java
package com.itheima.service; import org.hibernate.Session;
import org.hibernate.Transaction; import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.utils.HibernateUtils; public class UserService { public void save(User u1,User u2) {
UserDao dao=new UserDao();
//获取session
Session session=HibernateUtils.getCurrentSession();
//开启事务
Transaction tr=session.beginTransaction();
try {
dao.save1(u1);
dao.save2(u2);
//提交事务
tr.commit();
} catch (Exception e) {
e.printStackTrace();
//出现问题:回滚事务
tr.rollback();
}finally { } }
}
UserDao.java
package com.itheima.dao; import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction; import com.itheima.domain.User;
import com.itheima.utils.HibernateUtils; public class UserDao { public void save1(User u1) { Session session=HibernateUtils.getCurrentSession();
session.save(u1);
}
public void save2(User u2) {
Session session=HibernateUtils.getCurrentSession();
session.save(u2);
}
}
HibernateUtils.java(稍微修改)
package com.itheima.utils; import javax.servlet.jsp.jstl.core.Config; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; /*
* Hibernate框架的工具
* @author chenyanlong
*/
public class HibernateUtils { //Curl+shift+x
private static final Configuration CONFIG;
private static final SessionFactory FACTORY; //编写静态代码
static {
//加载配置文件
CONFIG =new Configuration().configure();
//构造工厂
FACTORY=CONFIG.buildSessionFactory();
} /*
* 从工厂获取Session对象
* @return
*/
public static Session getSession() {
return FACTORY.openSession(); }
//获取本地Session
public static Session getCurrentSession() {
return FACTORY.getCurrentSession();
}
public static void main(String[] args) {
//调用获取session的方法
getSession();
}
}
hibernate.cfg.xml(开启绑定本地的session)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<!-- 记住:先配置sessionFactoryy --> <session-factory>
<!-- 必须的配置 -->
<!-- 配置连接数据库的基本的信息: -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day02</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property> <!-- 数据库的方言: -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 可选配置 -->
<!-- 显示SQL语句,在控制台显示 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 生成数据库的表结构
update:如果没有表结构,创建表结构。如果存在,不会创建,添加数据
-->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 设置数据库的隔离级别,就使用默认值就OK
<property name="hibernate.connection.isolation">4</property>
--> <!-- 开启绑定本地的session -->
<property name="hibernate.current_session_context_class">thread</property> <!-- 映射配置文件,需要引入映射的配置文件 -->
<mapping resource="com/itheima/domain/User.hbm.xml"/> </session-factory>
</hibernate-configuration>
package com.itheima.servlet; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.itheima.domain.User;
import com.itheima.service.UserService; /**
* Servlet implementation class SaveServlet
*/
public class SaveServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public SaveServlet() {
super();
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User u1=new User();
u1.setName("测试一"); User u2=new User();
u2.setName("测试二"); new UserService().save(u1, u2);
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
package com.itheima.service; import org.hibernate.Session;
import org.hibernate.Transaction; import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.utils.HibernateUtils; public class UserService { public void save(User u1,User u2) {
UserDao dao=new UserDao();
//获取session
Session session=HibernateUtils.getCurrentSession();
//开启事务
Transaction tr=session.beginTransaction();
try {
dao.save1(u1);
dao.save2(u2);
//提交事务
tr.commit();
} catch (Exception e) {
e.printStackTrace();
//出现问题:回滚事务
tr.rollback();
}finally { } }
}
package com.itheima.dao; import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction; import com.itheima.domain.User;
import com.itheima.utils.HibernateUtils; public class UserDao { public void save1(User u1) { Session session=HibernateUtils.getCurrentSession();
session.save(u1);
}
public void save2(User u2) {
Session session=HibernateUtils.getCurrentSession();
session.save(u2);
}
}
package com.itheima.utils; import javax.servlet.jsp.jstl.core.Config; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; /*
* Hibernate框架的工具
* @author chenyanlong
*/
public class HibernateUtils { //Curl+shift+x
private static final Configuration CONFIG;
private static final SessionFactory FACTORY; //编写静态代码
static {
//加载配置文件
CONFIG =new Configuration().configure();
//构造工厂
FACTORY=CONFIG.buildSessionFactory();
} /*
* 从工厂获取Session对象
* @return
*/
public static Session getSession() {
return FACTORY.openSession(); }
//获取本地Session
public static Session getCurrentSession() {
return FACTORY.getCurrentSession();
}
public static void main(String[] args) {
//调用获取session的方法
getSession();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<!-- 记住:先配置sessionFactoryy --> <session-factory>
<!-- 必须的配置 -->
<!-- 配置连接数据库的基本的信息: -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day02</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property> <!-- 数据库的方言: -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 可选配置 -->
<!-- 显示SQL语句,在控制台显示 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 生成数据库的表结构
update:如果没有表结构,创建表结构。如果存在,不会创建,添加数据
-->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 设置数据库的隔离级别,就使用默认值就OK
<property name="hibernate.connection.isolation">4</property>
--> <!-- 开启绑定本地的session -->
<property name="hibernate.current_session_context_class">thread</property> <!-- 映射配置文件,需要引入映射的配置文件 -->
<mapping resource="com/itheima/domain/User.hbm.xml"/> </session-factory>
</hibernate-configuration>







