<!-- 建立数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<!-- 数据库驱动,我这里使用的是Mysql数据库 -->
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<!-- 数据库地址-->
<property name="url"
value="jdbc:mysql://localhost:3306/photo">
</property>
<property name="username" value="root"></property>
<property name="password" value="782030"></property>
</bean>
<!-- 把数据源注入给Session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 定义Hibernate的SessionFactory的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/sis/entity/Admin.hbm.xml</value>
<value>com/sis/entity/Folder.hbm.xml</value>
<value>com/sis/entity/File.hbm.xml</value></list>
</property>
</bean>
<bean id="adminDAOImpl" class="com.sis.DAO.impl.AdminDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
————————————————————————————————————————————
public interface AdminDAO {
public boolean login(Admin admin); //admin登录
}
————————————————————————————————————————————
public class AdminDAOImpl extends HibernateDaoSupport implements AdminDAO {
private AdminDAO admindao;
public AdminDAO getAdmindao() {
return admindao;
}
public void setAdmindao(AdminDAO admindao) {
this.admindao = admindao;
}
public boolean login(Admin admin) {
ApplicationContext bf = new ClassPathXmlApplicationContext("applicationContext.xml");
// TODO Auto-generated method stub
Session se =this.getSession();//获取Session对象
String HQL = "from admin where admin.username='"+ admin.getUsername()+"' and admin.password='"+admin.getPassword()+"'" ;
System.out.println("sdsasasadsd");
List<Admin> list = this.getHibernateTemplate().find(HQL);
System.out.println(list);
if (list!=null && !list.isEmpty()){
System.out.println("111111111");
return true;}
else{
System.out.println("ss22222ss");
return false;
}
}
}
————————————————————————————————————————————————
java.lang.NullPointerException
org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:143)
com.sis.DAO.impl.AdminDAOImpl.login(AdminDAOImpl.java:27)
com.sis.Action.AdminAction.doLogin(AdminAction.java:37)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)