java - MyBatis with Spring - 配置错误空指针异常

标签 java spring mybatis

我是一个尝试集成mybatis的新手。终于解决了这个空指针异常。

我的 POM

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.1.1</version>
    </dependency>

我的 Spring 配置

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<context:component-scan base-package="biz.canisrigel.slapMe" />

<!-- enable autowire -->
<context:annotation-config />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/slapMe" />
    <property name="username" value="root" />
    <property name="password" value="adminadmin" />
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="typeAliasesPackage" value="biz.canisrigel.slapMe.bean" />
</bean>

<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="biz.canisrigel.slapMe.mapper" />
</bean>

我的映射器

public interface UserMapper {

UserBean getUser();

}

<mapper namespace="biz.canisrigel.slapMe.mapper.UserMapper">

<select id="getUser" resultType="UserBean">
    Select * from Users where id = 1;
</select>

</mapper>

用户 Bean

只是几个变量

我的 DAO

@Service
public class UserDao {

@Autowired
private UserMapper userMapper;

public UserBean getUser(int id) {

    if (userMapper == null) {
        System.out.println("Errorrrrrr...................");
        // return new UserBean();
    }
    return userMapper.getUser();
}

}

Controller

@Controller
@RequestMapping("/authenticate")
public class LoginController {

@RequestMapping("/index")
public String index() {
    UserDao userDao = new UserDao();
    System.out.println(userDao.getUser(1).getPassword());
    return "Login";
}

}

错误

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:932) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:816) javax.servlet.http.HttpServlet.service(HttpServlet.java:621)

最佳答案

您的 UserDao 对象需要来自 Spring 对象存储以及您的映射器。像你一样在 index 方法中实例化 UserDao 对象将意味着当你调用时映射器是 null ,因为 Spring 没有为你设置对象并且因此无法 Autowiring 您的映射器。

您需要使用 @Resource 注释将您的 UserDao 对象带入您的 Controller,如下所示:

 @Controller
 @RequestMapping("/authenticate")
 public class LoginController {

    @Resource
    UserDao userDao;

    @RequestMapping("/index")
    public String index() {
        System.out.println(userDao.getUser(1).getPassword());
        return "Login";
    }

 }

关于java - MyBatis with Spring - 配置错误空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15280734/

相关文章:

java - 如何在过滤器中添加请求 header 并在 Controller 中获取该 header

java - 一个同步块(synchronized block)与多个 AtomicInteger 增量相比

java - OpenCV FeatureDetector 从文件读取后似乎没有设置各种属性

spring - Spring 中的 'init-method' 之类的东西,但在注入(inject)依赖项后调用?

java - JPA+Hibernate+Mysql的查询语言问题

java - Tomcat 类路径没有改变

java - 如何使用ibatis将java.util.Set类型的数据插入到我的sql数据库中,其中列类型为set?

java - 更改在 JTextArea 中键入的文本的字体

java - mybatis 的 foreach 返回 Null 结果

Java - 避免创建空的子类和接口(interface)或生成 Java 源代码模板