50分紧急求助大神!hibernate与spring整合空指针异常报错!!搞了一天没搞定!
这个系统暂时实现登陆和注册功能,整体目录结构:
DAO层代码:
//userDAO.java
package dao;
import com.entity.TtUsers;
public interface userDAO {
public void add(TtUsers user);
// public void delete(TtUsers user);
public TtUsers load(Integer id);
//public void update(TtUsers user);
}
package dao;
import java.io.Serializable;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.entity.TtUsers;
@SuppressWarnings("serial")
public class userDAOImpl extends HibernateDaoSupport implements userDAO,Serializable {
@Override
public void add(TtUsers user) {
System.out.println(getSessionFactory());
System.out.println(getHibernateTemplate());
try{
super.getHibernateTemplate().save(user);
}catch(RuntimeException e){
e.printStackTrace();
System.out.println("注册用户失败!");
}
}
/* public void delete() {
}*/
@Override
public TtUsers load(Integer id) {
String hql="from tt_users where id="+id;
@SuppressWarnings("unchecked")
List<TtUsers> list=super.getHibernateTemplate().find(hql);
TtUsers user=(TtUsers)list.get(0);
return user;
}
/* public void update(TtUsers user) {
super.update(user);
}*/
}
package biz;
import com.entity.TtUsers;
import dao.userDAOImpl;
public class UserBiz {
userDAOImpl userDAO;
public userDAOImpl getUserDAO() {
return userDAO;
}
public void setUserDAO(userDAOImpl userDAO) {
this.userDAO = userDAO;
}
public boolean login(int id,String password){
TtUsers user1=userDAO.load(id);
if(!user1.getPassword().equals(password)){
System.out.println("登陆验证失败!");
return false;
}
return true;
}
public void register(TtUsers user){
userDAOImpl dao=new userDAOImpl();
dao.add(user);
}
public TtUsers load(int id){
return new userDAOImpl().load(id);
}
}
package com.entity;
import java.lang.Integer;
/**
* AbstractTtUsers entity provides the base persistence definition of the
* TtUsers entity. @author MyEclipse Persistence Tools
*/
public abstract class AbstractTtUsers implements java.io.Serializable {
// Fields
private Integer id;
private String password;
private String name;
private String gender;
private String birthdate;
private String mailaddress;
// Constructors
/** default constructor */
public AbstractTtUsers() {
}
/** minimal constructor */
public AbstractTtUsers(Integer id) {
this.id = id;
}
/** full constructor */
public AbstractTtUsers(Integer id, String password, String name,
String gender, String birthdate, String mailaddress) {
this.id = id;
this.password = password;
this.name = name;
this.gender = gender;
this.birthdate = birthdate;
this.mailaddress = mailaddress;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return this.gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirthdate() {
return this.birthdate;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
public String getMailaddress() {
return this.mailaddress;
}
public void setMailaddress(String mailaddress) {
this.mailaddress = mailaddress;
}
}
package action;
import java.util.Map;
import biz.UserBiz;
import com.entity.TtUsers;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
UserBiz ub;
public UserBiz getUb() {
return ub;
}
public void setUb(UserBiz ub) {
this.ub = ub;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password ;
}
public void setPassword(String password) {
this.password = password;
}
public String getRepassword() {
return repassword ;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public String getValidate() {
return validate ;
}
public void setValidate(String validate) {
this.validate = validate;
}
public String getMailaddress() {
return mailaddress;
}
public void setMailaddress(String mailaddress) {
this.mailaddress = mailaddress;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirthdate() {
return birthdate;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
private static final long serialVersionUID=1L;
private int id;
private String name;
private String password;
private String repassword;
private String gender;
private String birthdate;
private String mailaddress;
private String validate;
public String execute() throws Exception{
TtUsers user= new TtUsers();
user.setId(id);
user.setName(name);
user.setMailaddress(mailaddress);
user.setPassword( password);
user.setGender(gender);
user.setBirthdate(birthdate);
TtUsers tu=ub.load(user.getId());//查询该身份证号是否已注册
if(tu!=null){
System.out.println("用户名已存在");
this.addFieldError(name ,"用户名已存在!!!" );
return INPUT ;
} else{
System.out.println("可以继续注册!");
ub.register(user);
return SUCCESS ;
}
}
public void validate(){
ActionContext ac=ActionContext. getContext();
Map<String, Object> session=ac.getSession();
System.out.println(session.get("random" ));//显示获取的随机验证码
if(!repassword.equals(password)){ //密码校对
this.addFieldError(repassword , "重复密码不一致!!" );
} if(!session.get("random" ).equals(validate)){//验证码检验
this.addFieldError(validate , "验证码错误!!" );
}
}
}
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:orcl">
</property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/entity/TtUsers.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userDAO" alt="50分大神!hibernate与spring整合空指针错误报错!搞了一天没搞定" />
我用的是Oracle数据库,数据库驱动连接测试是通过的,也就是说数据库这边没问题,就是配置问题了,现在求助各位大神,这个到底啥情况,我找了半天了都没找到错在哪。。。先谢谢各位了! Spring Hibernate 异常 空指针
[解决办法]