萍凡如我 2015-10-05 13:06 采纳率: 0%
浏览 2278

spring3+mybatis 注入空指针问题,请各位帮忙一下。

大家好,我刚学习三大框架,碰到一个空指针问题怎么都解决不了,快3天了,请帮忙下,谢谢了。
我测试所有的注入都是空指针

applicationContext.xml

 <?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jdbc=" http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
           http://www.springframework.org/schema/aop   
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
           http://www.springframework.org/schema/tx  
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
           http://www.springframework.org/schema/jdbc   
           http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.2.xsd"
    default-autowire="byName" default-lazy-init="false">

    <!-- 开启自动扫描并注册Bean定义支持 -->
    <!-- <context:component-scan base-package="cn.xhf"></context:component-scan> -->

    <!-- 采用注释的方式配置bean -->
    <!-- <context:annotation-config /> -->

    <!-- 配置datasource源 -->
    <context:property-placeholder location="classpath:mysql.properties" />

    <!-- 使用mysql配置属性值 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        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="maxActive" value="${maxActive}"></property>
        <property name="maxIdle" value="${maxIdle}"></property>
        <property name="maxWait" value="${maxWait}"></property>
        <property name="defaultAutoCommit" value="${defaultAutoCommit}"></property>
    </bean>

    <!-- 配置sqlSessionFactory,同时制定数据源 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:Configuration.xml" />
    </bean>

    <!-- 配置SqlSessionTemplate -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
        <constructor-arg index="1" value="BATCH" />
    </bean>

    <!-- 配置dao -->
    <bean id="booksDao" class="cn.xhf.dao.impl.BooksDaoImpl">
        <property name="sqlSession" ref="sqlSession"></property>
    </bean>
    <bean id="usersDao" class="cn.xhf.dao.impl.UsersDaoImpl">
        <property name="sqlSession" ref="sqlSession"></property>
    </bean>

    <!-- 配置service -->
    <bean id="booksService" class="cn.xhf.service.impl.BooksServiceImpl"
        scope="prototype">
        <property name="booksDao" ref="booksDao"></property>
    </bean>

    <bean id="usersService" class="cn.xhf.service.impl.UsersServiceImpl"
        scope="prototype">
        <property name="usersDao" ref="usersDao"></property>
    </bean>

    <!-- 配置action -->
    <bean id="booksAction" class="cn.xhf.action.BooksAction" scope="prototype">
        <property name="booksService" ref="booksService"></property>
    </bean>

    <bean id="usersAction" class="cn.xhf.action.UsersAction" scope="prototype">
        <property name="usersService" ref="usersService"></property>
    </bean>


    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="get*" read-only="true" />
            <tx:method name="select*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 切面配置 -->
<!--    <aop:config>
        <aop:pointcut expression="execution(* cn.xhf.service.*.*(..))"
            id="method" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="method" />
    </aop:config> -->

</beans>

dao层

 package cn.xhf.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;

import cn.xhf.dao.BooksDao;
import cn.xhf.pojo.Books;
/**
 * 
 */
public class BooksDaoImpl implements BooksDao {

    private SqlSession sqlSession;  //就是这个空指针 ,其他bean也是空

    @Override
    public int deleteBooksById(String bid) {
        return sqlSession.delete("books.deleteBooksById", bid);
    }


    @Override
    public int updataBooksById(String bid) {
        return sqlSession.update("books.updataBooksById", bid);
    }

    @Override
    public List<Books> findAll() {
        List<Books> booksList = new ArrayList<Books>();
        booksList = sqlSession.selectList("books.findBooks");
        return booksList;
    }

    @Override
    public List<Books> findBooksByKeyword(String bookName, String bookAuthor) {
        List<Books> bookList = new ArrayList<Books>();
//      Books books = new Books();
//      books.setBookName(bookName);
//      books.setBookAuthor(bookAuthor);
//      bookList = sqlSession.selectList("books.findBooks", books);
        return bookList;
    }

    @Override
    public int insertBooks(Books books) {
        return sqlSession.insert("books.insertBooks", books);
    }

    public SqlSession getSqlSession() {
        return sqlSession;
    }

    public void setSqlSession(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

}

test方法

 package cn.xhf.pojo;

import java.util.List;

import cn.xhf.dao.BooksDao;
import cn.xhf.dao.impl.BooksDaoImpl;

public class TestBooks {
    public static void main(String[] args) {
        BooksDao booksDao = new BooksDaoImpl();
        List<Books> list = booksDao.findAll();
        System.out.println(list.size());
    }
}

报错信息:
Exception in thread "main" java.lang.NullPointerException
at cn.xhf.dao.impl.BooksDaoImpl.findAll(BooksDaoImpl.java:32)
at cn.xhf.pojo.TestBooks.main(TestBooks.java:11)

  • 写回答

2条回答 默认 最新

  • Guan.shuang 2015-10-05 14:48
    关注

    我来看解决办法的。。。。同是初学者。。。

    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