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)
This is my user test simple java fileCode:<?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 dao mapper classCode: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 Service interface and ServiceImp classCode: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(); }
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(); }this is my controllerCode: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(); } }
and this is my pageCode: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); } }
I get a NullPointer Ex, don't know what is going on!!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>
Any help will be appreciated...
Thanks in advance!..


Reply With Quote