当前位置: 代码迷 >> Java Web开发 >> Spring MVC 关于数据库跟dao层 配置 出现“java.lang.NullPointerException”
  详细解决方案

Spring MVC 关于数据库跟dao层 配置 出现“java.lang.NullPointerException”

热度:1084   发布时间:2016-04-13 22:20:14.0
Spring MVC 关于数据库和dao层 配置 出现“java.lang.NullPointerException
dao层

public class DBConnect {
public static Connection connection;
public static Statement statement;
ResultSet resultSet;
public static Connection getConnection(String url,String userName,String password) throws ClassNotFoundException,SQLException{
//String URL="jdbc:oracle:thin:@localhost:1521:orcl";
//jdbc:oracle:thin:@localhost:1521:orcl
//String userName="scott";
//String password="mnbvcxzaq1";
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
connection=DriverManager.getConnection(url,userName,password);
System.out.println("jdbc:"+connection);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return connection;
}
public void close()throws ClassNotFoundException,IOException{
//关闭结果集
if(resultSet!=null){
try {
resultSet.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//关闭声明
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}
public class SelectAllDescImpl extends SimpleJdbcDaoSupport implements SelectAllDesc{

@Override
public ArrayList<BookBean> selectBook() {
// TODO Auto-generated method stub
ArrayList<BookBean> list=new ArrayList<BookBean>();
//sql="select bookid,bookname,booknumber,booktypeid,bookprice,nvl(bookauthor,'')as bookauthor,nvl(bookpublishers,'')as bookpublishers,nvl(authorintroduction,'') as authorintroduction,nvl(bookcontent,'') as bookcontent from bookInfo where enable=1 order by bookid desc";
String sql="select bookid,bookname,booknumber,booktypeid,bookprice,bookauthor,bookpublishers,authorintroduction,bookcontent from bookInfo where enable=1 order by bookid desc";
System.out.println("SelectAllDescImpl"+sql);
try{
list = (ArrayList<BookBean>)getJdbcTemplate().query(sql,ParameterizedBeanPropertyRowMapper
.newInstance(BookBean.class));
}catch(Exception e){
e.printStackTrace();
}
return list;
}

}

controller层
引用

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bird.bean.BookBean;
import com.bird.dao.impl.SelectAllDescImpl;


@Controller
public class TestMainDo {
private SelectAllDescImpl selectAllDescImpl;

@RequestMapping("/main.do")
public ModelAndView mian(HttpServletRequest request, HttpServletResponse response) {
System.out.println("Main.do");
ArrayList<BookBean> list=null;
list=selectAllDescImpl.selectBook();
request.setAttribute("list", list);
return new ModelAndView("TestDemo1");
}
}

application.xml对象注入:
    
<bean id="selectAllDescImpl" class="com.bird.dao.impl.SelectAllDescImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>  

    <import resource="applicationContext-dbresource.xml" />  

applicationContext-dbresource.xml数据库配置:

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
<property name="username" value="scott" />
<property name="password" value="mnbvcxzaq1" />
</bean>
<!--事务模板 -->  
<bean id="template"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="txManager"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<!-- jdbc事务管理器 -->  
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- (this dependency is defined somewhere else) -->
<property name="dataSource" ref="dataSource" />
</bean>

搞了一下午,实在不明白了,这个都是在配置文件中的中间加了输出语句发现都没走,不知道配置文件的注入bean哪里错了?
------解决思路----------------------
引用:
忘记说那句空指针了。
controller层:

 list=selectAllDescImpl.selectBook();

所以,我感觉我配置的bean注入有问题。

TestMainDo?类中的SelectAllDescImpl?没有注入吧???
------解决思路----------------------
楼上正解,主要你配置了使用注解,将selectAllDescImpl注入应该就可以了
  相关解决方案