用ssh思想寫的一個學生資訊管理系統

zhupengqq發表於2017-10-15

用ssh思想寫的一個學生資訊管理系統


下面給大家介紹一下  系統架構


(1) 整合Struts2、Spring和Hibernate框架 (2) 實現“登入”功能 (3) 實現“學生資訊管理”功能 (4) 實現“學生成績管理”功能 (5) 實現分頁功能 目的:
掌握Struts2的開發步驟 掌握Hibernate的開發步驟
掌握Spring的開發步驟,理解依賴注入、AOP、事務管理等 掌握Struts2、Spring和Hibernate框架的整合 掌握分頁技術 思路: 1、 建庫建表
2、 利用分層思想,建package 3、 新增Spring開發能力 4、 新增Hibernate開發能力
5、 生成Hibernate所需的POJO類和對映檔案 6、 開發DAO層 (1) 新建DAO層介面。
(2) 新建DAO層實現類(該類要實現DAO層介面,繼承
HibernateDaoSupport類)。
(3) 在Spring配置檔案中增加該DAO層實現類的定義,並需要依
賴注入一個SessionFactory bean的引用。 7、 開發Service層 (1) 新建Service層介面。
(2) 新建Service層實現類,該類中需要宣告所呼叫DAO層介面,
並生其setter方法。
(3) 在Spring配置檔案中增加該Service層實現類的定義,並需要
依賴注入DAO層的bean。 8、 實現WEB層
(1) 在web.xml中增加struts2的過濾器和Spring的監聽器。 (2) 增加Spring和Struts2的整合檔案struts.properties。 (3) 新建所需的jsp檔案。
(4) 新建Action類,該類要繼承ActionSupport,同時該類要依賴注
入Service的bean(宣告+setter方法)。
(5) 在Spring配置檔案中增加該Action類的定義,並注入Service
層的bean。
(6) 在struts.xml中增加該Action的定義,其中class的屬性是在
Spring中定義的該Action bean的id。 9、部署執行 實驗步驟:


StudentAction.java

package com.cn.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.cn.model.Student;
import com.cn.model.User;
import com.cn.service.StudentManager;
import com.cn.service.UserManager;
import com.cn.vo.Info;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Component("student")
@Scope("prototype")
public class StudentAction extends ActionSupport implements ModelDriven{
	
	private Info info = new Info();
	
	private StudentManager studentManager;
	
	private List<Student> students;

	public List<Student> getStudents() {
		return students;
	}
	public void setStudents(List<Student> students) {
		this.students = students;
	}
	public StudentManager getStudentManager() {
		return studentManager;
	}
	@Resource(name="studentManager")
	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
	}
	
	
	public Info getInfo() {
		return info;
	}
	public void setInfo(Info info) {
		this.info = info;
	}

	/**
	 * 增加學生資訊
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception{
		Student stu = new Student();
		stu.setId(info.getId());
		stu.setSid(info.getSid());
		stu.setName(info.getName());
		stu.setCollege(info.getCollege());
		stu.setProfession(info.getProfession());
		studentManager.add(stu);
		return "success";
	}
	
	/**
	 * 查詢學生資訊
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception{
		this.students = this.studentManager.getStudents();
		return "list";
	}
	/**
	 * 獲取需要刪除學生資訊的學號
	 * @return
	 * @throws Exception
	 */
	public String numberlistfordelete() throws Exception{
		this.students = this.studentManager.getStudents();
		return "numberlistfordelete";
	}
	/**
	 * 獲取需要修改學生資訊的學號
	 * @return
	 * @throws Exception
	 */
	public String numberlistformodify() throws Exception{
		this.students = this.studentManager.getStudents();
		return "numberlistformodify";
	}
	
	/**
	 * 刪除學生資訊
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception{
		studentManager.delete(Integer.parseInt(info.getSelect()));
		return "delete";
	}
	
	/**
	 * 更新學生資訊
	 * @return
	 * @throws Exception
	 */
	public String modify() throws Exception{
		Student stu = new  Student();
		stu.setCollege(info.getCollege());
		stu.setName(info.getName());
		stu.setProfession(info.getProfession());
		stu.setSid(info.getSid());
		System.out.println(stu.getCollege()+ " " +stu.getProfession() + " "+ stu.getName()+" "+ stu.getSid());
		studentManager.update(stu.getCollege(), stu.getProfession(), stu.getName(), stu.getSid());
		return "modify";
	}
	
	public Object getModel() {
		// TODO Auto-generated method stub
		return info;
	}
	
	
}
Useraction.java

package com.cn.action;

