【问题标题】:MyBatis with Spring - configuration error Null Pointer ExceptionMyBatis with Spring - 配置错误 Null Pointer Exception
【发布时间】:2013-02-23 05:09:11
【问题描述】:

我是一个尝试集成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
@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:请求处理失败;嵌套异常是 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)

【问题讨论】:

    标签: java spring mybatis


    【解决方案1】:

    您的 UserDao 对象需要来自 Spring 对象存储以及您的映射器。像您一样在 index 方法中实例化 UserDao 对象将意味着当您拨打电话时映射器是 null,因为 Spring 没有为您设置对象,因此无法自动装配您的映射器。

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

     @Controller
     @RequestMapping("/authenticate")
     public class LoginController {
    
        @Resource
        UserDao userDao;
    
        @RequestMapping("/index")
        public String index() {
            System.out.println(userDao.getUser(1).getPassword());
            return "Login";
        }
    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      • 2021-10-01
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      相关资源
      最近更新 更多