Results 1 to 3 of 3

Thread: Spring MVC 3.0.1 + MyBatis 3.1.0 + Annotations... not working (null pointer)

  1. #1
    Join Date
    Mar 2012
    Posts
    3

    Default Spring MVC 3.0.1 + MyBatis 3.1.0 + Annotations... not working (null pointer)

    Hello everyone, I'm fighting with some config stuff between Spring MVC and Mybatis... I managed to get a simple hello world but i can't see how retrieve data from oracle database, here is my code:

    This this the spring xml config file. (connect-servlet.xml)
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:jdbc="http://www.springframework.org/schema/jdbc"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
    <context:annotation-config />
    <!-- Enable annotation style of managing transactions -->
    <context:component-scan base-package="cl.fal.connect.controller" />
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    		<property name="prefix" value="/WEB-INF/jsp/" />
    		<property name="suffix" value=".jsp" />
    	</bean>
    	 <!-- datasource -->
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    	 	<property name="driverClassName">
    	 		<value>oracle.jdbc.OracleDriver</value>
    	 	</property>
    	 <property name="url">
    	 	<value>jdbc:oracle:thin:@localhost:1521:XE</value>
    	 </property>
    		 <property name="username">
    		 	<value>admin</value>
    		 </property>
    		 <property name="password">
    		 	<value>admin</value>
    		 </property>
    	</bean>
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
            <property name="dataSource" ref="dataSource" />  
        </bean>
    	<!-- session -->
    	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    	  <property name="dataSource" ref="dataSource" /> 
    	</bean>
    	<!-- mapper --> 
    	<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
    	  <property name="mapperInterface" value="cl.fal.connect.dao.mapper.UserMapper" />
    	  <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    	</bean>
    </beans>
    This is my user test simple java file
    Code:
    package cl.fal.connect.dao;
    public class User {
    	
    	private String id;
    	private String name;
    
               public User(String id, String name) {
    		this.id = id;
    		this.name = name;
    	}
    
               public User() {}	
    	
    	public String getId() {
    		return id;
    	}
    	
    	public void setId(String id) {
    		this.id = id;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	
    	public void setName(String name) {
    		this.name = name;
    	}
    }
    This is my dao mapper class
    Code:
    package cl.fal.connect.dao.mapper;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Select;
    
    
    import cl.fal.connect.dao.User;
    
    public interface UserMapper {
    	
    	  @Select("SELECT * FROM users")
    	  public List<User> getUsers();
    
    }
    This is my Service interface and ServiceImp class
    Code:
    package cl.fal.connect.dao.mapper;
    import java.util.List;
    import cl.fa.connect.dao.User;
    
    public interface FooService {
    
    	public void setUserMapper(UserMapper userMapper);
    	
    	public List<User> findUsers();
    	
    }
    Code:
    package cl.fal.connect.dao.mapper;
    import java.util.List;
    import cl.fal.connect.dao.User;
    
    public class FooServiceImpl implements FooService {
    
    	private UserMapper userMapper;
    	
    	public void setUserMapper(UserMapper userMapper) {
    	  this.userMapper = userMapper;
    	}
    	
    	public List<User> findUsers() {
    		return this.userMapper.getUsers();
    	}
    }
    this is my controller
    Code:
    package cl.fal.connect.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import cl.fal.connect.dao.mapper.FooServiceImpl;
    import cl.fal.connect.dao.mapper.FooService;
    
    
    @Controller
    public class HelloWorldController {
    	
    	private FooServiceImpl fooService;
    	
    	public void setFooService(FooServiceImpl fooService) {
    		this.fooService = fooService;
    	}
    	
    	@RequestMapping("/hello")
    	public ModelAndView helloWorld(){
    		
    		FooService fooService = new FooServiceImpl();
    		String message = fooService.findUsers().get(0).getName();
    		
    		return new ModelAndView("hello", "message", message);
    	}
    }
    and this is my page
    Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Spring 3.0 MVC</title>
    </head>
    <body>
    	${message}
    </body>
    </html>
    I get a NullPointer Ex, don't know what is going on!!

    Any help will be appreciated...

    Thanks in advance!..

  2. #2
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,036

    Default

    This forum is for the Spring Integration project http://www.springsource.org/spring-integration

    I suggest you repost on the Data forum...

    http://forum.springsource.org/forumdisplay.php?27-Data

    They will probably need a stack trace too "I get a NullpointerException" is not enough information.
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    Moved thread.

    About the problem use dependency injection, dont construct your own instance of the FooServiceImpl as you are doing in the controller.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •