调用方法时,显示Mapper空指针异常:java.lang.NullPointerException: Cannot invoke "com.light.light_community.dao.UserMapper.selectById(int)" because "this.userMapper" is null
当我在编写完访问数据库方法准备测试时,运行方法显示Mapper空指针异常
错误提示信息是:java.lang.NullPointerException: Cannot invoke "com.light.light_community.dao.UserMapper.selectById(int)" because "this.userMapper" is null
但是我的Mapper是已经注入的
这是我的工程目录
这是我的application.properties 配置文件
这是我的Mapper接口:UserMapper
package com.light.light_community.dao;
import com.light.light_community.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
User selectById(int id);//根据id查询用户
User selectByName(String name);//根据账号查询用户
User selectByEmail(String email);//根据邮箱查询用户
int insertUser(User user);//插入用户数据
int updateStatus(int id,int status);//根据id更新用户状态
int updateHeader(int id,String headerUrl);//根据id更新用户头像
int updatePassword(int id,String password);//根据id更新用户密码
}
这是我的Mapper接口的映射文件:user-mapper.xml
mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.light.light_community.dao.UserMapper">
<sql id="selectFields">
id,username,password,salt,email,type,status,activation_code,header_url,create_time
sql>
<sql id="insertFields">
username,password,salt,email,type,status,activation_code,header_url,create_time
sql>
<select id="selectById" resultType="User">
select <include refid="selectFields">include>
from user
where id=#{id}
select>
<select id="selectByName" resultType="User">
select <include refid="selectFields">include>
from user
where username=#{username}
select>
<select id="selectByEmail" resultType="User">
select <include refid="selectFields">include>
from user
where email=#{email}
select>
<insert id="insertUser" parameterType="User" keyProperty="id">
insert into user(<include refid="insertFields">include>)
values(#{username},#{password},#{salt},#{email},#{type},#{status},#{activationCode},#{headerUrl},#(createTime))
insert>
<update id="updateStatus">
update user
set status=#{status}
where id=#{id}
update>
<update id="updateHeader">
update user
set header_url=#{headerUrl}
where id=#{id}
update>
<update id="updatePassword">
update password
ser password=#{password}
where id=#{id}
update>
mapper>
这是我的测试程序
@Autowired
private UserMapper userMapper;
@Test
public void testSelectUser(){
User user = userMapper.selectById(101);
System.out.println(user);
}
首先运行这个测试程序时报的是: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.light.light_community.dao.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
这个异常,我在网上查资料,看显示是要将@Autowire的改为@Autowired(required=true),但当我改完之后报的就是Mapper空指针异常了:java.lang.NullPointerException: Cannot invoke "com.light.light_community.dao.UserMapper.selectById(int)" because "this.userMapper" is null
MapperScan
你的resources目录下的路径和java目录下的路径不一致,修改方法:
在resources目录下创建com\light\light_community\dao\UserMapper.目录,将*.xml拷贝到这个目录中。
扫描的包配置了吗
请问你解决了吗,我也遇到了相同的问题