学生信息管理系统web版(Struts2+spring+hibernate)

一、需求分析

实现一个可以完成前后端的基于ssh框架的学生管理系统,要求由老师登录,可以看见老师管理的学生,以及对学生进行增删改查的操作。

二、数据库设计

1、学生表设计

CREATE TABLE `student_info` (
  `uuid` varchar(64) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `schoolId` int(11) DEFAULT NULL,
  `gender` varchar(20) DEFAULT NULL,
  `className` varchar(20) DEFAULT NULL,
  `teacherId` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

学生表保存了学生信息以及其对应老师的ID,由于看了阿里巴巴开发手册,其建议不使用外键,便参考进去。

2、老师表设计

CREATE TABLE `teacher_info` (
  `id` varchar(20) NOT NULL,
  `password` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

三、技术分析

由struts2作为controller层,控制页面数据的请求与响应,hibernate作为dao层,对数据库做持久化操作,spring整合service层,使用jsp作为前端技术。前端是比较简单的jsp,下面不再赘述。

项目目录架构如下:

学生信息管理系统web版(Struts2+spring+hibernate)_第1张图片

四、登陆功能实现

(1)form表单提交到struts2的action中。

用户登录

(2)配置struts2的action


            /student_list.jsp
            /login.jsp
        

(3)action的逻辑代码

@Controller
public class LoginAction extends ActionSupport {
    private String id;
    private String password;


    @Resource(name = "teacherService")
    private TeacherService teacherService;
    @Resource(name = "StudentService")
    private StudentService studentService;

    public void setId(String id) {
        this.id = id;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    public String login(){
        return SUCCESS;
    }

    public String loginBy(){
        String t_password = teacherService.findPassowrdByid(id);
        if (password.equals(t_password)){
            List list = studentService.findStudentByTeacherid(id);
            HttpServletRequest request = ServletActionContext.getRequest();
            //teacher登陆成功后将id存入session中
            request.getSession().setAttribute("id",id);
            request.setAttribute("list",list);
            return SUCCESS;
        }else {
            this.addActionError("用户名或密码错误");
            return ERROR;
        }
    }
}

五、增删改查功能实现

增删改查的配置与登陆功能的配置基本类似。

逻辑代码实现:

(1)增加

@Controller
public class addStudentAction extends ActionSupport {
    private String name;
    private String gender;
    private String className;
    private Integer schoolId;

    @Resource(name = "StudentService")
    private StudentService service;

    public void setName(String name) {
        this.name = name;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public void setSchoolId(Integer schoolId) {
        this.schoolId = schoolId;
    }

    /**
     * 添加学生
     * @return
     */
    public String addStudent(){
        StudentInfo info = new StudentInfo();
        info.setClassName(className);
        info.setGender(gender);
        info.setSchoolId(schoolId);
        info.setName(name);
        //从session中获得teacher的id
        String teacherId = (String) ServletActionContext.getRequest().getSession().getAttribute("id");
        info.setTeacherId(teacherId);
        info.setUuid(UUID.randomUUID().toString());

        service.addStudent(info);

        return SUCCESS;
    }
    public String addStudentGo(){
        return SUCCESS;
    }
}

(2)修改

@Controller
public class updateAction extends ActionSupport {
    private String uuid;
    private String name;
    private String gender;
    private String className;
    private Integer schoolId;

    public void setName(String name) {
        this.name = name;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public void setClassName(String className) {
        this.className = className;
    }
    public void setSchoolId(Integer schoolId) {
        this.schoolId = schoolId;
    }
    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    @Resource(name = "StudentService")
    private StudentService service;



    public String update(){
        StudentInfo info = new StudentInfo();
        info.setSchoolId(schoolId);
        info.setUuid(uuid);
        info.setGender(gender);
        info.setClassName(className);
        info.setName(name);
        info.setTeacherId((String) ServletActionContext.getRequest().getSession().getAttribute("id"));
        service.updateStudent(info);
        return SUCCESS;
    }

    public String updateGo(){
        List students = service.findStudentByUuid(uuid);
        StudentInfo st = students.get(0);
        ServletActionContext.getRequest().setAttribute("st",st);
        return SUCCESS;
    }
}

(3)删除

@Controller
public class deleteAction extends ActionSupport {
    private String name;
    @Resource(name = "StudentService")
    private StudentService service;

    public void setName(String name) {
        this.name = name;
    }
    public String deleteByName(){
        service.deleteStudentByName(name);
        return SUCCESS;
    }
}

(4)查找

@Controller
public class ShowAction extends ActionSupport {
    @Resource(name = "StudentService")
    private StudentService studentService;
    public String showView(){
        //从session中获得id,用于查询
        String name = (String) ServletActionContext.getRequest().getSession().getAttribute("id");
        List list = studentService.findStudentByTeacherid(name);
        ServletActionContext.getRequest().setAttribute("list",list);
        return SUCCESS;
    }
}

六、dao层的实现

学生dao:

public class StudentDaoImpl extends HibernateDaoSupport implements StudentDao {

    public void addStudent(StudentInfo st) {
        this.getHibernateTemplate().save(st);
    }

    public void updateStudent(StudentInfo st) {
        this.getHibernateTemplate().update(st);
    }

    public void deleteStudentByUuid(String uuid) {
        List list = (List) this.getHibernateTemplate().find("from StudentInfo where uuid=?0", uuid);
        StudentInfo info = list.get(0);
        this.getHibernateTemplate().delete(info);
    }

    public StudentInfo findStudentByName(String name) {
        DetachedCriteria criteria = DetachedCriteria.forClass(StudentInfo.class);
        criteria.add(Restrictions.eq("name",name));
        List list = (List) this.getHibernateTemplate().findByCriteria(criteria);
        return list.get(0);
    }

    public List findStudentByTeacherid(String id) {
        String hql = "from StudentInfo where teacherId=?0";
        List list = (List) this.getHibernateTemplate().find(hql,id);
        if (list==null || list.size()==0){
            return null;
        }else {
            return list;
        }
    }

    public void updateStudentByname(String name, Integer schoolId) {
        StudentInfo student = findStudentByName(name);
        student.setSchoolId(schoolId);
        this.getHibernateTemplate().update(student);
    }

    public void deleteStudentByName(String name) {
        StudentInfo student = findStudentByName(name);
        this.getHibernateTemplate().delete(student);
    }

    public List findStudentByUuid(String uuid) {
        DetachedCriteria criteria = DetachedCriteria.forClass(StudentInfo.class);
        criteria.add(Restrictions.eq("uuid",uuid));
        List list = (List) this.getHibernateTemplate().findByCriteria(criteria);
        return list;
    }
}

七、其他的一些细节

1.request.setAttribute在jsp页面无法获取

楼主在配置struts2时,不小心把type配置成redirect,重定向的话,request就失效了,配置成请求转发才行。

2.hibernate除了find方法外其他都报错

没有开启事务,其他三个方法默认为readonly。


    
        
    
    
    

八、项目git地址

https://github.com/sustly/student.git

你可能感兴趣的:(Java,Javaweb,spring,hibernate,Struts2,Web开发之路,Java学习)