首先创建工程GuanLiPingTai ,构建Spring 、Hibernate和Struts框架。(若是不知道如何构建,可以看我上一篇文章)
反向工程教师表(里面包含教师的账号,密码,对应的权限),用于网页分辨登入人员权限。
新建Class类techerDao
package org.dao;
import java.sql.Timestamp;
import org.model.Teacher;
public interface TeacherDao {
public Teacher login(String name,String psd);
}
新建Dao实现类teacherDaoImp
package org.dao.imp;
import java.util.List;
import org.dao.TeacherDao;
import org.model.Teacher;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class TeacherDaoImp extends HibernateDaoSupport implements TeacherDao {
@SuppressWarnings("unchecked")
public Teacher login(String name, String psd) {
String[] parm = {name, psd};
List<Teacher> l = getHibernateTemplate()
.find("from Teacher t where t.id.TTeacherId = ? and t.TPassword = ?",
parm);
if (l.size() > 0) {
return l.get(0);
} else
return null;
}
}
记住要在applicationContext.xml中加入新的Bean 使TeacherDaoImp能与系统自带的框架相连
<bean id="TeacherDaoImp" class="org.dao.imp.TeacherDaoImp">
<property name="sessionFactory">
<ref bean="mysessionFactory" />
</property>
</bean>
连接成功后Beans Graph会出现相连的箭头
新建Action类WebAction
package org.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.struts2.interceptor.SessionAware;
import org.dao.TeacherDao;
import org.model.SysMenu;
import org.model.Teacher;
import web.util.JSONTools;
import web.util.MD5Util;
import com.opensymphony.xwork2.ActionSupport;
public class WebAction extends ActionSupport implements SessionAware{
public void setSession(Map<String, Object> arg0) {
}
private JSONObject s;
Map session;
private TeacherDao teacherDao;
private String name;
private String psdMd5;//密码
int id;//登录用的Id
private int result;
public int