java-MyBatis与Spring-配置错误Null指针异常

前端之家收集整理的这篇文章主要介绍了java-MyBatis与Spring-配置错误Null指针异常 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我是一个尝试整合mybatis的新手.最终落在此Null Pointer异常上.

我的POM

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

我的春季配置

<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: 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对象存储以及您的映射器.像您一样在索引方法中实例化UserDao对象将意味着在您进行调用时,映射器为null,因为Spring并未为您设置对象,因此无法自动连接映射器.

您将需要使用@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";
    }

 }
原文链接:https://www.f2er.com/spring/531892.html

猜你在找的Spring相关文章