我用spring jdbc模板进行增删改查,测试时总是出现空指针异常,查询总是jdbcTemplate.update/query()语句出错。请大家帮帮忙原文件如下:【StudentDaoImp.java】
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import com.bbs.dao.StudentDao;
import com.bbs.model.Student;@Repository
@SuppressWarnings("unchecked")
public class StudentDaoImp implements StudentDao {
protected JdbcTemplate jdbcTemplate;
protected void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
} @Transactional(rollbackFor=Exception.class,propagation=Propagation.REQUIRED)
public boolean deleteStudents(int id) {
String sql = "delete from students where id =" + id;
int rows = jdbcTemplate.update(sql);
if(rows == 1)
return true;
else 
    return false;
} public List<Student> getAllStudents() {
String sql = "select * from students";
List<Student> studentsList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Student>(Student.class));
return studentsList;
} public Student getStudentsById(int id) {
String sql = "select * from students where id =" + id;
List<Student> students = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Student>(Student.class));
return students.get(0);
} public boolean updateStudents(Student student) {
String sql = "update students set name=?, sex=?, age=?, phone=? where id=? ";
int rows = jdbcTemplate.update(sql, student.getName(), student.getSex(), student.getAge(),
student.getPhone(), student.getId());
if(rows == 1)
return true;
else 
    return false;
} public boolean addStudents(Student student) {
String sql = "insert into students(name, sex, age, phone) values(?, ?, ?, ?)";
int rows = jdbcTemplate.update(sql, student.getName(), student.getSex(), student.getAge(), student.getPhone());
if(rows == 1)
return true;
else 
    return false;
}}
【bean.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
       
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306//mysql"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>      
        <!-- 连接池启动时的初始值 -->
        <property name="initialSize" value="2"></property>
        <!-- 连接池的最大值 -->
        <property name="maxActive" value="100"></property>
        <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
        <property name="maxIdle" value="20"></property>
        <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会去预申请一些连接,以免洪峰来时来不及申请 -->
        <property name="minIdle" value="3"></property>
        <!-- 最大等待时间,当没有可用连接时,连接池等待连接释放的最大时间,超过该时间限制会抛出异常 -->
        <property name="maxWait" value="60000"></property>
  
    </bean>
      <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
   abstract="false" lazy-init="default">
     <property name="dataSource">
         <ref bean="dataSource" />
        </property>
  </bean>  <tx:annotation-driven transaction-manager="txManager"/>
  <context:component-scan base-package="com.bbs" />
 
</beans>

解决方案 »

  1.   

    jdbcTemplate为空?在配置文件中没有在dao下面引入sessionFactory。
      

  2.   

    StudentDaoImp中得不到dataSource对象,你在jdbcTemplate = new JdbcTemplate(dataSource);
    时候用beanFactory去得到dataSource放入,或者你在StudentDaoImp配置到spring文件中去
      

  3.   


    <bean id="studentDao" class="xxx.xx.xx.StudentDaoImp">
    <property name="dataSource">
      <ref bean="dataSource" /></bean>
      

  4.   

    你的贴了这么多也没人能具体的帮你。
    我大概说下把。
    你首先试验下,你增删改查一个都不能实现吗?如果就改的不行
    可能是在jsp中你没有<hidden>
    如果都不行,那就是配置错误,
    在beans.xml这么配置
    <bean id="studentDao" class="xxx.xx.xx.StudentDaoImp">
    <property name="dataSource">
       <ref bean="dataSource" /></bean>还不行就用debug
      

  5.   

    好好看看依赖注入吧,你的bean和set方法都乱了
      

  6.   

    看你的代码我就一个感觉,混乱!!spring的ioc恐怕你还得继续看看哟
      

  7.   

    这个注入 dataSource 感觉有问题啊,
    protected void setDataSource(DataSource dataSource) {
    jdbcTemplate = new JdbcTemplate(dataSource);
    }