2 wang8831567 wang8831567 于 2014.10.22 10:52 提问

mybaitis springmvc整合空指针异常

这是我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<servlet>
    <servlet-name>mvcdemo</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvcdemo</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定spring bean配置文件所在位置 -->   
<context-param>
         <!-- 这个是固定写法contextConfigLocation -->
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/applicationContext.xml</param-value>
</context-param>

applicationContex.xml文件

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" 
    p:driverClassName="${driver}"
    p:url="${url}"
    p:username="${username}" p:password="${password}" p:maxActive="10" p:maxIdle="10">
</bean>
<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- dataSource属性指定要用到的连接池 -->
    <property name="dataSource" ref="dataSource" />
    <!-- configLocation属性指定mybatis的核心配置文件 -->
    <property name="configLocation" value="classpath:config/Configuration.xml" />
    <!-- 所有配置的mapper文件 -->
    <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<bean id="propertyMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
    <property name="mapperInterface" value="wang.dao.IPropertyDao"></property>  
    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>  
</bean>

这里是mvcdemo-servlet.xml文件
context:annotation-config/

    <!-- spring扫描器,用于实现bean自动载入UserController中 -->
    <context:component-scan base-package="wang.controller"/>    
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
               <property name="prefix" value="/WEB-INF/property/"/>
               <property name="suffix" value=".jsp"/>
    </bean> 

IPropertyDao层
package wang.dao;
import java.util.List;
import wang.model.Property;
public interface IPropertyDao {
public List selectProperty();
}

property.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">


select * from property

最后是controller层
package wang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import wang.dao.IPropertyDao;
import wang.model.Property;

@Controller
@RequestMapping("/property")
public class PropertyController {
private IPropertyDao propertyDao;
private Property property;
@RequestMapping("/show")
public String show(Model model){

    System.out.println(propertyDao.selectProperty());

// property = propertyDao.selectProperty();
// model.addAttribute("property", property);
return "/list";
}
public IPropertyDao getPropertyDao() {
return propertyDao;
}
public void setPropertyDao(IPropertyDao propertyDao) {
this.propertyDao = propertyDao;
}
public Property getProperty() {
return property;
}
public void setProperty(Property property) {
this.property = property;
}

}
运行后错误500,
debug发现propertyDao.selectProperty()是空的,请问大神,是哪里错啦?

1个回答

weixin_39324376
weixin_39324376   2017.07.05 16:51

不用问,我和你出现差不多的错误我也不知道,你知道告诉我一下

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!