Reputation: 3975
I'm a newbie trying to integrate mybatis. finally landed on this Null Pointer exception.
My POM
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.1.1</version>
</dependency>
My Spring Config
<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>
My Mapper
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>
User Bean
Just couple of variables
My 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";
}
}
Error
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)
Upvotes: 3
Views: 4340
Reputation: 3322
Your UserDao
object needs to come from the Spring object store as well as your mapper. Instantiating the UserDao object inside of your index
method like you do will mean the mapper is null
when you make your call, since Spring didn't set the object up for you and thus can't autowire your mapper.
You will want to use an @Resource
annotation to bring your UserDao object into your Controller, like below:
@Controller
@RequestMapping("/authenticate")
public class LoginController {
@Resource
UserDao userDao;
@RequestMapping("/index")
public String index() {
System.out.println(userDao.getUser(1).getPassword());
return "Login";
}
}
Upvotes: 3