import javax.annotation.Resource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.cn.model.Student;
import com.cn.model.User;
import com.cn.service.StudentManager;
import com.cn.service.UserManager;
import com.cn.service.impl.UserManagerImpl;
import com.cn.vo.Info;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Component("user")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven{
	private UserManager userManager;
	private Info info = new Info();
	
	private UserManager um ;
	

	
	public UserManager getUserManager() {
		return userManager;
	}
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	public Info getInfo() {
		return info;
	}
	public void setInfo(Info info) {
		this.info = info;
	}
	public UserManager getUm() {
		return um;
	}
	@Resource(name="userManager")
	public void setUm(UserManager um) {
		this.um = um;
	}
	
	/**
	 * 使用者登入判斷的action
	 * @return success登入成功介面 fail登入失敗介面
	 * @throws Exception
	 */

	public String login() throws Exception {
		User u = new User();
		u.setUsername(info.getUsername());
		u.setPassword(info.getPassword());
		if(um.existsUserNamePwd(u)){
			return "success";
		}
		return "fail";
	}
	/**
	 * 使用者註冊
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception{
		User  stu = new User();	
		System.out.println("輸出"+info.getName()+"密碼"+info.getPassword());
		stu.setUsername(info.getUsername());
		stu.setPassword(info.getPassword());
		userManager.add(stu);
		return "success";
	}
	
	
	public Object getModel() {
		// TODO Auto-generated method stub
		return info;
	}
	
	
}

StudentDao.java

package com.cn.dao;

import java.util.List;

import com.cn.model.Student;

public interface StudentDao {
	public void save(Student stu);
	public List<Student> selectStudent();
	public void delete(int sid);
	public void update(String college,String profession,String name, int sid);
	
}


UserDao.java

package com.cn.dao;

import com.cn.model.Student;
import com.cn.model.User;


public interface UserDao {
	public void save(User stu);
	public boolean checkUserExistsWithName(String username);
	public boolean checkUserExistsWithNamePwd(String username,String password);
	
}

StudentDaoImpl.java

package com.cn.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.cn.dao.StudentDao;
import com.cn.model.Student;
@Component("studentDao")
public class StudentDaoImpl implements StudentDao{
	
	private HibernateTemplate hibernateTemplate;
	
	

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	@Resource(name="hibernateTemplate")
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void save(Student stu) {
		hibernateTemplate.save(stu);
	}
	public List<Student> selectStudent() {
		
		return (List<Student>)this.hibernateTemplate.find("from Student s order by s.id asc ");
	}
	public void delete(int sid) {
		
		List<Student> s = hibernateTemplate.find("from Student s where s.sid = '"+sid+"' ");
		hibernateTemplate.deleteAll(s);
	
	}
	public void update(String college,String profession,String name, int sid) {
		Session session = hibernateTemplate.getSessionFactory().openSession();
		session.beginTransaction();
		session.createQuery("update Student stu set stu.college ='"+college+"',stu.profession = '"+profession+"',stu.name = '"+name+"' where stu.sid='"+sid+"' ").executeUpdate();
		session.getTransaction().commit();
		
	}
	
	


	
}
UserDaoImpl.java

package com.cn.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.cn.dao.UserDao;
import com.cn.model.Student;
import com.cn.model.User;
@Component("userDao")
public class UserDaoImpl implements UserDao{
	
	private HibernateTemplate hibernateTemplate;
	
	

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	@Resource(name="hibernateTemplate")
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void save(Student stu) {
		hibernateTemplate.save(stu);
	}

	public boolean checkUserExistsWithName(String username) {
		List<User> users = hibernateTemplate.find("from User u where u.username = '"+username+"' ");
		int count = users.size();
		if(users != null && count > 0) return true;
		
		return false;
	}
	public boolean checkUserExistsWithNamePwd(String username, String password) {
		List<User> users = hibernateTemplate.find("from User u where u.username = '"+username+"' and u.password = '"+password+"'");
		int count = users.size();
		if(users != null && count > 0) return true;
		
		return false;
	}
	@Override
	public void save(User stu) {
		// TODO Auto-generated method stub
		hibernateTemplate.save(stu);
	}

}
下面是對映檔案Student.java
package com.cn.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="stuinfo")
public class Student {
	private int id;
	private int sid;
	private String name;
	private String college;
	private String profession;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCollege() {
		return college;
	}
	public void setCollege(String college) {
		this.college = college;
	}
	public String getProfession() {
		return profession;
	}
	public void setProfession(String profession) {
		this.profession = profession;
	}
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	
	
	
	
}

User.java

package com.cn.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="loginaccount")
public class User {
	private int id;
	private String username;
	private String password;
	@Id
	@GeneratedValue
	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;
	}
	
	
}

StudentManager.java

package com.cn.service;

import java.util.List;

import com.cn.model.Student;
import com.cn.model.User;

public interface StudentManager {

	

	public  void add(Student stu);

	public List<Student> getStudents();
	
	public void delete(int sid);
	
	public void update(String college,String profession,String name, int sid);

}
UserManager.java
package com.cn.service;

import com.cn.model.Student;
import com.cn.model.User;

public interface UserManager {

	public  boolean existsUserName(User u);
	public  boolean existsUserNamePwd(User u);

	public  void add(User log);

}


StudentManagerImpl.java

package com.cn.service.impl;



import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.cn.dao.StudentDao;
import com.cn.model.Student;
import com.cn.model.User;
import com.cn.service.StudentManager;

@Component("studentManager")
public class StudentManagerImpl implements StudentManager {
	private StudentDao studentDao;
	
	
	
	public StudentDao getStudentDao() {
		return studentDao;
	}
	@Resource(name="studentDao")
	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}
	

	

	public void add(Student stu){
		studentDao.save(stu);
	}
	public List<Student> getStudents() {
		// TODO Auto-generated method stub
		return studentDao.selectStudent();
	}
	public void delete(int sid) {
		studentDao.delete(sid);
		
	}
	public void update(String college,String profession,String name, int sid){
		studentDao.update(college, profession, name, sid);
	}
	
	
}
UserManagerImpl.java

package com.cn.service.impl;



import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.cn.dao.UserDao;
import com.cn.model.Student;
import com.cn.model.User;
import com.cn.service.UserManager;

@Component("userManager")
public class UserManagerImpl implements UserManager {
	private UserDao userDao ;
	
	public UserDao getUserDao() {
		return userDao;
	}
	@Resource(name="userDao")
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	
	public boolean existsUserName(User u){
		return userDao.checkUserExistsWithName(u.getUsername());
	}
	
	
	public void add(User stu){
		userDao.save(stu);
	}
	public boolean existsUserNamePwd(User u) {
		// TODO Auto-generated method stub
		return userDao.checkUserExistsWithNamePwd(u.getUsername(), u.getPassword());
	}	
}
HibernateUtil.java

package com.cn.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
	private static SessionFactory sf;
	static {
		sf = new AnnotationConfiguration().configure().buildSessionFactory();
	}
	public static SessionFactory getSessionFactory(){
		return sf;
	}
}

Info.java

package com.cn.vo;

public class Info {
	private String username;
	private String password;
	private int id;
	private int sid;
	private String name;
	private String college;
	private String profession;
	private String select;
	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;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCollege() {
		return college;
	}
	public void setCollege(String college) {
		this.college = college;
	}
	public String getProfession() {
		return profession;
	}
	public void setProfession(String profession) {
		this.profession = profession;
	}
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public String getSelect() {
		return select;
	}
	public void setSelect(String select) {
		this.select = select;
	}
	
	
	
}
beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
           

		<context:annotation-config/>
		<context:component-scan base-package="com.cn"/>
		
  	
		
		
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  
  <!-- results in a setDriverClassName(String) call -->
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://localhost:3306/lk"/>
  <property name="username" value="root"/>
  <property name="password" value="root"/>
  </bean>
  
  <bean id="sessionFactory" 
  		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!--  
    <property name="annotatedClasses">
      <list>
        <value>com.lk.model.User</value>
        <value>com.lk.model.Log</value>
      </list>
    </property>
    -->
    
    <property name="packagesToScan">
      <list>
        <value>com.cn.model</value> 
      </list>
    </property>
    
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">
       org.hibernate.dialect.MySQLDialect
		</prop>
        <prop key="hibernate.show_sql">true</prop>
    </props>

    </property>
  </bean>
  
  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
  	<property name="sessionFactory" ref="sessionFactory"></property>
  </bean>

 <bean id="txManager" 
 	class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
 </bean>


  <aop:config>
  	<aop:pointcut id="bussinessService" expression="execution(public * com.cn.service..*.*(..))"/>
  	<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice"/>
  </aop:config>
  
  <tx:advice id="txAdvice" transaction-manager="txManager">
  	<tx:attributes>
  		<tx:method name="exists" read-only="true"/>
  		<tx:method name="add*" propagation="REQUIRED"/>
  	</tx:attributes>
  </tx:advice>





</beans>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.i18n.encoding" value="UTF-8" />  
    <package name="registration" extends="struts-default" >
       
        
        <action name="user" class="com.cn.action.UserAction" method="login">
            <result name="fail">index.jsp</result>
          	<result name="success">stuAdmin.jsp</result>
        </action>
          <action name="user" class="com.cn.action.UserAction" method="add">
            <result name="fail">index.jsp</result>
          	<result name="success">stuAdmin.jsp</result>
        </action>
        <action name="student" class="com.cn.action.StudentAction" >
          	<result name="list">stuselect.jsp</result>
          	<result name="success">stuaddSuccess.jsp</result>
          	<result name="numberlistfordelete">studelete.jsp</result>
          	<result name="delete">deletesuce.jsp</result>
          	<result name="numberlistformodify">stumodify.jsp</result>
          	<result name="modify">stumodifycheck2.jsp</result>
        </action>
       
    </package>
</struts>
jsp頁面我就不寫了

執行效果圖:


相關文章