空指针错误
各位大虾好我Spring手工集成hibernate登录程序报空指针错误请帮忙解决下applicationContext.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.
xmlns:xsi="http://www.
xmlns:aop="http://www.
xmlns:tx="http://www.
xsi:schemaLocation="http://www. http://www.
http://www. http://www.
http://www. http://www.
<!-- 定义数据源 -->
<bean id="dataSource" class="org. destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/myweb" />
<property name="username" value="root" />
<property name="password" value="111111" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="configLocation" ref="dataSource" />
<property name="mappingResources">
<list>
<value>classpath:User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">MySQLDialect</prop>
</props>
</property>
</bean>
<aop:config >
<aop:pointcut id="allservicemanager" expression="execution(* com.ssh.service.*.*(..))"/>
<aop:advisor pointcut-ref="allservicemanager" advice-ref="txAdvice"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<bean id="txManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="hibernteTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="loginDao" class="com.ssh.dao.LoginDao">
<property name="hibernteTemplate" ref="hibernteTemplate" />
</bean>
<bean id="loginService" class="com.ssh.service">
<property name="loginDao" ref="loginDao" />
<property name="txManager" ref="txManager" />
</bean>
</beans>
LoginService.java代码
package com.ssh.service;
import java.util.List;
import com.ssh.dao.LoginDao;
import com.ssh.pojo.User;
public class LoginService {
private LoginDao loginDao;
public void login(User user){
List list = loginDao.getUser();
User user1 = (User)list.get(0);
if(user.getUsername().equals(user1.getUsername()) && user.getPassword().equals(user1.getPassword())){
System.out.println("success" + "username=" +user1.getUsername());
System.out.println("success" + "username=" +user1.getPassword());
}else{
System.out.println("error!!");
}
}
public LoginDao getLoginDao() {
return loginDao;
}
public void setLoginDao(LoginDao loginDao) {
this.loginDao = loginDao;
}
}
User.java 代码
package com.ssh.pojo;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
User.hbm.xml代码:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.
<hibernate-mapping >
<class name="com.ssh.pojo.User" table="user">
<id name="id">
<generator class="native"/>
</id>
<property name="username"/>
<property name="password" />
</class>
</hibernate-mapping>
LoginDao.java代码
package com.ssh.dao;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class LoginDao {
private HibernateTemplate hibernateTemplate;
public List getUser(){
List list = hibernateTemplate.find("from User");
System.out.print(list);
return list;
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
}
其中的 List list = hibernateTemplate.find("from User");这一句报出空指针错误