在SpringMVC中使用hibernateTemplate获取nullPointerException

在SpringMVC中使用hibernateTemplate获取nullPointerException,spring,spring-mvc,Spring,Spring Mvc,我正在尝试一些spring示例程序。当使用hibernate模板时,我得到了NullPointerException package project4; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Ent

我正在尝试一些spring示例程序。当使用hibernate模板时,我得到了NullPointerException

package project4;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="user")
public class User1 {

  private int id;
  private String name;
  private String password;
  private String gender;
  private String country;


  @Id
  @GeneratedValue
  @Column(name="USER_ID")
  public int getId() {
      return id;
  }
  public void setId(int id) {
      this.id = id;
  }

  @Column(name="USER_NAME")
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }

  @Column(name="USER_PASSWORD")
  public String getPassword() {
      return password;
  }
  public void setPassword(String password) {
      this.password = password;
  }

  @Column(name="USER_GENDER")
  public String getGender() {
       return gender;
  }
  public void setGender(String gender) {
      this.gender = gender;
  }

  @Column(name="USER_COUNTRY")
  public String getCountry() {
      return country;
  }
  public void setCountry(String country) {
      this.country = country;
  }

}
package project4;
import project4.User1;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.HibernateTemplate;

 public class UserDAOImpl1 implements UserDAO1 {

   private HibernateTemplate hibernateTemplate;

   public void setSessionFactory(SessionFactory sessionFactory) {
       this.hibernateTemplate = new HibernateTemplate(sessionFactory);
   }

   @Override
   public void saveUser(User1 user) {

    hibernateTemplate.saveOrUpdate(user);

    }


    @Override
   @SuppressWarnings("unchecked")
     public List<User1> listUser() {
        return hibernateTemplate.find("from user");
   }

    @Override
    public void deleteUser(User1 user) {
          hibernateTemplate.delete(user);
    }

    @Override
    public List<User1> findUser(User1 user) {

        return hibernateTemplate.find("from user where USER_ID=:" +user.getId());
    }

    @Override
    public void updateUser(User1 user) {
        hibernateTemplate.update(user);

    }
 }
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"  
xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans   
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema  
      /jdbc/spring-jdbc-3.0.xsd
 http://www.springframework.org/schema/tx   http://www.springframework.org/schema
       /tx/spring-tx-3.0.xsd
 http://www.springframework.org/schema/util  http://www.springframework.org/schema
     /util/spring-util-3.0.xsd
  http://www.springframework.org/schema/mvc   http://www.springframework.org/schema
         /mvc/spring-mvc-3.0.xsd
  http://www.springframework.org/schema/context  http://www.springframework.org/schema
    /context/spring-context-3.0.xsd">

 <context:annotation-config/>

 <bean id="mySessionFactory"  
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="basicDataSource" />
    <property name="packagesToScan" value="project4"/>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
 </bean>

 <bean id="myUserDAO" class="project4.UserDAOImpl1">
    <property name="sessionFactory" ref="mySessionFactory"/>
  </bean>
 </beans>
我的控制器类如下

package project4;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="user")
public class User1 {

  private int id;
  private String name;
  private String password;
  private String gender;
  private String country;


  @Id
  @GeneratedValue
  @Column(name="USER_ID")
  public int getId() {
      return id;
  }
  public void setId(int id) {
      this.id = id;
  }

  @Column(name="USER_NAME")
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }

  @Column(name="USER_PASSWORD")
  public String getPassword() {
      return password;
  }
  public void setPassword(String password) {
      this.password = password;
  }

  @Column(name="USER_GENDER")
  public String getGender() {
       return gender;
  }
  public void setGender(String gender) {
      this.gender = gender;
  }

  @Column(name="USER_COUNTRY")
  public String getCountry() {
      return country;
  }
  public void setCountry(String country) {
      this.country = country;
  }

}
package project4;
import project4.User1;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.HibernateTemplate;

 public class UserDAOImpl1 implements UserDAO1 {

   private HibernateTemplate hibernateTemplate;

   public void setSessionFactory(SessionFactory sessionFactory) {
       this.hibernateTemplate = new HibernateTemplate(sessionFactory);
   }

   @Override
   public void saveUser(User1 user) {

    hibernateTemplate.saveOrUpdate(user);

    }


    @Override
   @SuppressWarnings("unchecked")
     public List<User1> listUser() {
        return hibernateTemplate.find("from user");
   }

    @Override
    public void deleteUser(User1 user) {
          hibernateTemplate.delete(user);
    }

    @Override
    public List<User1> findUser(User1 user) {

        return hibernateTemplate.find("from user where USER_ID=:" +user.getId());
    }

    @Override
    public void updateUser(User1 user) {
        hibernateTemplate.update(user);

    }
 }
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"  
xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans   
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema  
      /jdbc/spring-jdbc-3.0.xsd
 http://www.springframework.org/schema/tx   http://www.springframework.org/schema
       /tx/spring-tx-3.0.xsd
 http://www.springframework.org/schema/util  http://www.springframework.org/schema
     /util/spring-util-3.0.xsd
  http://www.springframework.org/schema/mvc   http://www.springframework.org/schema
         /mvc/spring-mvc-3.0.xsd
  http://www.springframework.org/schema/context  http://www.springframework.org/schema
    /context/spring-context-3.0.xsd">

 <context:annotation-config/>

 <bean id="mySessionFactory"  
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="basicDataSource" />
    <property name="packagesToScan" value="project4"/>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
 </bean>

 <bean id="myUserDAO" class="project4.UserDAOImpl1">
    <property name="sessionFactory" ref="mySessionFactory"/>
  </bean>
 </beans>
CController.java

import project4.UserDAO1;
import project4.User1;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.validation.BindingResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;


