在mybatis框架中使用事务(增、删、改)时出现空指针异常

问题描述

在mybatis使用事务(增、删、改)时出Exception in thread “main” java.lang.NullPointerException空指针异常
错误代码如下:

package com.yellowsmalltiger.service;

import com.yellowsmalltiger.entity.FlightEntity;
import com.yellowsmalltiger.mapper.FlightMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;



public class FlightService {
    private FlightMapper flightMapper;
    private SqlSession sqlSession;

    public FlightService() throws IOException {
        // 通过无参构造方法 初始化mybatis 得到flightMapper
        // mybatis-config.xml 目录位置
        String resource = "mybatis-config.xml";
        // 1.解析mybatis-config.xml 得到数据库相关的配置信息
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //2.创建得到一个sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //3.获取到sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        flightMapper = sqlSession.getMapper(FlightMapper.class);

    }

    //查询所有航班信息
    public List<FlightEntity> getByFlightAll() {
        return flightMapper.getByFlightAll();
    }


    //根据id查询航班信息
    public FlightEntity getByIdFlight(Integer id) {
        return flightMapper.getByIdFlight(id);
    }

    //插入航班信息
    public int insertFlight(FlightEntity flightEntity) {
        int result = flightMapper.insertFlight(flightEntity);
        // 需要提交事务事务的
        sqlSession.commit();// 提交事务
        sqlSession.close();
        return result;
    }

    public int updateFlight(FlightEntity flightEntity) {
        int result = flightMapper.updateFlight(flightEntity);
        // 需要提交事务事务的  数据增加 update  删除
        sqlSession.commit();// 提交事务
        sqlSession.close();
        return result;
    }

    public int deleteByIdFlight(Integer id) {
        int result = flightMapper.deleteByIdFlight(id);
        // 需要提交事务事务的  数据增加 update  删除
        sqlSession.commit();// 提交事务
        sqlSession.close();
        return result;
    }

//    多条件查询

    public List<FlightEntity> getByIdFlightPoJo(FlightEntity flightEntity){
       return flightMapper.getByIdFlightPoJo(flightEntity);
    }

    // 多态查询
    public List<FlightEntity> getByIdFlightDynamicParameter(FlightEntity flightEntity){
        return flightMapper.getByIdFlightDynamicParameter(flightEntity);
    }
}

报错截图:
在这里插入图片描述

原因分析:

其中的sqlSession根本没有创建成功。


解决方案:

修改FlightService()中的代码为:

//SqlSession sqlSession = sqlSessionFactory.openSession();会报空指针异常
sqlSession = sqlSessionFactory.openSession();

在这里插入图片描述
删除标红圈的SqlSession即可。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yellow Small Tiger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值