Mybatis配置sqlsession 空指针异常 全解析

报错

nested exception is java.lang.NullPointerException 空指针异常

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-11-18 16:44:55.380 ERROR 2056 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl' defined in file [W:\~IDEA\web_class\back_end\target\classes\com\example\web_class\service\impl\UserServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.web_class.service.impl.UserServiceImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException

Error building SqlSession.

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.web_class.service.impl.UserServiceImpl]: Constructor threw exception; nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in mapper/UserMapper.xml

错误原因及解决方案

过程分析(排错的过程、思路)
项目结构:
在这里插入图片描述

错误原因:忘记加注解,或者注解加错了

  1. UserController调用userMapper接口
package com.example.web_class.controller; 
......
@Autowired   /****** 1.  @Autowired 忘记加会出现报错!******/
private UserService userService;
    
@PostMapping("login")
    public Result login(
            @RequestParam(value = "username",required = false) String username,
            @RequestParam(value = "password",required = false) String password,
            ModelAndView modelAndView){
        User user = User.builder().username(username).password(password).build();
        //userService被调用
        String data = userService.login(user);
        return data!=null? ResultGenerator.genSuccessResult(data):ResultGenerator.genFailResult("账号密码错误!");
    };
  1. 查看userService接口
package com.example.web_class.service;
......
@Service  /****** 2.  @Service  忘记加会出现报错!******/
public interface UserService {
    String login(User user);
}
  1. userService接口的实现 implement
package com.example.web_class.service.impl;
......
@Service  /****** 3.  @Service  忘记加会出现报错!******/
public class UserServiceImpl implements UserService{
    String resource= "config/mybatis-config.xml";
    //    JAVA中getResourceAsStream这个方法是用来获取配置文件
    InputStream inputStream= Resources.class.getResourceAsStream((resource));
    /******** 接下来这几句就是报出空指针异常的语句!!!  **********/
    InputStream inputStream= Resources.class.getResourceAsStream((resource));
    SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession=sqlSessionFactory.openSession(true);
    UserMapper userMapper =sqlSession.getMapper(UserMapper.class);
    ......
}

配置文件的路径写错了

String resource= "config/mybatis-config.xml";
IDEA 无法识别java包下面的xml文件。可以修改pom文件识别。

<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

配置文件里面mapper.xml的路径写错了

//mybatis-config.xml
......
    <mappers>
<!--        <mapper resource="com/example/web_class/mapper/UserMapper.xml"/>-->
        <mapper resource="mapper/UserMapper.xml"/>
        <mapper resource="mapper/MailMapper.xml"/>
    </mappers>
......

A 无法识别java包下面的xml文件,同样可以修改pom文件识别。xml路径中不能时com.example.这样的,要用/,同时注意url、resource等的区别。

超级难以难以察觉到的错误!导入错误的包!(resources)

在这里插入图片描述

import org.apache.ibatis.io.Resources;
...
//正确的resources包应该是 org.apache.ibatis.io.Resources;
    InputStream inputStream= Resources.getResourceAsStream((resource));

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页
评论

打赏作者

caesarding

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值