@Controller




public class CController{

    private UserDAO1 userDAO;
    @Autowired
    @Qualifier("myUserDAO")
    private UserDAOImpl1 myUserDAO;

    public void setUserDAO(UserDAO1 userDAO) {
        this.userDAO = userDAO;
    }

    @RequestMapping(value = "/frm4/add", method = RequestMethod.POST)
    public ModelAndView add( @ModelAttribute("add") User1 user,HttpServletRequest  
            request,HttpServletResponse response) throws Exception {
             System.out.println("hai");

        userDAO.saveUser(user);
        System.out.println("hai");
        return new ModelAndView("redirect:list.htm");
    }

        @RequestMapping(params = "delete", method = RequestMethod.POST)
        @Transactional
        public ModelAndView delete(@ModelAttribute("delete") User1 
      user,HttpServletRequest request,HttpServletResponse response) throws Exception {
                  userDAO.deleteUser(user);
                return new ModelAndView("redirect:list.htm");
     }


      @RequestMapping(params = "find", method = RequestMethod.POST)
      @Transactional
      public ModelAndView find(@ModelAttribute("find") User1 user,HttpServletRequest 
             request,HttpServletResponse response) throws Exception {  
                     userDAO.findUser(user);
                      return new ModelAndView("redirect:list.htm");
      }


      @RequestMapping(params = "update", method = RequestMethod.POST)
      @Transactional
    public ModelAndView update(@ModelAttribute("update") User1 user,HttpServletRequest 
             request,HttpServletResponse response) throws Exception {  
                     userDAO.updateUser(user);
                      return new ModelAndView("redirect:list.htm");
      }

       public ModelAndView list(HttpServletRequest request,
               HttpServletResponse response) throws Exception {


              ModelMap modelMap = new ModelMap();
              modelMap.addAttribute("userList", userDAO.listUser());
              modelMap.addAttribute("user", new User1());
              return new ModelAndView("list", modelMap);
      }
  }
我的hibernateTemplate实现类如下

package project4;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="user")
public class User1 {

  private int id;
  private String name;
  private String password;
  private String gender;
  private String country;


  @Id
  @GeneratedValue
  @Column(name="USER_ID")
  public int getId() {
      return id;
  }
  public void setId(int id) {
      this.id = id;
  }

  @Column(name="USER_NAME")
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }

  @Column(name="USER_PASSWORD")
  public String getPassword() {
      return password;
  }
  public void setPassword(String password) {
      this.password = password;
  }

  @Column(name="USER_GENDER")
  public String getGender() {
       return gender;
  }
  public void setGender(String gender) {
      this.gender = gender;
  }

  @Column(name="USER_COUNTRY")
  public String getCountry() {
      return country;
  }
  public void setCountry(String country) {
      this.country = country;
  }

}
package project4;
import project4.User1;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.HibernateTemplate;

 public class UserDAOImpl1 implements UserDAO1 {

   private HibernateTemplate hibernateTemplate;

   public void setSessionFactory(SessionFactory sessionFactory) {
       this.hibernateTemplate = new HibernateTemplate(sessionFactory);
   }

   @Override
   public void saveUser(User1 user) {

    hibernateTemplate.saveOrUpdate(user);

    }


    @Override
   @SuppressWarnings("unchecked")
     public List<User1> listUser() {
        return hibernateTemplate.find("from user");
   }

    @Override
    public void deleteUser(User1 user) {
          hibernateTemplate.delete(user);
    }

    @Override
    public List<User1> findUser(User1 user) {

        return hibernateTemplate.find("from user where USER_ID=:" +user.getId());
    }

    @Override
    public void updateUser(User1 user) {
        hibernateTemplate.update(user);

    }
 }
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"  
xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans   
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema  
      /jdbc/spring-jdbc-3.0.xsd
 http://www.springframework.org/schema/tx   http://www.springframework.org/schema
       /tx/spring-tx-3.0.xsd
 http://www.springframework.org/schema/util  http://www.springframework.org/schema
     /util/spring-util-3.0.xsd
  http://www.springframework.org/schema/mvc   http://www.springframework.org/schema
         /mvc/spring-mvc-3.0.xsd
  http://www.springframework.org/schema/context  http://www.springframework.org/schema
    /context/spring-context-3.0.xsd">

 <context:annotation-config/>

 <bean id="mySessionFactory"  
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="basicDataSource" />
    <property name="packagesToScan" value="project4"/>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
 </bean>

 <bean id="myUserDAO" class="project4.UserDAOImpl1">
    <property name="sessionFactory" ref="mySessionFactory"/>
  </bean>
 </beans>
第42行在哪里

userDAO.saveUser(user);
在控制器类中,我使用jsp页面从用户处获取输入


是否有人可以帮助plz尝试在此处添加
@Autowired

public class CController{

    @Autowired
    private UserDAO1 userDAO;
    .....

并删除
UserDAOImpl1

看起来您的userdaoimplbean并没有注入到控制器中。您可以手动初始化控制器bean并将ref属性设置为myuserdaobean

<bean class="project4.CController">
   <property name="userDAO" ref="myUserDAO"/>
</bean>

或者在controller for UserDao中使用@Autowired注释。

在代码中找不到“myUserDAO”类,因此它将抛出NULLPointerException

创建UserDAOImpl1对象而不是UserDAO1类对象。 示例代码:

public class CController{
  @Autowired
  private UserDAOImpl1 myUserDAO;


  //your logic...
  //..

}
而不是:

private UserDAO1 userDAO;
    @Autowired
    @Qualifier("myUserDAO")
    private UserDAOImpl1 myUserDAO;

@user1847395您可以从控制器中删除UserDAOImpl1,您不使用它,并且已经通过Spring配置映射了它。