mybatis dao层空指针异常。

严重: Servlet.service() for servlet [springmvc] in context with path [/fourth] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.ssm.fourth.service.MapServiceImpl.selectAll(MapServiceImpl.java:20)
at com.ssm.fourth.controller.MapController.second(MapController.java:23)

下面是具体配置 和代码   跪求解决方法 谢谢了


web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>



springmvc
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath:springmvc.xml

1


springmvc
*.action


org.springframework.web.context.ContextLoaderListener

applicationContext.xml
<context:component-scan 
base-package="com.ssm.fourth.service,com.ssm.fourth.dao"></context:component-scan>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties"/>
</bean>

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="driverClassName" value="${driver}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${username}"/>
    <property name="password" value="${password}"/>

    <!-- 初始化连接池大小 -->
    <property name="initialSize" value="${initialSize}"></property>
    <!-- 连接池的最大数量 -->
    <property name="maxActive" value="${maxActive}"></property>
    <!-- 连接池的最大空闲 -->
    <property name="maxIdle" value="${maxIdle}"></property>
    <!-- 连接池的最小空闲 -->
    <property name="minIdle" value="${minIdle}"></property>
    <!-- 连接池的最大等待时间 -->
    <!-- <property name="maxWaite" value="${maxWaite}"></property> -->
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:mybaties/SqlMapConfig.xml"/>
    <!-- 自动扫描mapping.xml文件 -->
    <property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>


springmvc.xml
<mvc:annotation-driven/>
    <context:component-scan base-package="com.ssm.fourth.controller"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsps/"/>
    <property name="suffix" value=".jsp"/>
</bean>


代码:
package com.ssm.fourth.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ssm.fourth.entity.Map;
import com.ssm.fourth.service.MapService;

@Controller
@RequestMapping(value="b")
public class MapController {

@Autowired
private MapService mapService;


@RequestMapping(value="second.action")
public String second(){
    System.out.println("second");
    List<Map> list = mapService.selectAll();
    for(int i=0;i < list.size();i++){
        System.out.println(list.get(i).getLatitude());
    }
    return "second";
}

}

package com.ssm.fourth.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.ssm.fourth.entity.Map;

@Repository
public interface MapDao {

public List<Map> selectAll();

}

package com.ssm.fourth.service;

import java.util.List;

import com.ssm.fourth.entity.Map;

public interface MapService {

public List<Map> selectAll();

}

package com.ssm.fourth.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.ssm.fourth.dao.MapDao;
import com.ssm.fourth.entity.Map;

@Service
public class MapServiceImpl implements MapService{

@Qualifier("mapDao")
private MapDao mapDao;

@Override
public List<Map> selectAll() {
    return this.mapDao.selectAll();
}

}

I don't konw what it is! MyBatis's Exception: I encontered the problem of "NullPointerException" of "SqlSession" .Who caused this?
The Main bad woman is:
In The MyBatis Configuration's file(SqlMaoConfig.xml) :

<environments default="environment">  
    <environment id="environment">  

The "default" and "id" may be the same name.Or maybe NullPointerException.OK ,this is my problem and I solve that through my hands by
searching the internet. Maybe it help you.

看你程序你是在用springmvc+mybatis,但是你没有用spring完全整合mybatis。你的web.xml文件中有这样的引用吗


contextConfigLocation

/WEB-INF/config/spring-*.xml

    建议把spring和mybatis的整合在看下

你去百度搜下,肯定有意外的收获

配置文件中哪里有dao用spring注入的代码了?
@Qualifier("mapDao")
private MapDao mapDao;
这个好像是springmvc的注解,你扫描Controller包下注解,怎么能扫描到dao呢!
还有一般mybatis的mapper都是绑定到sql语句的,直接调用接口就行了,还需要配置dao?