获取session对象的两种方法

获取session对象的两种方法

1.openSession()
2.getCurrentSession(),此方法需要在hibernate.cfg.xml配置文件中添加 <property name="hibernate.current_session_context_class">thread</property>

以下为两种方法的代码:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;


public class SessionTest {

    @Test
    public void testOpenSession(){
        Configuration config=new Configuration().configure();//获得配置对象
        //获得服务注册对象
        ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
        //获得sessionFactory对象
        SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
        //获得session对象
        Session session=((SessionFactory) sessionFactory).openSession();
        if(session!=null){
            System.out.println("session创建成功");
        }
        else{
            System.out.println("session创建失败");
        }



    }

    @Test
    public void testGetCurrenSession(){
        Configuration config=new Configuration().configure();//获得配置对象
        //获得服务注册对象
        ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
        //获得sessionFactory对象
        SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
        //获得session对象
        Session session=((SessionFactory) sessionFactory).getCurrentSession();
        if(session!=null){
            System.out.println("session创建成功");
        }
        else{
            System.out.println("session创建失败");
        }


    }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: SpringMVC可以通过HttpServletRequest对象获取Session,具体步骤如下: 1. 在Controller方法中添加HttpServletRequest参数。 2. 在方法中通过HttpServletRequest对象获取Session。 3. 使用Session对象进行相关操作,如设置属性、获取属性等。 示例代码如下: ``` @RequestMapping("/test") public String test(HttpServletRequest request) { HttpSession session = request.getSession(); session.setAttribute("username", "张三"); String username = (String) session.getAttribute("username"); return "test"; } ``` 在上述代码中,我们通过HttpServletRequest对象获取Session,并设置了一个名为“username”的属性,然后又获取了该属性的值。 ### 回答2: 在Spring MVC中获取Session的过程非常简单。首先,我们需要在控制器方法的参数列表中添加一个HttpServletRequest类型的参数。然后,我们可以使用这个参数的getSession()方法获取当前请求的Session对象。 下面是一个示例代码: ``` @Controller public class MyController { @RequestMapping("/example") public String handleRequest(HttpServletRequest request) { HttpSession session = request.getSession(); // 在这里可以对Session进行操作,比如设置属性、获取属性等等 return "example"; } } ``` 在这个示例中,我们通过调用request.getSession()方法获得了当前请求的Session对象。接下来,我们可以在控制器方法中对Session进行一些操作,比如设置属性、获取属性等等。最后,我们可以根据需要返回一个视图名称,用于渲染响应。 需要注意的是,如果希望在方法参数中直接使用HttpSession对象,可以直接将它作为参数进行接收,而不是通过HttpServletRequest获取: ``` @Controller public class MyController { @RequestMapping("/example") public String handleRequest(HttpSession session) { // 直接使用session对象进行操作 return "example"; } } ``` 通过这种方式,我们可以更为简洁地获取并操作Session对象。 ### 回答3: 在Spring MVC中,要获取Session对象非常简单。主要有两种方式可以实现: 1. 使用HttpServletRequest对象进行获取: 在Spring MVC的控制器方法中,可以通过使用HttpServletRequest对象获取Session对象。 可以使用以下代码来实现: ```java @RequestMapping("/example") public String example(HttpServletRequest request) { HttpSession session = request.getSession(); // 进行Session相关操作 return "example"; } ``` 在上述代码中,我们通过HttpServletRequest对象的getSession()方法获取Session对象,并进行相关操作。 2. 使用@RequestParam注解获取Session属性: 另一种方式是使用@RequestParam注解来直接获取Session属性。可以通过以下代码实现: ```java @RequestMapping("/example") public String example(@RequestParam("sessionId") String sessionId, HttpSession session) { // 进行Session相关操作 return "example"; } ``` 在上述代码中,我们使用@RequestParam注解来获取SessionsessionId,并通过HttpSession类型的参数来获取Session对象。 无论使用哪种方式,都能够方便地获取和操作Session对象。可以通过Session对象来存储和获取用户的相关数据,并实现用户登录状态的维护和跟踪。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值