mybatis访问数据库时报java.lang.NullPointerException

本文记录了一个关于Spring MVC项目中登录控制器出现NullPointerException的问题及其解决过程。问题出现在尝试通过UserService查询用户时,具体原因是数据库连接配置中URL的localhost拼写错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)问题描述:

严重: Servlet.service() for servlet [springMvc] in context with path [/New_OPC] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at cn.edu.hdu.Controller.System.LoginController.loginPost(LoginController.java:52)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)等等  

(2)本项目说明
LoginController.java类中

System.out.println("正在执行查询方法");
        System.out.println("查询用户的结果:"+userservice.getUserById(1));
        System.out.println("查询方法执行完毕");

业务接口

public interface UserService {

    User getUserById(Integer userId);

}

业务接口实现类

@service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;


    @Override
    public User getUserById(Integer userId) {
        // TODO Auto-generated method stub
        return userMapper.getUserById(userId);
    }
}

mabatis映射接口

public interface UserMapper  {

    User getUserById(Integer userId);   
}

mabatis映射xml文件

<mapper namespace="cn.edu.hdu.Dao.System.UserMapper">
     <select id="getUserById" parameterType="int" resultMap="User">
        select * from tb_user u where u.user_id = #{userId}
    </select>
</mapper>

数据库:
这里写图片描述

解决:localhost写错了

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://locahost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF8&characterSetResults=utf8
jdbc.username=root
jdbc.password=******
executorService.execute(()->{ projectDataMapper.updateProjectData(pd); });Exception in thread "pool-1-thread-11" org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: java.lang.NullPointerException ### The error may exist in file [D:\work2024\xiaom-product-admin\ruoyi-system\target\classes\mapper\system\ProjectDataMapper.xml] ### The error may involve com.ruoyi.system.mapper.ProjectDataMapper.updateProjectData ### The error occurred while executing an update ### Cause: java.lang.NullPointerException at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441) at com.sun.proxy.$Proxy109.update(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:288) at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:64) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89) at com.sun.proxy.$Proxy147.updateProjectData(Unknown Source) at com.ruoyi.system.service.impl.LuckysheetServiceImpl.lambda$pasteSave$52(LuckysheetServiceImpl.java:2162) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: java.lang.NullPointerException ### The error may exist in file [D:\work2024\xiaom-product-admin\ruoyi-system\target\classes\mapper\system\ProjectDataMapper.xml] ### The error may involve com.ruoyi.system.mapper.ProjectDataMapper.upd
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值