StudentDao类:
package com.dao;
/*import java.util.ArrayList;
import java.util.List;*/
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/*import org.hibernate.query.Query;*/
import com.student.Student;
public class StudentDao {
public Student getById(int id){
Configuration cfg=new Configuration().configure();
SessionFactory sessionFactory=cfg.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tran=session.beginTransaction();
Student s=session.get(Student.class, id);
tran.commit();
session.close();
return s;
}
}
jsp代码:
<%
StudentDao dao=new StudentDao();
Student s=dao.getById(13);
%>
<table>
<tr>
<td><%=s.getName() %></td>
<td><%=s.getNumber() %></td>
<td><%=s.getBirthday() %></td>
</tr>
提示Configuration cfg=new Configuration().configure();这一行代码有错,可是我写的测试类可以成功过去s,在jsp页面中调用
就有错,类已经导入了,数据库也有数据,是运行时的错误。